Skip to content

fix: clean up prefetch workers on early consumer exit - #308

Merged
ryoari merged 2 commits into
AOSSIE-Org:mainfrom
panshularora:fix/prefetch-cancellation
Nov 14, 2026
Merged

ryoari merged 2 commits into
AOSSIE-Org:mainfrom
panshularora:fix/prefetch-cancellation

Conversation

@panshularora

Copy link
Copy Markdown
Contributor

Summary

Closes #305.

  • Use a cancellation event and timeout-aware bounded-queue writes for batches, loader errors and the terminal sentinel.
  • Close the underlying loader in the producer thread's finally; retain the worker and join for at most one second when the consumer exits.
  • Explicitly close the prefetched iterator in run_phases, cmd_bench and bench_sizes, including STOP, errors and timed breaks.
  • Capture benchmark elapsed time after GPU synchronization but before producer cleanup, so cancellation latency is not counted as training throughput.
  • Document iterator ownership and the blocking-I/O limitation. No new dependency.

Verification

  • Regression-first: full-queue close, retained STOP/callback-error streams, terminal delivery and both benchmark early-break sites fail on unchanged upstream; a further fake-clock regression catches cleanup contaminating benchmark timing.
  • 90 CPU tests passed, including 20 new parametrized cases across fast/reference loaders.
  • ruff check simple tests and git diff --check: passed.
  • Re-ran the original real-loader reproduction at batch 32/context 512 on synthetic 70-batch memmaps: three consecutive early closes leave 0, 0, 0 live prefetch workers above baseline for both fast and reference loaders (originally 1, 2, 3).
  • Tests compare every tensor and resume position, preserve the first-step rolling batch hash, propagate loader errors, and explicitly retain iterator references to ensure cleanup does not rely on CPython GC.
  • AST comparison against the pinned base confirms RECIPE and all other ovl.py functions (including both loaders, step, digests, checkpoint and verifier functions) are unchanged.
  • Bandit on changed production files: no findings; this is a limited static scan, not a security certification.

Limits / review focus

Cancellation is cooperative: a thread inside arbitrary blocking I/O cannot be killed safely and can outlive the one-second join until I/O returns. No full RTX 5090 segment replay, CUDA bit-equivalence, GPU memory reduction or throughput speedup is claimed. Benchmark tests use a simulated clock and mocked CUDA accounting on CPU, not performance measurements. Existing short-stream benchmark arithmetic issue #223 is deliberately unchanged.

Base: f42508c. No changes to release recipe, RNG, optimizer arithmetic, batch construction/order, digests, chain or checkpoints. This branch contains no other pending fixes.

@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 57 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4d0f8f1 and 9f24238.

Files selected for processing: 2

  • simple/ovl.py
  • tests/simple/test_prefetch.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@panshularora
panshularora marked this pull request as ready for review November 11, 2026 15:20

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

Thanks. The fix in prefetch() (a stop event, put with a timeout, the loader closed in the worker, a bounded join) is right, and the tests for it are good.

Please keep this PR to that change. run_phases, cmd_bench and bench_sizes.py don't need the closing(...) wrappers. When a for loop is left by return, break or an exception, CPython drops its iterator at once, and the generator's finally runs then. The project only runs on CPython. Those wrappers re-indent the training loop, which is the published training path, and that makes the diff much harder to check, for no change in behaviour. The benchmark timing change only follows from the wrappers.

So please drop the closing(...) changes, the bench_sizes.py and cmd_bench changes and the README paragraph. Keep the prefetch() change and the tests that call prefetch() directly (close with a full queue, loader error, terminal delivery, exhaustion). Then mark it ready for review and I'll merge it.

@panshularora
panshularora force-pushed the fix/prefetch-cancellation branch from 8e230ad to 200dc7f Compare November 14, 2026 10:43

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

Thanks, this is now only the prefetch() fix and its tests, which is much easier to check. Merging.

@ryoari
ryoari merged commit e099682 into AOSSIE-Org:main Nov 14, 2026
6 checks passed
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.

[BUG] prefetch leaves its producer thread blocked when a batch consumer stops early

2 participants