Skip to content

loop: drain on request (SIGUSR1) - #703

Merged
rusholme merged 2 commits into
mainfrom
loop-drain/1-drain
Dec 5, 2026
Merged

rusholme merged 2 commits into
mainfrom
loop-drain/1-drain

Conversation

@rusholme

@rusholme rusholme commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

What and why

A running rapidpipe loop can now be asked to take nothing more in and finish what it has, by SIGUSR1. Runs 298 and 306 were stopped by stopping the loop's unit: the differences in flight ran on and published, and the 999 of run 306 got no packet job because the loop that would have submitted them was gone. A drain instead carries every difference already submitted through its packet job.

The contract, each line with a test in tests/unit/test_loop.py:

  • No signal, no change. Every existing test passes unchanged; test_a_pass_never_asked_to_drain_returns_what_it_returns_without_the_question compares the JSON of a pass with and without the question, byte for byte.
  • The cut is at the submission. run_pass asks draining() before each submission and each rerun. test_a_drain_asked_between_two_submissions_starts_no_later_one_but_the_packets: the request arrives after the first intake; the second intake and the reference never start, and the packet still goes. A pass asked before it plans submits its packets under the cap, and the other stages are not counted against it (test_a_draining_pass_submits_the_packets_whatever_the_other_stages_would_take_of_the_cap). From the request on, every pass carries "draining": true and left by stage: intake the stems not taken in, psf and reference the jobs planned and withheld, difference those withheld plus the deferred ones (test_a_draining_pass_counts_the_deferred_differences_as_left_and_submits_none). A pass before the request carries neither key.
  • Packets and reruns. A difference that publishes several passes after the request gets its packet job (test_a_loop_sent_sigusr1_carries_a_difference_publishing_passes_later_to_its_packet_and_exits, five passes through the real CLI and a real os.kill). A dead difference or packet job is still rerun. A dead psf, intake or reference job is not rerun and is reported failed (test_a_draining_pass_reruns_a_dead_difference_or_packet_and_leaves_a_dead_intake_failed).
  • The exit. A draining pass that finds nothing pending, no packet to submit and none deferred is drained. The loop then exits 2 (ExitCode.INCOMPLETE), or 1 if a job of the run failed or was refused, as now. Its last stderr line is rapidpipe loop: run N drained on request; left unsubmitted: A stems not taken in, B psf, C reference and D difference jobs. Code 2 is the existing "not yet terminal" code, which the loop did not use until now; job wait and reprocess use it for a timeout. It says that the selection is not finished and that the loop, started again, takes it up. No new code is added to ExitCode; its docstring now names the loop's use.
  • Signals. The handler is installed only when the loop repeats passes (--local included). --once, --stage and --dry-run install none, so they are unchanged. SIGTERM and SIGINT are never touched. A second SIGUSR1 sets the same flag again (test_once_stage_and_dry_run_take_no_drain_and_no_loop_touches_sigterm_or_sigint; the CLI test sends the signal twice). --local is covered by test_a_local_loop_sent_sigusr1_mid_job_runs_the_passs_packet_and_nothing_else.
  • Help. The loop parser's description, cli/main.py's module text and foundation/loop.py's module text each describe the drain in one paragraph. None of them says any longer that a pass produces packet sets to the topic, which has been untrue since publish: a set's messages are keyed by the set, the send waits half an hour, and the loop does not sweep #680 (test_the_loops_help_describes_the_drain_and_no_send).

Files

  • rapidpipe/foundation/loop.py: run_pass(..., draining=), the module's drain paragraph, and the "ends by producing" sentence removed.
  • rapidpipe/cli/main.py: the SIGUSR1 handler (a threading.Event), the exit and stderr line, the parser description and the module text.
  • rapidpipe/exitcodes.py: INCOMPLETE's docstring names the loop's drain.
  • README.md, AGENTS.md: the stale "ends each pass with publish" / "ending at the topic" corrected, and the drain named.
  • tests/unit/test_loop.py: nine tests (twelve cases) and the docstring's contracts.

Divergences

  • The card asked for "a code the loop does not use today, chosen from the repo's exit-code conventions". I chose 2 (INCOMPLETE) over adding a new value: exitcodes.py asks for the rapid_docs tool page to change first for a new value, and 2 already means "not yet terminal, come back". The tool page's table and loop.md's table carry it (loop: the drain on request (SIGUSR1); no page says the loop sends rapid_docs#259).
  • The design-authority pages are corrected in the rapid_docs pull request, which is merged before this one.

Measurements

before after
tests/unit/test_loop.py 110 passed 122 passed (2.9 s)
tests/unit on rapid-rusholme 1,475 passed, 2 failed: test_measured_with_a_set_falls_back_to_it_on_any_measurement_error (both cases), OSError: [Errno 16] Device or resource busy on an .nfs* file under the EFS TMPDIR. This is the host's shared filesystem, not this change; CI is the gate.

A mutation check: with the handler's install removed, the CLI test's os.kill ends the pytest process (SIGUSR1's default action), so the test cannot pass without the handler.

The baseline's loop findings (kepler-baseline, run 310)

Read from that card's RESULT, ledger, loop-310.log (118 passes), guard-310.log and counts-310.log. None is a loop defect, and none was fixed here:

  • --dates 2026-09-28..2026-10-27 planned exactly the window: 4,045 differences, every row dated 10-03 to 10-27, as in run 298. The loop drained and exited 1 on its 10 coverage-floor references and 211 bkgest differences. This bears on the exit: a drain with failures exits 1, as in run 310.
  • The guard never stopped run 310. Its stop is systemctl --user stop of the loop unit, the stop that stranded run 306's 999 packets. A drain is the stop such a guard should send, with --kill-whom=main.
  • Differences were deferred at 1,000 in flight from 01:12 to 01:49 UTC (up to 2,689 deferred). Under a drain, those are never submitted and are counted under left.difference.
  • No rerun in any pass (rerun empty in all 118), and no Batch retry held.
  • Pass seconds: about 100 s on the intake passes (500 submissions each), and 20 to 70 s afterwards. Submission is about 0.2 s a job, so a request arriving mid-pass stops the pass within one submission.
  • Each summary line carries every job id it submitted, every failed job's scope and the 642 held fields: 17 MB of log for 118 passes. This is noted, not changed: the contract keeps a summary byte for byte.

Improvements seen

  • The sleep between passes is not cut short by the signal (PEP 475 resumes it), so a drain requested during the sleep starts at the next pass, at most --interval later. That is harmless, and nothing was built for it.

Session notes

  • tests/unit on this host's EFS TMPDIR fails test_measured_with_a_set_falls_back_… on a busy .nfs* file, twice in two runs.

What the next task must know

  • Task 2 (drain-proof) builds this merged commit. The unit must exec the loop so that --kill-whom=main reaches it. The exit is 2 with no failed job, and the last stderr line has the shape above.

Left

  • Nothing.

🤖 Generated with Claude Code

rusholme and others added 2 commits December 5, 2026 04:05
A running loop sent SIGUSR1 submits no further psf, intake, reference or
difference job (the cut at each submission, the rest of a pass under way
included), keeps submitting the packet job of every difference that
publishes, reruns a dead difference or packet job and no other, and exits
2 (1 with a failed or refused job) once nothing of the run is pending and
no packet job is left to submit, its last stderr line the drain and the
counts left. Every pass from the signal reports "draining": true and
"left" by stage; a pass never asked is unchanged. --once, --stage and
--dry-run take no drain; SIGTERM and SIGINT are untouched.

The loop's help, the module texts, README and AGENTS no longer say that a
pass produces packet sets to the topic (untrue since #680).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rusholme
rusholme merged commit 84d1f46 into main Dec 5, 2026
5 checks passed
@rusholme
rusholme deleted the loop-drain/1-drain branch December 5, 2026 04:08
rusholme added a commit that referenced this pull request Dec 5, 2026
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.

1 participant