Skip to content

Disable SO_REUSEPORT on Python portability gRPC servers - #41629

Draft
damccorm wants to merge 1 commit into
masterfrom
fix/python-grpc-disable-so-reuseport
Draft

damccorm wants to merge 1 commit into
masterfrom
fix/python-grpc-disable-so-reuseport

Conversation

@damccorm

Copy link
Copy Markdown
Contributor

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

Passes ("grpc.so_reuseport", 0) to ephemeral-port gRPC servers created by the Python portability runner (GrpcServer, LocalJobServicer, SubprocessSdkWorker, and UberJarBeamJob) so distinct servers cannot share a port on Linux.

Production Impact

This changes production gRPC server setup in the Python portability / FnApiRunner / local job service stack (worker_handlers.py, local_job_service.py, abstract_job_service.py). Previously, on Linux, gRPC's default grpc.so_reuseport=1 allowed multiple independent ephemeral-port servers (control, data, state, logging, job service, or artifact staging) running under the same user—whether in the same process or across concurrent jobs/workers on the same host—to collide on the same port and silently misroute RPCs to the wrong servicer with StatusCode.UNIMPLEMENTED: Method not found!. Explicitly setting ("grpc.so_reuseport", 0) guarantees exclusive port allocation for each server without affecting single-server behavior or non-Linux platforms.

Root Cause

In PreCommit Python Runners (run 45413523150), PortableRunnerTestWithSubprocesses.test_batch_rebatch_pardos_into_multi_column_batches failed when the subprocess SDK worker's BeamFnData.Data stream to localhost:45943 terminated with:

grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
  status = StatusCode.UNIMPLEMENTED
  details = "Method not found!"
>

Because grpc.server() enables SO_REUSEPORT by default on Linux, two distinct gRPC servers binding ephemeral ports ([::]:0 / localhost:0) under the same UID (such as parallel pytest -n 6 workers or the four separate servers inside GrpcServer) can receive the same port from the kernel. Incoming connections are then hashed across both listeners, routing a BeamFnData RPC to a server that only implements another service (e.g., control, state, logging, or job service).

Fix

  • Add ("grpc.so_reuseport", 0) to the gRPC server options in:
    • sdks/python/apache_beam/runners/portability/fn_api_runner/worker_handlers.py (GrpcServer.__init__)
    • sdks/python/apache_beam/runners/portability/local_job_service.py (LocalJobServicer.start_grpc_server and SubprocessSdkWorker.run)
    • sdks/python/apache_beam/runners/portability/abstract_job_service.py (UberJarBeamJob._start_artifact_service)
  • Add unit tests verifying that a second grpc.server cannot bind any port owned by GrpcServer (WorkerHandlerManagerTest.test_grpc_server_disables_so_reuseport) or LocalJobServicer (LocalJobServerTest.test_grpc_server_disables_so_reuseport).

Verification

  • Verified formatting with yapf --style setup.cfg --diff across all 5 modified files (clean).
  • Verified locally with pytest:
    • worker_handlers_test.py + local_job_service_test.py (6 passed, including both new SO_REUSEPORT tests).
    • flink_uber_jar_job_server_test.py (6 passed).
    • portable_runner_test.py -k "PortableRunnerTestWithSubprocesses and test_batch_rebatch_pardos_into_multi_column_batches" (2 passed).

Recent Occurrences

1 failing master workflow run in the past 3 weeks:

Verification

  • Triggered beam_PreCommit_Python_Runners.yml workflow run on branch: 45437029119

@damccorm damccorm added the test-failure-cleanup Automated test health and flake cleanup PRs label Nov 24, 2026
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 58.05%. Comparing base (32ebd5e) to head (9cbf127).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...ache_beam/runners/portability/local_job_service.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41629   +/-   ##
=========================================
  Coverage     58.05%   58.05%           
  Complexity     2844     2844           
=========================================
  Files          1210     1210           
  Lines        173280   173280           
  Branches       2329     2329           
=========================================
+ Hits         100604   100605    +1     
+ Misses        69964    69963    -1     
  Partials       2712     2712           
Flag Coverage Δ
python 80.67% <66.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added python runners size/s S: 10-49 lines changed labels Nov 24, 2026
@damccorm damccorm added the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review runners size/s S: 10-49 lines changed test-failure-cleanup Automated test health and flake cleanup PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant