[WIP][SS][PYTHON] Stop leaking Python workers and isolation sandboxes on TransformWithState init failure - #57941
Open
jon-gao-db wants to merge 1 commit into
Conversation
jon-gao-db
marked this pull request as ready for review
August 12, 2026 19:08
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.
What changes were proposed in this pull request?
This patch makes the driver-side
TransformWithStateInPySparkpre-initialization runner clean up on every lifecycle path. It movesinit()andprocess()intoUtils.tryWithSafeFinally, guaranteeing thatstop()runs when initialization or processing fails. It also preserves the original initialization failure when cleanup itself throws by relying ontryWithSafeFinally's suppressed-exception behavior.The runner's state-server daemon thread is nullable until the end of initialization, so
stop()now checks for null before interrupting it. A six-case regression suite covers initialization failure cleanup, repeated failures, suppressed cleanup errors, process failure wrapping, the success path, and stopping before state-server startup.Why are the changes needed?
StreamingPythonRunner.init()creates the Python worker before it finishes initialization. If initialization fails afterward, the previous code never reachedstop(), leaking the worker and its associated resources for the driver's lifetime. Repeated streaming restarts can accumulate these leaked resources and eventually prevent new isolated workers from starting.Does this PR introduce any user-facing change?
No. Successful execution behavior is unchanged. This only restores cleanup on existing failure paths and preserves the original initialization error instead of allowing cleanup failures or a null-thread error to obscure it.
How was this patch tested?
Added
TransformWithStateInPySparkPreInitCleanupSuitewith six tests and no real Python worker dependency. Static validation passed withgit diff --checkand the changed-file line-length check.The focused Spark test could not run because this checkout does not have
sbtinstalled and its launcher download was unavailable. A Maventest-compilefallback also could not resolve dependencies because the configured Maven mirrors were unreachable from the environment.Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (Codex CLI).