damccorm opened a new pull request, #41871:
URL: https://github.com/apache/beam/pull/41871

   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:
   
   - The PR is an appropriate fix for the issue (e.g. we're not changing a test 
to avoid a bug)
   - The PR is a simple fix to the issue (e.g. we're not adding infra when we 
could just modify a test)
   - The PR is not covering for other problems (e.g. if we're bumping timeouts, 
it is not because we're covering for a performance problem)
     
   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 `contextClassLoader` of lazily created 
`UnboundedScheduledExecutorService` worker threads to the `ClassLoader` 
captured when `UnboundedScheduledExecutorService` is constructed, preventing 
embedded `FnHarness` worker threads from inheriting a stale, closed Flink 
`SafetyNetWrapperClassLoader` from gRPC's shared executor pool.
   
   ## Failing Run
   - **Workflow**: `PreCommit Java Flink Versions` — `:runners:flink:1.20:test`
   - **Run**: https://github.com/apache/beam/actions/runs/46790017727 (job 
https://github.com/apache/beam/actions/runs/46790017727/job/143149761241, build 
scan https://develocity.apache.org/s/hmedxz5ybvwwk)
   - **Failing Tests** (both on `Gradle Test Executor 5`):
     - `PortableExecutionTest.testExecution[streaming: true]` (`AssertionError: 
expected:<DONE> but was:<FAILED>`)
     - `PortableStateExecutionTest.testExecution[streaming: false]` 
(`TestTimedOutException` after 120s)
   - **Error**: `java.lang.IllegalStateException: Trying to access closed 
classloader` at 
`FlinkUserCodeClassLoaders$SafetyNetWrapperClassLoader.ensureInner(FlinkUserCodeClassLoaders.java:189)`
 → 
`SerializableUtils$ContextualObjectInputStream.resolveClass(SerializableUtils.java:191)`
 on `pool-71-thread-5` and `pool-93-thread-5..9`.
   
   ## Root Cause
   1. In both tests, `FnHarness.main()` ran on `CreateEnvironment-thread`, 
whose `contextClassLoader` was the current Flink job's live 
`SafetyNetWrapperClassLoader` (confirmed by `JvmInitializers.beforeProcessing` 
succeeding via `ServiceLoader` on `CreateEnvironment-thread` immediately before 
entering the instruction loop).
   2. `FnHarness.main()` constructs `new UnboundedScheduledExecutorService()` 
(`FnHarness.java:285`), which uses a `ThreadPoolExecutor` with `corePoolSize = 
0` and `MoreExecutors.platformThreadFactory()` without pinning 
`contextClassLoader`.
   3. When `BeamFnControlClient$InboundObserver.onNext()` later dispatches 
`InstructionRequest`s on gRPC's static JVM-wide `grpc-default-executor-N` 
threads (which can still hold a closed `SafetyNetWrapperClassLoader` inherited 
from an earlier Flink job in the same JVM/TaskManager), `ThreadPoolExecutor` 
lazily creates new `FnHarness` worker threads inheriting that **closed** 
`SafetyNetWrapperClassLoader`.
   4. `SerializableUtils$ContextualObjectInputStream.resolveClass()` then calls 
`ReflectHelpers.findClassLoader()` → `Class.forName(...)` on the closed 
`SafetyNetWrapperClassLoader` and throws `IllegalStateException`.
   
   ## Changes
   - 
`sdks/java/core/src/main/java/org/apache/beam/sdk/util/UnboundedScheduledExecutorService.java`:
 Captures `Thread.currentThread().getContextClassLoader()` at construction time 
and sets it on every thread created by the wrapped `ThreadFactory`.
   - 
`sdks/java/core/src/test/java/org/apache/beam/sdk/util/UnboundedScheduledExecutorServiceTest.java`:
 Added `testWorkerThreadsInheritCreatorContextClassLoader` verifying that 
lazily created worker threads inherit the constructing thread's 
`contextClassLoader` even when submitted from a thread with a different 
`contextClassLoader`.
   
   ## Verification
   - Verified formatting conventions match repository `google-java-format` 
style (local host has JRE 21 / JDK 27 without JDK 17/21 `javac`, so full Gradle 
compilation, Spotless, Checker Framework, `:sdks:java:core:test`, and 
`beam_PreCommit_Java_Flink_Versions.yml` are verified in GitHub Actions).
   
   ### Verification
   - Triggered `.github/workflows/beam_PreCommit_Java_Flink_Versions.yml` 
workflow run on branch: 
[`46795176165`](https://github.com/apache/beam/actions/runs/46795176165)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to