Skip to content

[SPARK-58932][SS] Close the accepted socket in TransformWithStateInPySparkStateServer - #58205

Open
j1wonpark wants to merge 1 commit into
apache:masterfrom
j1wonpark:tws-state-server-socket-leak
Open

[SPARK-58932][SS] Close the accepted socket in TransformWithStateInPySparkStateServer#58205
j1wonpark wants to merge 1 commit into
apache:masterfrom
j1wonpark:tws-state-server-socket-leak

Conversation

@j1wonpark

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Run the request loop of TransformWithStateInPySparkStateServer.run() inside Utils.tryWithResource so the socket returned by stateServerSocket.accept() is always closed.

Why are the changes needed?

run() accepts a connection from the Python worker but never closes it. The task completion listener closes only the listening ServerSocketChannel, so the accepted connection's file descriptor is held until the executor exits.

initStateServer() binds an ephemeral port per task, so a long-running streaming query leaks one socket per task. Once the ephemeral port range is exhausted, every new connection on that executor fails with java.net.BindException: Cannot assign requested address, which also takes down unrelated connections such as the Kafka source.

The loop has several early returns, so the close has to be tied to the scope.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Two tests added to TransformWithStateInPySparkStateServerSuite, covering the path where the request loop is never entered and the EOF early-return path. Both fail without the fix. The suite previously had no test calling run().

Was this patch authored or co-authored using generative AI tooling?

Yes. Generated-by: Claude Opus 5

…SparkStateServer

### What changes were proposed in this pull request?

Run the request loop of `TransformWithStateInPySparkStateServer.run()` inside
`Utils.tryWithResource` so the socket returned by `stateServerSocket.accept()`
is always closed.

### Why are the changes needed?

`run()` accepts a connection from the Python worker but never closes it. The
task completion listener closes only the listening `ServerSocketChannel`, so
the accepted connection's file descriptor is held until the executor exits.

`initStateServer()` binds an ephemeral port per task, so a long-running
streaming query leaks one socket per task. Once the ephemeral port range is
exhausted, every new connection on that executor fails with
`java.net.BindException: Cannot assign requested address`, which also takes
down unrelated connections such as the Kafka source.

The loop has several early returns, so the close has to be tied to the scope.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Two tests added to `TransformWithStateInPySparkStateServerSuite`, covering the
path where the request loop is never entered and the EOF early-return path.
Both fail without the fix. The suite previously had no test calling `run()`.

### Was this patch authored or co-authored using generative AI tooling?

Yes. Generated-by: Claude Opus 5

Signed-off-by: Jiwon Park <jpark92@outlook.kr>
@j1wonpark
j1wonpark force-pushed the tws-state-server-socket-leak branch from 2334021 to 9f7e7ba Compare August 21, 2026 14:05
@uros-b

uros-b commented Aug 21, 2026

Copy link
Copy Markdown
Member

Thank you @j1wonpark!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants