GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Faster leak detector implementation for tests by yawkat · Pull Request #15622 · netty/netty

Conversation

@yawkat

@yawkat yawkat commented Sep 4, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

All reactions
yawkat added 3 commits September 4, 2025 09:38
@yawkat
Make ResourceLeakDetector.track not final
f9c42f6
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.
@yawkat
revert change to fix source compatibility
7ca07d6
@yawkat
LeakPresenceDetector draft
0f9462d
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.
@yawkat

yawkat commented Sep 4, 2025

Copy link
Copy Markdown
Contributor Author

cc @chrisvest @franz1981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Base automatically changed from custom-detector to 4.2 September 4, 2025 15:12
yawkat added 3 commits September 5, 2025 15:51
@yawkat
progress
7036e3a
@yawkat
Merge branch '4.2' into custom-detector
ca3da3b
@yawkat
Merge branch 'custom-detector' into leak-presence-detector
07d1b92
@yawkat
yawkat marked this pull request as ready for review September 5, 2025 13:53
@yawkat yawkat changed the title LeakPresenceDetector draft Faster leak detector implementation for tests Sep 5, 2025
@yawkat
yawkat requested review from chrisvest and normanmaurer September 5, 2025 13:54
@yawkat yawkat added the improvement label Sep 5, 2025
@yawkat

yawkat commented Sep 5, 2025

Copy link
Copy Markdown
Contributor Author

Test failures in tcnative, I guess I have to get those tests running locally...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat
move to OPENSSL_REFCNT in some tests
33b9975
@normanmaurer

normanmaurer commented Sep 8, 2025

Copy link
Copy Markdown
Member

@yawkat ping me once its ready for review

yawkat reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yawkat added 14 commits September 9, 2025 09:30
@yawkat
move to OPENSSL_REFCNT in some tests
b7bdb0a
@yawkat
as long as we release, we don't actually need OPENSSL_REFCNT.
93a95b5
@yawkat
style
9d1aa81
@yawkat
Merge branch '4.2' into leak-presence-detector
a89e72d
@yawkat
update version
9421f02
@yawkat
fix ocsp tests
eda5114
@yawkat
wait for event loop shutdown
15b2c87
@yawkat
disable testHugeDecompress for slow leak detection again
8fd7f84
@yawkat
checkstyle
633cc87
@yawkat
wait for shutdown
1ad9fdf
@yawkat
Add a grace period for shutdown
8cc27e7
@yawkat
release context
ea28195
@yawkat
style
016ae30
@yawkat
fix ssl init
32552c7
yawkat
yawkat commented Sep 10, 2025
View reviewed changes
Comment thread handler/src/test/java/io/netty/handler/ssl/OpenSslClientContextTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@yawkat
Update handler/src/test/java/io/netty/handler/ssl/OpenSslClientContex…
bd372ae
…tTest.java
normanmaurer
normanmaurer requested changes Sep 12, 2025
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread common/src/main/java/io/netty/util/LeakPresenceDetector.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread handler/src/test/java/io/netty/handler/ssl/CloseNotifyTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread handler/src/test/java/io/netty/handler/ssl/OpenSslCertificateCompressionTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pom.xml
<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>

@normanmaurer normanmaurer Sep 12, 2025

Copy link
Copy Markdown
Member

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.

<3

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread testsuite-common/pom.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer mentioned this pull request Sep 12, 2025
OpenSSL: Retain context before using it in the engine constructor #15649
Merged
yawkat added 4 commits September 12, 2025 10:12
@yawkat
CR
ce81e62
@yawkat
Use test-jar
3ad4524
@yawkat
Merge branch '4.2' into leak-presence-detector
359752f
@yawkat
fix new test
5466bf5
@yawkat

yawkat commented Sep 12, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@georgebanasios https://github.com/netty/netty/actions/runs/17672187009/job/50226618760 This looks like a spurious failure in the new AutoScalingEventExecutorChooserFactoryTest, could you take a look?

georgebanasios reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat

yawkat commented Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat
Use an InvocationInterceptor instead of BeforeEach in the extension
3015129
@yawkat

yawkat commented Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

I've changed the extension a bit as suggested in junit-team/junit-framework#4938 . I think it's less hacky now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat
Revert "Use an InvocationInterceptor instead of BeforeEach in the ext…
05416ca
…ension"

This reverts commit 3015129.
@yawkat

yawkat commented Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

Reverted. Didn't work with parameterized tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer approved these changes Sep 16, 2025
View reviewed changes
@normanmaurer

normanmaurer commented Sep 16, 2025

Copy link
Copy Markdown
Member

@chrisvest PTAL as well

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat
Merge branch '4.2' into leak-presence-detector
496f0b8
@yawkat yawkat mentioned this pull request Sep 25, 2025
Fix how huge decompression was disabled #15711
Merged
@yawkat
Merge branch '4.2' into leak-presence-detector
763dc69
# Conflicts:
#	codec-compression/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java
chrisvest
chrisvest reviewed Sep 25, 2025
View reviewed changes

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread testsuite-common/pom.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread codec-http2/src/main/java/io/netty/handler/codec/http2/Http2CodecUtil.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread codec-http3/pom.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

yawkat added 3 commits September 29, 2025 07:37
@yawkat
Revert "Use test-jar"
d7b0789
This reverts commit 3ad4524.
@yawkat
Merge branch '4.2' into leak-presence-detector
f3eab74
# Conflicts:
#	codec-compression/src/test/java/io/netty/handler/codec/compression/AbstractIntegrationTest.java
@yawkat
checkstyle
d78ced8
chrisvest
chrisvest approved these changes Oct 1, 2025
View reviewed changes
@normanmaurer normanmaurer added this to the 4.2.7.Final milestone Oct 2, 2025
@normanmaurer
normanmaurer merged commit bf03648 into 4.2 Oct 2, 2025
@normanmaurer
normanmaurer deleted the leak-presence-detector branch October 2, 2025 06:18
@normanmaurer

normanmaurer commented Oct 2, 2025

Copy link
Copy Markdown
Member

@yawkat this is awesome! Thanks a lot for all the work on this.

yawkat reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@clescot clescot mentioned this pull request Feb 26, 2026
Netty 4.2 support planned ? nettyplus/netty-leak-detector-junit-extension#57
Open
@k-wall k-wall mentioned this pull request Jul 23, 2026
[Future] Upgrade to JUnit 6 kroxylicious/kroxylicious#4439
Open
1 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer approved these changes
@chrisvest chrisvest chrisvest approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

improvement

Projects

None yet

Milestone

4.2.7.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@yawkat @normanmaurer @chrisvest
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.