Skip to content

Clean up finished tox workdirs in postCommitPyDep Gradle tasks - #41778

Draft
damccorm wants to merge 5 commits into
masterfrom
fix/py311-tft-121-xdist-workers-46199424647
Draft

damccorm wants to merge 5 commits into
masterfrom
fix/py311-tft-121-xdist-workers-46199424647

Conversation

@damccorm

@damccorm damccorm commented Nov 30, 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

Removes each recreated target/.tox-$TOX_ENVIRONMENT workdir after tox finishes when CLEANUP_TOX_ENV=1, and configures cleanupToxEnv = true at the Gradle toxTask level for all postCommitPyDep dependency tasks in sdks/python/test-suites/tox/py311/build.gradle and sdks/python/test-suites/tox/py314/build.gradle so sequential ML dependency virtualenvs do not accumulate and exhaust pod ephemeral storage (regardless of which workflow or suite runs those tasks).

Recent Occurrences

  • 46632187833 (2026-12-01T11:00:21Z): beam_PostCommit_Python_Dependency (3.11) lost communication during testPy311tft-121 on main.
  • 46468330036 (2026-11-30T21:00:20Z): beam_PostCommit_Python_Dependency (3.11) lost communication during testPy311tft-121 on main.
  • 46199424647 (2026-11-29T00:34:24Z): beam_PostCommit_Python_Dependency (3.11) lost communication during testPy311tft-121 on main.
  • 45908263057 (2026-11-27T01:03:28Z): beam_PostCommit_Python_Dependency (3.11) lost communication during testPy311tft-121 on main.
  • 45660051807 & 45443630129: same pod eviction during testPy311tft-121.
  • Earlier iterations of this PR ruled out both PYTEST_XDIST_WORKERS=2 (46452596108 still evicted in testPy311tft-121) and switching to highmem runners (46504976877 evicted twice in a row at ~57m including the 40m heartbeat timeout on both attempt 1 and attempt 2).

Root Cause

sdks/python/scripts/run_tox.sh isolates each tox environment under target/.tox-$TOX_ENVIRONMENT and always runs tox run --recreate, so finished workdirs are never reused (and pytest_*.xml reports are written to sdks/python/pytest_*.xml outside target/.tox-*). However, nothing deletes finished target/.tox-* directories between Gradle tasks. During beam_PostCommit_Python_Dependency (3.11), 26 dependency environments accumulate sequentially on the same pod (including PyTorch, TensorFlow, TFT 1.21, Transformers, ONNX, XGBoost, Embeddings, TFHubEmbeddings, Pandas, and 10 PyArrow versions), eventually triggering kubelet ephemeral-storage pod eviction.

Fix

  • In sdks/python/scripts/run_tox.sh, delete target/${ENV_NAME} after tox exits (preserving $exit_code) when CLEANUP_TOX_ENV=1 is set.
  • In buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy, add optional cleanupToxEnv support (task.ext.cleanupToxEnv, defaulting to false) to project.ext.toxTask, setting CLEANUP_TOX_ENV=1 on the project.exec environment when enabled (while respecting an explicit CLEANUP_TOX_ENV environment override such as CLEANUP_TOX_ENV=0 for local debugging).
  • In sdks/python/test-suites/tox/py311/build.gradle and sdks/python/test-suites/tox/py314/build.gradle, configure it.ext.cleanupToxEnv = true on every postCommitPyDep dependency task so any workflow or suite invoking those tasks gets automatic cleanup without workflow-level env configuration.

Verification

Build/CI/test-script-only change; no production SDK code is modified.

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

Copy link
Copy Markdown
Contributor Author

/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 makes the number of pytest-xdist workers configurable via PYTEST_XDIST_WORKERS (defaulting to 6) in sdks/python/scripts/run_pytest.sh and reduces the worker count to 2 for the py311-tft-121 tox environment to mitigate OOM evictions. The implementation is clean, minimal, and backward-compatible across existing test environments.

🔍 General Feedback

  • Inheriting {[testenv]setenv} in [testenv:py311-tft-121] properly preserves global test environment variables while scoping the worker reduction specifically to the memory-intensive TFT test suite.
  • Using ${PYTEST_XDIST_WORKERS:-6} in run_pytest.sh maintains the existing default concurrency for all other test suites without affecting the sequential no_xdist test execution pass.

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

@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Nov 30, 2026
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.91%. Comparing base (ac07580) to head (706a0b0).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41778   +/-   ##
=========================================
  Coverage     58.91%   58.91%           
  Complexity     2346     2346           
=========================================
  Files          1136     1136           
  Lines        171138   171144    +6     
  Branches       1567     1567           
=========================================
+ Hits         100819   100824    +5     
- Misses        67762    67763    +1     
  Partials       2557     2557           
Flag Coverage Δ
python 80.77% <ø> (+<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 changed the title Limit pytest-xdist workers in py311-tft-121 tox env to avoid runner OOM Run PostCommit Python Dependency on highmem runners Nov 30, 2026
@damccorm

Copy link
Copy Markdown
Contributor Author

/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 switches beam_PostCommit_Python_Dependency from main to highmem self-hosted runners to prevent OOM/pod evictions when running the heavy ML dependency tox suites (py311-tft-121, PyTorch, TensorFlow, etc.). The change is minimal, well-justified by recent CI failure evidence, and aligns with existing memory-intensive Python workflows in the repository.

🔍 General Feedback

  • The single-line workflow configuration update (runs-on: [self-hosted, ubuntu-24.04, highmem]) matches the established pattern in beam_PreCommit_Python_Coverage.yml and other resource-heavy Python workflows.
  • No issues found; this looks ready to merge once the verification run completes.

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

@damccorm damccorm changed the title Run PostCommit Python Dependency on highmem runners Clean up finished tox workdirs in PostCommit Python Dependency Nov 30, 2026
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@github-actions github-actions Bot added build size/xs XS: <10 lines changed and removed size/s S: 10-49 lines changed labels Nov 30, 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 adds an opt-in CLEANUP_TOX_ENV=1 mechanism to sdks/python/scripts/run_tox.sh to delete individual target/.tox-$TOX_ENVIRONMENT working directories after tox finishes, and enables it in beam_PostCommit_Python_Dependency.yml. This cleanly resolves the ephemeral-storage pod evictions caused by accumulating 23 heavy ML virtualenvs sequentially during the dependency postcommit suite.

🔍 General Feedback

  • The cleanup in run_tox.sh properly preserves the tox exit code and guards rm -rf with a non-empty ENV_NAME check.
  • Because pytest_*.xml JUnit reports are written outside target/.tox-* and tox is invoked with --recreate, removing the finished environment directory is safe and does not affect test result reporting or local developer workflows.

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

@damccorm damccorm added the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Dec 1, 2026
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.BEAM_DEVELOCITY_ACCESS_KEY }}
HF_INFERENCE_TOKEN: ${{ secrets.HF_INFERENCE_TOKEN }}
CLEANUP_TOX_ENV: "1"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this should be set by tasks that run into this problem, not at the workflow level. Otherwise if another suite adopts the same task, it will run into the same problem

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in af36f38: removed CLEANUP_TOX_ENV: "1" from .github/workflows/beam_PostCommit_Python_Dependency.yml, added optional task-level cleanupToxEnv support to project.ext.toxTask in BeamModulePlugin.groovy (respecting an explicit CLEANUP_TOX_ENV override if set), and enabled it.ext.cleanupToxEnv = true on all postCommitPyDep dependency tasks in sdks/python/test-suites/tox/py311/build.gradle and sdks/python/test-suites/tox/py314/build.gradle so any suite adopting those tasks gets cleanup automatically. Re-triggered beam_PostCommit_Python_Dependency.yml for verification.

@damccorm damccorm removed the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Dec 2, 2026
@damccorm

damccorm commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

/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 prevents pod ephemeral storage exhaustion in PostCommit Python Dependency suites by adding an opt-in CLEANUP_TOX_ENV mechanism to run_tox.sh and BeamModulePlugin.groovy, and enabling it for all postCommitPyDep tasks in py311 and py314. The implementation is clean, preserves exit codes and pytest JUnit XML artifacts, and encapsulates the cleanup configuration at the Gradle task level.

🔍 General Feedback

  • Moving the cleanupToxEnv configuration from the GitHub Actions workflow level to the Gradle task definitions in BeamModulePlugin.groovy and build.gradle ensures any suite or invocation running these tasks benefits from the cleanup while still allowing explicit CLEANUP_TOX_ENV environment overrides.
  • Please note that the PR description under Summary and Fix still mentions setting CLEANUP_TOX_ENV: "1" in .github/workflows/beam_PostCommit_Python_Dependency.yml from an earlier iteration; consider updating the PR description to match the current Gradle-based approach.

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

@github-actions github-actions Bot added size/s S: 10-49 lines changed and removed size/xs XS: <10 lines changed labels Dec 2, 2026
@damccorm damccorm changed the title Clean up finished tox workdirs in PostCommit Python Dependency Clean up finished tox workdirs in postCommitPyDep Gradle tasks Dec 2, 2026
@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

build python ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review 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