Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread leak/invasion #5589

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Conversation

snazy
Copy link

@snazy snazy commented Apr 2, 2024

Jersey/Jetty, at least in the 3.1 version line, creates one thread for each HTTP request. This behavior was introduced with #5372 and seems not present in the 2.x or 3.x versions of Jersey.

From the javadoc of java.util.Timer:

Implementation note: All constructors start a timer thread.
...
After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur.

It is fair to assume that "arbitrarily long" may also mean never, in case GC never runs.

This change replaces the timer & thread per request with a ScheduledExecutorService instance per JettyHttpContainer.

Also changed the set-timeout mechanism to use System.nanoTime() instead of System.currentTimeMillis(), because the latter is prone to wall-clock drift and can result into wrong timeout values.

Fixes #5588

Jersey/Jetty, at least in the 3.1 version line, creates one thread for each HTTP request. This behavior was introduced with eclipse-ee4j#5372 and seems not present in the 2.x or 3.x versions of Jersey.

From the javadoc of `java.util.Timer`:
```
Implementation note: All constructors start a timer thread.
...
After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur.
```
It is fair to assume that "arbitrarily long" may also mean _never_, in case GC never runs.

This change replaces the timer & thread per request with a `ScheduledExecutorService` instance per `JettyHttpContainer`.

Also changed the set-timeout mechanism to use `System.nanoTime()` instead of `System.currentTimeMillis()`, because the latter is prone to wall-clock drift and can result into wrong timeout values.

Fixes eclipse-ee4j#5588

Signed-off-by: Robert Stupp <[email protected]>
@snazy
Copy link
Author

snazy commented Apr 2, 2024

/cc @senivam @jansupol

@wendigo
Copy link

wendigo commented Apr 4, 2024

@senivam is this going to land in the next 3.1.x release?

@jansupol jansupol merged commit de52713 into eclipse-ee4j:3.1 Apr 4, 2024
3 checks passed
@senivam
Copy link
Contributor

senivam commented Apr 4, 2024

@wendigo, it's heading there

@senivam senivam added this to the 3.1.6 milestone Apr 4, 2024
@snazy snazy deleted the fix-timer-thread-invasion branch April 4, 2024 13:10
This was referenced Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants