Skip to content

[Python] Retry transient Windows PermissionError reading MultiProcessShared files - #41868

Draft
damccorm wants to merge 3 commits into
masterfrom
fix/multi-process-shared-win-address-retry-46774372052
Draft

damccorm wants to merge 3 commits into
masterfrom
fix/multi-process-shared-win-address-retry-46774372052

Conversation

@damccorm

@damccorm damccorm commented Dec 2, 2026

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

Fixes intermittent Windows PermissionError: [Errno 13] Permission denied failures when MultiProcessShared opens a .address (or .address.error) file immediately after a spawned server process publishes it with os.rename().

Failing Run

Root Cause

  1. In _run_server_process(), the spawned child (PID 9684) logged Proxy serving 954f04c862704dec82455bd64aff177a_model_status at ('127.0.0.1', 51350), wrote address_file + '.tmp', closed it, and ran os.rename(address_file + '.tmp', address_file) (multi_process_shared.py:323-325). No cleanup_files() exit occurred.
  2. In _create_server() / _get_manager(), the parent held both _process_local_lock and self._cross_process_lock on that fresh UUID tag, polled os.path.exists(address_file) (multi_process_shared.py:557), and immediately called open(address_file) (multi_process_shared.py:420).
  3. On Windows, os.path.exists() (GetFileAttributesExW) can observe the renamed entry before MoveFileW finishes closing its DELETE-access handle or while a Windows filesystem minifilter (e.g., Windows Defender real-time scan) holds a transient handle on the newly renamed file. Because CPython's open() uses CRT _wsopen_s without FILE_SHARE_DELETE, the resulting ERROR_SHARING_VIOLATION / ERROR_ACCESS_DENIED maps to EACCES (PermissionError).
  4. The same os.rename() → os.path.exists() → open() handoff applies to error_file (multi_process_shared.py:334, 561).

Changes

  • sdks/python/apache_beam/utils/multi_process_shared.py:
    • Added _is_windows(), _is_windows_permission_error(), and _read_published_file(path, with_stat=False) decorated with @retry.with_exponential_backoff(num_retries=5, initial_delay_secs=0.05, retry_filter=_is_windows_permission_error) (~1.55s expected total retry budget on Windows; zero retries on POSIX or for non-PermissionError exceptions).
    • Used _read_published_file() when reading address_file in _get_manager() and error_file in _create_server().
  • sdks/python/apache_beam/utils/multi_process_shared_test.py:
    • Added 5 unit tests covering Windows transient PermissionError recovery, POSIX non-retry, retry exhaustion, FileNotFoundError non-retry, and end-to-end MultiProcessShared.acquire() recovery.

Verification

  • Verified syntax (python3 -m py_compile) and 80-char line limit locally.
  • Executed the 4 _read_published_file unit tests (4/4 passed in 0.010s) plus an in-process MultiProcessShared.acquire() retry test (calls=3, passed) locally with minimal stubs for fasteners / pickler.
  • Python tests (including windows-latest Python 3.14) and PreCommit Python Formatter run automatically on this PR.

Verification

  • Triggered .github/workflows/python_tests.yml workflow run on branch: 46786793044

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

damccorm commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@github-actions github-actions Bot added python size/m M: 50-249 lines changed labels Dec 2, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR cleanly resolves intermittent PermissionError (EACCES) failures on Windows when reading newly renamed .address and .address.error files in MultiProcessShared by introducing a Windows-scoped exponential backoff retry helper (_read_published_file). The root cause analysis is thorough, the retry parameters are well-bounded (~1.55s max budget on Windows, zero overhead on POSIX), and the accompanying unit tests comprehensively cover retry recovery, exhaustion, platform filtering, and exception filtering.

🔍 General Feedback

  • The implementation and unit tests in multi_process_shared.py and multi_process_shared_test.py look solid and ready to merge.
  • Minor optional follow-up: in multi_process_shared_test.py (test_reused_server_address_resets_stale_proxy_connection, around line 765), addr2_file is read via raw open(addr2_file) immediately after handle2._create_server(addr2_file), which could also benefit from multi_process_shared._read_published_file(addr2_file) on Windows CI.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@damccorm

damccorm commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in commit fe2c1714508:

  • Fixed the single yapf formatting hunk in multi_process_shared_test.py (PreCommit Python Formatter).
  • Switched addr2_file reading in test_reused_server_address_resets_stale_proxy_connection to use multi_process_shared._read_published_file(addr2_file) per Gemini review.

Note: the GroupByEncryptedKeyTest failures in Python tests, PreCommit Python Coverage, and PreCommit Python Transforms are an unrelated master breakage from cryptography==51.0.0 (tracked in #41870); all 5 new multi_process_shared_test.py tests passed on windows-latest Python 3.14.

/gemini review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR adds a @retry.with_exponential_backoff-decorated _read_published_file helper to handle transient Windows PermissionError exceptions when reading .address and .address.error files immediately after os.rename(). The implementation is clean, properly scoped to win32 and PermissionError, and well-covered by unit tests.

🔍 General Feedback

  • Preserving os.fstat(fin.fileno()) inside the open context manager when with_stat=True ensures file identity checks for server_key remain atomic with reading the address.
  • All call sites reading published address/error files (including addr2_file in multi_process_shared_test.py) now consistently use _read_published_file.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 58.16%. Comparing base (bb0a14d) to head (464f338).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
...s/python/apache_beam/utils/multi_process_shared.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41868   +/-   ##
=========================================
  Coverage     58.16%   58.16%           
  Complexity     2848     2848           
=========================================
  Files          1211     1211           
  Lines        173645   173654    +9     
  Branches       2330     2330           
=========================================
+ Hits         101005   101014    +9     
  Misses        69925    69925           
  Partials       2715     2715           
Flag Coverage Δ
python 80.77% <92.85%> (+<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.

@damccorm damccorm added the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Dec 2, 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 size/m M: 50-249 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