Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/main/java/org/htmlunit/HttpWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
private static final String HACKED_COOKIE_POLICY = "mine";

// have one per thread because this is (re)configured for every call (see configureHttpProcessorBuilder)
// do not use a ThreadLocal because this in only accessed form this class
// do not use a ThreadLocal because this in only accessed form this class, but we still need it synchronized
private final Map<Thread, HttpClientBuilder> httpClientBuilder_ = new WeakHashMap<>();
private final WebClient webClient_;

Expand Down Expand Up @@ -211,7 +211,9 @@
// Calling code may catch the StackOverflowError, but due to the leak, the httpClient_ may
// come out of connections and throw a ConnectionPoolTimeoutException.
// => best solution, discard the HttpClient instance.
httpClientBuilder_.remove(Thread.currentThread());
synchronized (httpClientBuilder_) {

Check warning on line 214 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use ReentrantLock rather than synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":18,"endLine":216,"startColumn":17,"startLine":214}}}],"message":{"text":"Use ReentrantLock rather than synchronization"},"ruleId":"AvoidSynchronizedStatement","ruleIndex":4}
httpClientBuilder_.remove(Thread.currentThread());

Check warning on line 215 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 To be compliant to J2EE, a webapp should not use any thread. Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":53,"endLine":215,"startColumn":47,"startLine":215}}}],"message":{"text":"To be compliant to J2EE, a webapp should not use any thread."},"ruleId":"DoNotUseThreads","ruleIndex":13}
}
throw e;
}
}
Expand Down Expand Up @@ -530,7 +532,7 @@
*
* @return the initialized HTTP client
*/
protected HttpClientBuilder getHttpClientBuilder() {
protected synchronized HttpClientBuilder getHttpClientBuilder() {

Check warning on line 535 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use block level locking rather than method level synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":66,"endLine":535,"startColumn":46,"startLine":535}}}],"message":{"text":"Use block level locking rather than method level synchronization"},"ruleId":"AvoidSynchronizedAtMethodLevel","ruleIndex":5}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rschwietzke is this correct, in the other cases we sync on the httpClientBuilder_ object and in this case on the HttpWebConnection. My guess is we should always sync on the httpClientBuilder_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. That was not fully correct, but might have worked somehow. ;) PR updates

final Thread currentThread = Thread.currentThread();
HttpClientBuilder builder = httpClientBuilder_.get(currentThread);
if (builder == null) {
Expand Down Expand Up @@ -1288,7 +1290,9 @@
*/
@Override
public void close() {
httpClientBuilder_.clear();
synchronized (httpClientBuilder_) {

Check warning on line 1293 in src/main/java/org/htmlunit/HttpWebConnection.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Use ReentrantLock rather than synchronization Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/HttpWebConnection.java"},"region":{"endColumn":10,"endLine":1295,"startColumn":9,"startLine":1293}}}],"message":{"text":"Use ReentrantLock rather than synchronization"},"ruleId":"AvoidSynchronizedStatement","ruleIndex":4}
httpClientBuilder_.clear();
}
sharedAuthCache_.clear();
httpClientContextByThread_.clear();

Expand Down