GeezerVison is showing github.com — Faster leak detector implementation for tests by yawkat · Pull Request #15622 · netty/netty
Conversation
yawkat
commented
Sep 4, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
For some tests such as Bzip2IntegrationTest.testHugeDecompress, normal leak detection is too slow. Similarly, for fuzzing, I have to use paranoid leak detection to detect bugs in a reproducible way, which in turn adds a performance cost that may prevent some bugs from being found.
Leak presence detection could be very fast though – it just won't give us any details about the leak when it triggers.
Modification:
Introduce a LeakPresenceDetector (netty-common) that uses a LongAdder to count open resources. A LeakPresenceExtension (netty-testsuite-common, not public API) checks that counter after each test, and reports an error if a resource remains open.
The big problem with this approach is that some resources are held in static variables, which may be initialized inside a test, and will count towards the counter. Fundamentally, the only way to exclude these resources involves capturing a stack trace during tracker creation and checking if we're in some <clinit>. We could avoid actually materializing the StackTraceElements, but that would mean saving the trackers somewhere where we can inspect them, which is also not ideal.
Instead, I chose to add a wrapper method staticInitializer that static initializers would need to wrap around any resource creation. This is demonstrated in LeakPresenceDetectorTest.ResourceInStaticVariable1. This approach is pretty performant, but it requires changes in a few places inside netty, and potentially in user code. Since this is only necessary for the improved LeakPresenceDetector which is intended for tests and opt-in, I think this is an acceptable compromise.
Alternative suggestions welcome.
Result:
Low-cost leak detection for tests. Bzip2IntegrationTest runs basically just as fast as without leak presence detection.
Leaks can be unambiguously associated with a particular test, instead of only being reported on GC.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
For fuzzing, paranoid leak detection is necessary, but running GC for each test is too slow. Instead, I check the `allLeaks` field after each run for any open trackers (with some pruning to avoid static buffers). This means that a lot of the tracked data, and in particular the whole reference queue, is unnecessary.
For better performance, it would be nice to instead only track the number of open leaks, and compare that number before and after a test runs. This can be achieved using a custom ResourceLeakTracker implementation, but for that ResourceLeakDetector.track must become non-final.
Modification:
Make the method non-final.
Also fix a bunch of warnings.
Result:
ResourceLeakDetector.track can be overridden for more efficient leak detection.
Motivation:
For some tests such as Bzip2IntegrationTest.testHugeDecompress, normal leak detection is too slow. Leak *presence* detection can be very fast though – it just won't give us any information about the leak if it occurs.
Modification:
Introduce a LeakPresenceDetector that uses a LongAdder to count open resources. A LeakPresenceExtension checks that counter after each test, and reports an error if a resource remains open.
The big problem with this approach is that some resources are held in static variables, which may be initialized inside a test, and will count towards the counter. Fundamentally, the only way to exclude these resources involves capturing a stack trace during tracker creation and checking if we're in some `<clinit>`. We could avoid actually materializing the StackTraceElements, but that would mean saving the trackers somewhere where we can inspect them, which is also not ideal.
Instead, I chose to add a wrapper method `staticInitializer` that static initializers would need to wrap around any resource creation. This is demonstrated in `LeakPresenceDetectorTest.ResourceInStaticVariable1`. This approach is pretty performant, but it would require changes in a few places inside netty, and potentially in user code. Since this is only necessary for the improved LeakPresenceDetector which is intended for tests and opt-in, I think this is an acceptable compromise.
Alternative suggestions welcome.
Result:
Low-cost leak detection for tests. Bzip2IntegrationTest runs basically just as fast as without leak detection.
Leaks can be unambiguously associated with a particular test, instead of only being reported on GC.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
yawkat
changed the title
LeakPresenceDetector draft
Faster leak detector implementation for tests
Sep 5, 2025
|
Test failures in tcnative, I guess I have to get those tests running locally...
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@yawkat ping me once its ready for review
|
yawkat reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First round of review... This is an amazing idea.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
|
<argLine.jni>-D_</argLine.jni>
|
|
|
<argLine.leak>-D_</argLine.leak> <!-- Overridden when 'leak' profile is active -->
|
|
|
<!-- This default enables leak presence detection. The leak profile replaces this with normal paranoid leak detection. -->
|
|
|
<argLine.leak>-Djunit.jupiter.extensions.autodetection.enabled=true</argLine.leak>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
yawkat
commented
Sep 12, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
georgebanasios reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
It's worth thinking about publishing a netty-junit5 module that has the leak presence extension in the future. It should be useful in many other projects that use netty directly. But I'm not really happy enough with the extension to do it now, The multithreading support is very hacky.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ension"
This reverts commit 3015129.
|
Reverted. Didn't work with parameterized tests.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# codec-compression/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had a couple of comments, but this is very nice work, Jonas!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# codec-compression/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java
|
@yawkat this is awesome! Thanks a lot for all the work on this.
|
yawkat reacted with hooray emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|