Fix KryoSerializer ThreadLocal ClassLoader leak in web containers#353
Merged
Fix KryoSerializer ThreadLocal ClassLoader leak in web containers#353
Conversation
Add reset() default method to Serializer interface and implement it in KryoSerializer to call kryos.remove(), preventing ClassLoader pinning / Metaspace leaks in web-container environments. Co-authored-by: hazendaz <975267+hazendaz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Review and propose fix for issue 351
Fix KryoSerializer ThreadLocal ClassLoader leak in web containers
Mar 4, 2026
hazendaz
approved these changes
Mar 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
KryoSerializerholds aThreadLocal<Kryo>on an enum singleton (effectively static) with no cleanup path. In containers like Tomcat that reuse threads across redeployments, this pins theWebappClassLoadervia:Thread → ThreadLocalMap → Kryo → Kryo.class → WebappClassLoader, causing repeatedOutOfMemoryError: Metaspace.Changes
Serializer— adds adefault void reset()no-op, giving callers a type-safe cleanup contract without breaking existing implementations.KryoSerializer— overridesreset()to callkryos.remove(), severing the reference chain.SerializerTestCase— addstestKryoResetClearsThreadLocalverifyingreset()completes without error and the serializer remains fully functional afterward (lazy re-initialization).Usage
Register a
ServletContextListenerand callreset()per worker thread on shutdown:Or use it generically through the
Serializerinterface if the concrete type is unknown:✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.