Repository navigation
Conversation
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a classloader leak/stale classloader issue in UnboundedScheduledExecutorService where lazily spawned worker threads inherited the contextClassLoader of the task-submitting thread (such as a shared gRPC executor thread holding a closed Flink SafetyNetWrapperClassLoader) instead of the thread that constructed the executor service. The fix cleanly captures the creator thread's contextClassLoader at construction time and applies it in the ThreadFactory, accompanied by a targeted unit test.
🔍 General Feedback
- Capturing
Thread.currentThread().getContextClassLoader()duringUnboundedScheduledExecutorServiceconstruction and pinning it on newly created worker threads is a clean, minimal, and idiomatic fix. - The unit test properly verifies that tasks submitted from a thread with a distinct
contextClassLoaderexecute on worker threads using the creator'scontextClassLoader, while cleaning up thread state and the executor infinallyblocks.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Fixed Checker Framework /gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a classloader leak/stale classloader issue in UnboundedScheduledExecutorService where lazily created worker threads previously inherited the contextClassLoader of the submitting thread (such as a shared gRPC executor thread holding a closed Flink SafetyNetWrapperClassLoader) instead of the thread that constructed the executor service. The fix cleanly captures the creator thread's contextClassLoader at construction time and applies it in the ThreadFactory, accompanied by a targeted unit test.
🔍 General Feedback
- Capturing
Thread.currentThread().getContextClassLoader()in theUnboundedScheduledExecutorServiceconstructor and setting it on threads created byMoreExecutors.platformThreadFactory()is clean, minimal, and properly annotated for Checker Framework nullness checks. - Because the constructor immediately submits the long-running
TaskLaunchertask, the unit test's subsequentsubmitcall from a separate thread deterministically exercises lazy worker thread creation under a different submittingcontextClassLoader.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41871 +/- ##
=========================================
Coverage 59.58% 59.58%
Complexity 5182 5182
=========================================
Files 1471 1471
Lines 188317 188317
Branches 3882 3882
=========================================
Hits 112205 112205
Misses 72699 72699
Partials 3413 3413 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Note: this PR was auto-generated by tooling. Before moving it out of draft, the author has done a full review of the PR, specifically evaluating it on the following criteria:
Full details of the methodology of generation and review can be found here - https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing
If you have concerns with the PR or with the underlying methodology used to generate this, please comment below - they will be addressed by the author not the tooling :)
Summary
Pins the
contextClassLoaderof lazily createdUnboundedScheduledExecutorServiceworker threads to theClassLoadercaptured whenUnboundedScheduledExecutorServiceis constructed, preventing embeddedFnHarnessworker threads from inheriting a stale, closed FlinkSafetyNetWrapperClassLoaderfrom gRPC's shared executor pool.Failing Run
PreCommit Java Flink Versions—:runners:flink:1.20:testGradle Test Executor 5):PortableExecutionTest.testExecution[streaming: true](AssertionError: expected:<DONE> but was:<FAILED>)PortableStateExecutionTest.testExecution[streaming: false](TestTimedOutExceptionafter 120s)java.lang.IllegalStateException: Trying to access closed classloaderatFlinkUserCodeClassLoaders$SafetyNetWrapperClassLoader.ensureInner(FlinkUserCodeClassLoaders.java:189)→SerializableUtils$ContextualObjectInputStream.resolveClass(SerializableUtils.java:191)onpool-71-thread-5andpool-93-thread-5..9.Root Cause
FnHarness.main()ran onCreateEnvironment-thread, whosecontextClassLoaderwas the current Flink job's liveSafetyNetWrapperClassLoader(confirmed byJvmInitializers.beforeProcessingsucceeding viaServiceLoaderonCreateEnvironment-threadimmediately before entering the instruction loop).FnHarness.main()constructsnew UnboundedScheduledExecutorService()(FnHarness.java:285), which uses aThreadPoolExecutorwithcorePoolSize = 0andMoreExecutors.platformThreadFactory()without pinningcontextClassLoader.BeamFnControlClient$InboundObserver.onNext()later dispatchesInstructionRequests on gRPC's static JVM-widegrpc-default-executor-Nthreads (which can still hold a closedSafetyNetWrapperClassLoaderinherited from an earlier Flink job in the same JVM/TaskManager),ThreadPoolExecutorlazily creates newFnHarnessworker threads inheriting that closedSafetyNetWrapperClassLoader.SerializableUtils$ContextualObjectInputStream.resolveClass()then callsReflectHelpers.findClassLoader()→Class.forName(...)on the closedSafetyNetWrapperClassLoaderand throwsIllegalStateException.Changes
sdks/java/core/src/main/java/org/apache/beam/sdk/util/UnboundedScheduledExecutorService.java: CapturesThread.currentThread().getContextClassLoader()at construction time and sets it on every thread created by the wrappedThreadFactory.sdks/java/core/src/test/java/org/apache/beam/sdk/util/UnboundedScheduledExecutorServiceTest.java: AddedtestWorkerThreadsInheritCreatorContextClassLoaderverifying that lazily created worker threads inherit the constructing thread'scontextClassLoadereven when submitted from a thread with a differentcontextClassLoader.Verification
google-java-formatstyle (local host has JRE 21 / JDK 27 without JDK 17/21javac, so full Gradle compilation, Spotless, Checker Framework,:sdks:java:core:test, andbeam_PreCommit_Java_Flink_Versions.ymlare verified in GitHub Actions).Verification
.github/workflows/beam_PreCommit_Java_Flink_Versions.ymlworkflow run on branch:46795176165