Skip to content

ci: run stdlib tests in parallel and build rpf9dump first to drop duplicate runs - #3879

Open
Jolymmiles wants to merge 4 commits into
ItsNotRexed:mainfrom
Jolymmiles:t3/ci-parallel-stdlib-tests
Open

Jolymmiles wants to merge 4 commits into
ItsNotRexed:mainfrom
Jolymmiles:t3/ci-parallel-stdlib-tests

Conversation

@Jolymmiles

Copy link
Copy Markdown
Contributor

Claim: #3796 (comment)

What changed and why

The check job in .github/workflows/ci.yml (windows-latest) had two speed and duplication issues:

  1. Duplicate rpf9dump-gated test runs: Tests (standard library only, no game data) ran before Build rpf9dump, so tests/test_rpf9dump_exit_codes.py and tests/test_ttpf_export_resume.py both ran once inside the stdlib loop (skipping because RPF9DUMP_DLL was unset) and then ran again in dedicated post-build steps.
  2. Strictly sequential bash/grep loop on Windows: ~250–299 stdlib test files ran one by one in a Git Bash while loop that also spawned two MSYS2 grep subprocesses per file (PYTEST_FILE_MARKERS and __main__), spending several minutes on sequential execution and process-spawn overhead on windows-latest (and stopping on the first failure via || exit 1).

Changes:

  • .github/workflows/ci.yml:
    • Move Build rpf9dump before Tests (standard library only, no game data).
    • Pass RPF9DUMP_DLL: tools/rpf9dump/bin/Release/net8.0/rpf9dump.dll to Tests (standard library only, no game data) and invoke python -I scripts/run_stdlib_tests.py directly (no shell: bash, no find/grep spawns).
    • Remove the now-redundant Test rpf9dump exit codes and Test TTPF export retry steps. Build the calibrated BCPack reader and test the texture pipeline stays unchanged because it requires numpy and the native Rust bcpack_calibration build.
  • scripts/run_stdlib_tests.py (new, standard library only):
    • Discovers the exact same tests/test_*.py subset (tests/semantic pruned, excluding PYTEST_FILE_MARKERS, requiring __main__, sorted deterministically).
    • Runs each file in its own isolated python -I subprocess across a ThreadPoolExecutor (--jobs, default os.cpu_count() or 1), capturing stdout+stderr as bytes (utf-8, errors="replace") and flushing each file's output atomically on completion without interleaving.
    • Runs every file past earlier failures, emits sorted ::error file=<path>::<path> failed (exit <code>) (or timeout) annotations, and exits 1 on any failure or if 0 files are discovered.
    • Sets sys.stdout.reconfigure(errors="replace") in __main__ (passes tests/test_tool_stdout_encoding.py and tests/test_text_write_line_endings.py).
  • tests/test_ci_registration.py:
    • Adds StdlibRunnerRegistrationTest verifying that scripts/run_stdlib_tests.py defaults and discover_tests() match ci_rules(ROOT) (keeping check and translator-tests in sync), that Build rpf9dump precedes the stdlib step with RPF9DUMP_DLL set, and that test_rpf9dump_exit_codes.py / test_ttpf_export_resume.py are not invoked again in duplicate steps.
  • tests/test_run_stdlib_tests.py (new):
    • 6 synthetic unit tests covering discovery parity, running all files past failures + ::error reporting, real concurrent execution (2-process rendezvous under -j 2), per-file timeout handling, zero-file refusal, and RPF9DUMP_DLL / PYTEST_FILE_MARKERS propagation under python -I.

Overlap / conflict with open PRs

Tested (CPU only, no GPU, no game data)

Real GitHub Actions runs on windows-latest (fork Jolymmiles/Leonida)

  1. Before round-42 rebase (on f847748c):

    • jactionlint: run 45075081703 — success.
    • CI: run 45075084572 — Build rpf9dump succeeded in 17s; Tests (standard library only, no game data) ran 250 stdlib test files in 214.78s (3m 35s) with 4 workers (cumulative unittest Ran ... time alone was 691.6s / 11m 32s, bottlenecked by tests/test_d16_format_probe.py at 189.49s): 249 passed, 1 failed — ran past the pre-existing main failure tests/test_report_paths.py and emitted only ::error file=tests/test_report_paths.py::....
    • Verified in the check log: tests/test_rpf9dump_exit_codes.py (Ran 1 test in 2.025s OK), tests/test_ttpf_export_resume.py (Ran 3 tests in 2.272s OK), tests/test_ci_registration.py (7/7 OK), and tests/test_run_stdlib_tests.py (6/6 OK) all executed for real (not skipped) with zero parallel conflicts.
    • For reference, on upstream main (45044865312), the old sequential bash loop took 3m 19s just to stop early at test_report_paths.py before reaching ~70 remaining files.
  2. Rebased onto latest main (4c52ce27, head d60e5094):

    • jactionlint: run 45075834725 — success.
    • CI: run 45075837210 — Build rpf9dump succeeded in 15s; Tests (standard library only, no game data) ran 299 stdlib test files in 233.88s (3m 54s) with 4 workers: 297 passed, 2 failed — tests/test_report_paths.py now passes (15/15 OK), tests/test_rpf9dump_exit_codes.py (Ran 2 tests in 2.734s OK) and tests/test_ttpf_export_resume.py (Ran 3 tests in 1.837s OK) ran once for real, tests/test_ci_registration.py (10/10 OK), tests/test_run_stdlib_tests.py (6/6 OK), translator-tests and viewer-tests passed. The only 2 failures are pre-existing round-42 Windows issues on main (tests/test_check_no_game_data.py read-only .git cleanup in TemporaryDirectory and tests/test_saves.py:362 Windows \\?\ symlink prefix), both surfaced in one run instead of the first hiding the second.

Local checks (macOS arm64, Python 3, CPU only, no game data)

  • python3 -m compileall -q make_game.py tools scripts tests — OK
  • python3 -I make_game.py --help — OK
  • python3 -I tests/test_ci_registration.py — 10/10 OK
  • python3 -I tests/test_run_stdlib_tests.py — 6/6 OK
  • python3 -I tests/test_tool_stdout_encoding.py — 4/4 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • actionlint .github/workflows/ci.yml — 0 findings
  • Full run python3 -I scripts/run_stdlib_tests.py (pre-rebase 250 files, 14 workers): 250/250 passed in 49.90s (vs 313.81s cumulative sequential time, 6.1x speedup).

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No translator rule change (docs/design.md contract table n/a).

…licate runs

In the check job on windows-latest, move Build rpf9dump ahead of the standard-library test step, pass RPF9DUMP_DLL there so test_rpf9dump_exit_codes.py and test_ttpf_export_resume.py run once for real instead of skipping and rerunning, and replace the sequential bash while/grep loop with scripts/run_stdlib_tests.py (pure stdlib, parallel python -I subprocesses, runs every file past failures and emits ::error annotations).
@evilclanker9000

Copy link
Copy Markdown
Contributor

Second check on Windows at d60e509. The six runner tests and ten CI registration tests passed, and --list selected 299 files with both rpf9dump-dependent tests listed once. I did not run the full CI suite.

I found one Windows output regression in run_one(). A scratch child launched through the same python -I invocation reported stdout.encoding as cp1252 and printed the word ending in U+00E9. Its stdout had byte E9, but the runner's unconditional UTF-8 decode turned it into U+FFFD. The test still exited 0, so this silently damages redirected diagnostics. The runner needs an encoding contract that matches its child, with a Windows test covering non-ASCII captured output.

AI-assisted.

A piped python -I child uses the platform locale encoding (Windows ANSI code page such as cp1252, UTF-8 on POSIX) because -I ignores PYTHONUTF8 and PYTHONIOENCODING. Decode captured bytes with locale.getencoding() instead of unconditional UTF-8, normalize CRLF to LF to avoid double newlines on Windows text stdout, and cover non-ASCII child output in tests/test_run_stdlib_tests.py.
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Note

🤖 Gemini 4 Neon responding on behalf of Jolymmiles

Fixed in 965c5a39: scripts/run_stdlib_tests.py now decodes captured child output with child_output_encoding() (locale.getencoding(), falling back to locale.getpreferredencoding(False)) instead of unconditional UTF-8, matching the ANSI code page a piped python -I child uses on Windows (while staying UTF-8 on POSIX), and normalizes \r\n to \n so Windows text-mode stdout does not double-space lines.

Added two tests in tests/test_run_stdlib_tests.py:

  • test_captured_non_ascii_output_matches_child_encoding: launches a real python -I child through run_one() printing a non-ASCII string encodable in child_output_encoding() (café on cp1252/UTF-8) and asserts the decoded output matches with no \ufffd and no \r\n.
  • test_decode_bytes_uses_explicit_child_encoding_and_normalizes_crlf: verifies b"caf\xe9\r\n" decodes to "café\n" under cp1252.

Locally verified on macOS (test_run_stdlib_tests.py 8/8, test_ci_registration.py 10/10, test_tool_stdout_encoding.py 4/4, test_text_write_line_endings.py 3/3); awaiting the Windows CI run on 965c5a39.

@tupacfan04

Copy link
Copy Markdown
Contributor

Checked by reproducing both selections locally on Windows 11 / Python 3.13.0: the old bash find/grep loop and discover_tests() select the same 299 files in the same order with no duplicates (scripts/run_stdlib_tests.py:66-96). The DLL-gated tests now really run, because Build rpf9dump (ci.yml:77) precedes the env-bearing stdlib step (:83-86), so tests/test_rpf9dump_exit_codes.py:65, test_ttpf_export_resume.py:32 and test_ttpf_assets.py:105 see the DLL. Nested files are included, tests/semantic is pruned, pytest-marker files are excluded. Failures stay fatal: a probe gave ::error file=... failed (exit 3) and exit 1 while still running every file.

Nits:

  • tests/test_ttpf_assets.py:105's DLL test now runs in the stdlib step and again at ci.yml:90-93, a new duplicate (the PR text only mentions two files).
  • scripts/run_stdlib_tests.py:30 allows 600 s per file against a 15-minute job (ci.yml:60), so one hang eats 10 minutes.
  • :170-189 has no finally: shutdown() for an unexpected worker exception.
  • The default -j=cpu_count (:221) can flake tests that have internal timeouts under load.

Merge-ready. AI-assisted review, CI only.

@jeremygold02 jeremygold02 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.

The per-file timeout in run_one() can leave a helper running on Windows. If the helper inherits the output pipe, the runner also waits beyond the configured timeout before it reports the failure.

I ran the public CLI at 965c5a39 with one worker, a 0.6-second timeout and one synthetic test file. Each helper slept for three seconds and then wrote a marker. All three runs correctly returned exit code 1.

Test file behavior Runner wall time Marker at runner exit
Sleep directly 0.67 s No helper
Start a helper with inherited stdout/stderr and wait 3.14 s Present
Start the same helper with stdout/stderr redirected to DEVNULL and wait 0.69 s Absent at exit, present afterward

To reproduce the middle row, put the following in a temporary tests/test_timeout.py and run python -I scripts/run_stdlib_tests.py --root <temp> --jobs 1 --timeout 0.6.

import subprocess, sys
if __name__ == "__main__":
    subprocess.run([sys.executable, "-I", "-c", "import time; time.sleep(3)"])

Could the Windows timeout path stop the test's process tree and bound the wait for captured output? A test with a helper that writes a late marker would cover both behaviors. All 18 existing runner and CI registration tests passed.

Windows 11 Home 25H2 build 26200.9974, Python 3.12.10, Intel Core Ultra 7 265K. CPU-only. NVIDIA GeForce RTX 5070 (621.01) installed. Synthetic inputs. AI-assisted.

…er pool

On timeout in run_one(), terminate the whole process tree (taskkill /F /T /PID while the parent PID is still alive on Windows; start_new_session + os.killpg on POSIX) and bound post-kill communicate()/wait() so a grandchild holding stdout/stderr cannot hang the runner or outlive the timeout. Also ensure run_suite() shuts down ThreadPoolExecutor in a finally block on any exception, and add tests covering both inherited-pipe and DEVNULL helper processes.
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Note

🤖 Gemini 4 Neon responding on behalf of Jolymmiles

Fixed both the Windows timeout process-tree leak (@jeremygold02) and the worker-pool shutdown path (@tupacfan04) in daa15f72:

  • Process-tree termination & bounded pipe drain (scripts/run_stdlib_tests.py:run_one): switched run_one() to subprocess.Popen (CREATE_NEW_PROCESS_GROUP on Windows, start_new_session=True on POSIX). On timeout, _terminate_process_tree() runs taskkill /F /T /PID <pid> while the parent PID is still alive on Windows (and os.killpg(proc.pid, signal.SIGKILL) on POSIX) before proc.kill(), then drains captured output with a bounded 5s communicate() fallback that closes proc.stdout and reaps the child if a detached handle remains open.
  • Coverage (tests/test_run_stdlib_tests.py): added test_timeout_kills_helper_process_tree_and_does_not_wait_for_inherited_pipe, covering both inherited stdout/stderr and DEVNULL helpers that sleep 3.5s and write a late marker. Under a 1.5s timeout, the test verifies the helper started, the runner returned in <3.2s with timed_out=True / exit 124, and the late marker is still absent after 4.0s.
  • Executor cleanup (scripts/run_stdlib_tests.py:run_suite): moved executor.shutdown(wait=not interrupted, cancel_futures=interrupted) into finally so any unexpected exception also tears down the pool.
  • Other nits from @tupacfan04:
    • tests/test_ttpf_assets.py:105: agreed that its RPF9DUMP_DLL subtest now executes in both the stdlib step and the calibrated texture step; the stdlib step stays numpy-free while the later step exercises the native bcpack_calibration path, so leaving test_ttpf_assets.py in both avoids special-casing a subtest skip.
    • DEFAULT_TIMEOUT = 600 and default --jobs = os.cpu_count(): on windows-latest (45075084572), tests/test_d16_format_probe.py took 189.49s, so 600s gives ~3x headroom without risking false timeouts while still reporting hangs inside the 15-minute job; 4 workers ran 250 and 299 files on windows-latest with zero parallel flakes.

Verified locally on macOS (test_run_stdlib_tests.py 9/9, test_ci_registration.py 10/10, test_tool_stdout_encoding.py 4/4, test_text_write_line_endings.py 3/3); awaiting Windows CI on daa15f72.

@jeremygold02

Copy link
Copy Markdown
Contributor

Rechecked daa15f72 with the same Windows CLI and 0.6-second timeout. With the test process waiting for its helper, both original cases now pass. The runner returned in 1.00 s with inherited output and 0.94 s with redirected output. Both helpers started, and neither wrote its late marker.

One case still exceeds the five-second drain in run_one(). The test file can start a helper with inherited stdout and then exit normally before the timeout. I changed the fixture to use subprocess.Popen([...]) without .wait(). The helper wrote a start marker, slept seven seconds and wrote a completion marker. The runner reported the 0.6-second timeout after 7.17 s, with the completion marker already on disk. The test process has already exited by the time taskkill /T runs in this case.

This matters when a short-lived test or wrapper leaves a subprocess holding its output pipe. Could the runner retain ownership of the helper's lifetime even after the parent exits? The original two regression cases and the additional early-exit case make separate controls.

19 runner and CI registration tests passed, plus 2 subtests. Windows 11 Home 25H2 build 26200.9974, Python 3.12.10, Intel Core Ultra 7 265K. CPU-only, NVIDIA GeForce RTX 5070 (621.01) installed. AI-assisted by Codex.

… cannot orphan helpers

On Windows, assign each python -I child in run_one() to a per-run Win32 Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE (falling back to taskkill /F /T /PID if unavailable), and call TerminateJobObject() on timeout so descendants holding stdout/stderr are killed immediately even if the test process itself already exited. Add an early_exit_inherited subtest alongside the inherited and devnull controls in tests/test_run_stdlib_tests.py.
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Note

🤖 Gemini 4 Neon responding on behalf of Jolymmiles

Fixed in 60b83f3d: on Windows, run_one() now assigns each python -I child to a per-run Win32 Job Object (_Win32Job, CreateJobObjectW + JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE + AssignProcessToJobObject, falling back to taskkill /F /T /PID only if job assignment is unavailable).

Because descendants inherit membership in the job object even after the test process itself exits, _terminate_process_tree() calls TerminateJobObject() on timeout (and finally closes the handle), killing orphaned helpers that still hold the inherited stdout/stderr pipe instead of waiting for them to exit.

In tests/test_run_stdlib_tests.py, test_timeout_kills_helper_process_tree_and_does_not_wait_for_inherited_pipe now runs all three controls separately: inherited, devnull, and early_exit_inherited (subprocess.Popen(...) with inherited stdout/stderr and immediate parent exit).

Verified locally on macOS (test_run_stdlib_tests.py 9/9 including all three subtests, test_ci_registration.py 10/10, test_tool_stdout_encoding.py 4/4, test_text_write_line_endings.py 3/3); Windows CI run on 60b83f3d is in progress at https://github.com/Jolymmiles/Leonida/actions/runs/45078085304.

@jeremygold02 jeremygold02 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.

Verified the requested timeout fix at 60b83f3d with the Windows CLI. The early-exit case that took 7.17 s on daa15f72 returned in 0.67 s at the same 0.6 s timeout. The helper started, the test process exited, and the helper's completion marker was still absent after its seven-second sleep would have finished.

The waiting-parent controls also passed. The inherited-output helper returned in 0.67 s and the redirected helper in 0.69 s. Neither wrote a late marker. The single-process control returned in 0.70 s, and all four commands reported timeout failures as expected. These checks exercised normal local Windows Job Object assignment. Hosts that prevent assignment use a separate fallback.

All 26 focused tests in the runner, CI registration, output encoding and line-ending suites passed, plus 5 subtests. Windows 11 Home 25H2 build 26200.9974, Python 3.12.10, Intel Core Ultra 7 265K. CPU-only. NVIDIA GeForce RTX 5070 (621.01) installed. Synthetic files. AI-assisted by Codex.

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.

4 participants