GH-3398: Fix potential ClassLoader leak caused by ThreadLocal lambda in Binary.java #3447
Open
LuciferYang wants to merge 1 commit intoapache:masterfrom
Open
GH-3398: Fix potential ClassLoader leak caused by ThreadLocal lambda in Binary.java #3447LuciferYang wants to merge 1 commit intoapache:masterfrom
LuciferYang wants to merge 1 commit intoapache:masterfrom
Conversation
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.
Rationale for this change
Fixes #3398
In
FromCharSequenceBinary, aThreadLocal<CharsetEncoder>is initialized viaThreadLocal.withInitial(StandardCharsets.UTF_8::newEncoder). The lambda generates a dynamicSupplierclass loaded by the current application ClassLoader. In long-lived thread pool environments (Spark/Flink executors, web containers), pooled worker threads survive job cancellation or hot-redeployment, retaining a strong reference chain:Thread→ThreadLocalMap→SuppliedThreadLocal→ lambda → ClassLoader. This permanently pins the application ClassLoader, preventing class unloading and causing linear Metaspace growth, eventually leading tojava.lang.OutOfMemoryError: Metaspace.What changes are included in this PR?
Replaced the
ThreadLocal<CharsetEncoder>based encoding inFromCharSequenceBinarywith a statelessvalue.toString().getBytes(StandardCharsets.UTF_8), consistent with the existingFromStringBinary.encodeUTF8()at line 251 in the same file.Also note that the original
catch (CharacterCodingException)block was effectively dead code —StandardCharsets.UTF_8is a standard charset guaranteed to be available, soCharsetEncoder.encode()would never throwCharacterCodingExceptionfor unsupported charset reasons.Are these changes tested?
Yes. All existing tests in
parquet-columnpass without modification.Are there any user-facing changes?
No. The encoding behavior is identical — both approaches produce the same UTF-8 byte sequence.
Performance note:
fromCharSequence()is only invoked as a fallback inAvroWriteSupport.fromAvroString()for non-String, non-Utf8CharSequenceimplementations. The dominant write paths usefromReusedByteArray()(AvroUtf8) andfromString()(JavaString), both of which already use stateless encoding withoutThreadLocal. Therefore, no measurable performance regression is expected.