Repository navigation
Conversation
|
This comment has been minimized.
This comment has been minimized.
|
The finding was right for the commit it reviewed, and I reproduced it: with
The deterministic synchronization this asks for is in this PR already: I did not add synchronization to this loop test as well. A strengthened version exists on my integration line (multi-thread flavor, a |
|
Friendly ping: the review finding is addressed at the current head (see my reply above), the branch is still mergeable against |
|
@greptile review |
`BackgroundTaskManager::status` reads `<task-id>.status.json` and treats an unreadable file as "task missing", so `wait` returns `None` for a task that is still running. Tasks rewrite that same file on progress and completion with a plain truncating write, so a reader can observe an empty file in the window between truncate and write. Reproduced at the library level: one manager rewrites the status file 400 times while another reads it 400 times and asserts the task is still tracked. Before this change it failed 8 of 8 runs. As a suite flake it made `background::tests::adopted_output_is_readable_while_running_and_preserves_final_result` fail 3 of 15 full `jcode-base --lib` runs at the default thread count, and a temporary diagnostic in `read_status_file` printed exactly one `parse len=0 ... EOF while parsing a value` per failing run. Publish updates through a uniquely named sibling plus a rename, so a reader sees either the previous complete file or the new complete one. The spawn, adopt, completion, cancel, and reconciliation writes all route through the helper. A failed publish can leave one uniquely named temp file behind; the reconcile sweep and `bg cleanup` both key off the `.json` extension, so such a file is inert. Rename-over-existing works on Windows because Rust opens files with `FILE_SHARE_DELETE`. Verified: `background::tests` 21 passed with the change, including the new regression; the same regression fails in 0.01s against the truncating write (A/B control run in the same worktree).
…odules `background.rs` is ratcheted at 1673 LOC and the atomic status write plus the graceful-cancel path pushed it to 1885. Move both into sibling modules with no behavior change: - `background/status_file.rs` owns `write_status_file_atomic` and `publish_status_file`. - `background/cancel_grace.rs` owns the TERM-to-KILL grace logic; `BackgroundTaskManager::cancel_with_grace` now delegates to it. Verified with `cargo test -p jcode-base --lib -- background` (30 tests) and `scripts/check_guardrails.sh --skip-slow` (code-size ratchet passes).
ee9c3d8 to
e708daf
Compare
Rework of the P1 raised on #1357 (closes #1350): the initial `Running` document was published through an awaited write *before* the task was registered. The caller is an agent turn that a server reload can abort at any await, so an abort landing inside that write left a `Running` document that no live task would ever finish, losing the adopted or spawned work's output and completion. Publish the initial document synchronously, on the caller's thread, and before the wrapper that owns the task handle is spawned, so no await separates the visible document from its owner: - `status_file::publish_initial_status` writes atomically, never awaits, and skips the manager-wide gate: the task id is freshly generated, so no other writer can reach that path yet, and the initial document carries no progress history. Every later update still runs off-worker behind the gate. - `spawn_with_notify`, `adopt_with_options`, and `register_detached_task` use it; the now test-only `write_status_file` wrapper is marked `#[cfg(test)]`. Tests in `crates/jcode-base/src/background/status_write_tests.rs`: - `initial_publication_ignores_a_held_status_gate` holds the gate and asserts the document is still visible. - `spawning_background_work_publishes_its_status_document_before_yielding` and `adopting_a_running_tool_publishes_its_status_document_before_yielding` poll the call site once and assert the document already exists. - `registering_a_detached_task_publishes_its_status_document_before_yielding` registers under a held gate, asserting `Poll::Ready` plus the `detached` and `pid` fields. Verified: `cargo test -p jcode-base --lib -- background` (34 tests) and `scripts/check_guardrails.sh`, whose only failure is the pre-existing local warning-budget gate from a broken toolchain `rust-objcopy`.
Greptile's P1 (comment 4243186337) measured the abort as "the caller stops while the `Running` document is already visible". Two tests now pin that window for both registration paths, and both fail against the awaited publication they replace. `aborting_the_adopting_caller_still_records_the_adopted_work` and `aborting_the_spawning_caller_still_runs_the_spawned_work` hold the status gate (another task's slow write) and the live map, then drop the caller future once it is parked. The caller is only ever parked after the document exists and after the wrapper that owns the work exists, so the adopted output, the spawned output, and the terminal `Completed` document still land even though the aborted caller never returned a task id. Under the awaited publication both tests fail at the first assertion: the caller parks inside the write, no document is published, and the work is never registered. The existing held-gate tests still own the discriminating assertion for the detached path, where registration is synchronous.
The status index already replaces status files atomically. Register spawn and adopt workers before awaiting publication so a disconnected caller cannot leave an unowned Running status. Upstream: 1jehuang#1357 Co-authored-by: Iraklis Analitis <[email protected]>
Upstream-PR: 1jehuang#1357 Upstream-PR-Commit: 1jehuang#1357 0da5e81 Upstream-PR: 1jehuang#1362 Upstream-PR-Commit: 1jehuang#1362 5962058 Upstream-PR: 1jehuang#1486 Upstream-PR-Commit: 1jehuang#1486 fadc624 Upstream-PR: 1jehuang#1733 Upstream-PR-Commit: 1jehuang#1733 01f3415 Upstream-PR: 1jehuang#1750 Upstream-PR-Commit: 1jehuang#1750 1ac0c82 Upstream-PR: 1jehuang#1822 Upstream-PR-Commit: 1jehuang#1822 73f85fc Upstream-PR: 1jehuang#1912 Upstream-PR-Commit: 1jehuang#1912 36d7682 Upstream-PR: 1jehuang#1978 Upstream-PR-Commit: 1jehuang#1978 0f55643 Upstream-PR: 1jehuang#2007 Upstream-PR-Commit: 1jehuang#2007 638f87c Upstream-PR: 1jehuang#2213 Upstream-PR-Commit: 1jehuang#2213 c5e1487 Upstream-PR: 1jehuang#2234 Upstream-PR-Commit: 1jehuang#2234 0d4d5d4 Upstream-PR: 1jehuang#2266 Upstream-PR-Commit: 1jehuang#2266 217ddc2 Upstream-PR: 1jehuang#2267 Upstream-PR-Commit: 1jehuang#2267 e98c40b
Closes #1350.
Problem
BackgroundTaskManager::statusreads<task-id>.status.jsonand treats an unreadable file as "task missing", sowaitreturnsNonewhile the task is still running. Tasks rewrite that same file on progress and completion with a plain truncating write, so a reader can observe an empty file in the window between truncate and write.Reproduced at the library level: one manager rewrites the status file 400 times while another reads it 400 times and asserts the task is still tracked. Before this change it failed 8 of 8 runs.
As a suite flake it made
background::tests::adopted_output_is_readable_while_running_and_preserves_final_resultfail 3 of 15 fulljcode-base --libruns at the default thread count, and a temporary diagnostic inread_status_fileprinted exactly one failure per failing run:Change
Publish status updates through a uniquely named sibling plus a rename, so a reader sees either the previous complete file or the new complete one:
The spawn, adopt, completion, cancel, and reconciliation writes all route through it.
Edge cases: a failed publish can leave one uniquely named temp file behind, and both the reconcile sweep and
bg cleanupkey off the.jsonextension, so such a file is inert. Rename-over-existing works on Windows because Rust opens files withFILE_SHARE_DELETE.Rework: interrupted initial publication
The first revision awaited the initial publication (acquire the
status_updatesgate, then aspawn_blockingrename) beforetokio::spawncreated the wrapper that owns the supplied handle or closure. Dropping the caller inside that await does not cancel the blocking write, so aRunningdocument could land with no live task to finish it, and an adopted handle's output and completion were never recorded. That is the gap reported as a P1 review finding on this PR.The initial publication is now synchronous (
status_file::publish_initial_status) on the caller's thread, before the wrapper is spawned, so no.awaitcan sit between the document becoming visible and registration. It does not take the status gate, because it must not join another task's slow write; that is safe while the task is unregistered and no other writer exists for its path. Progress and terminal writes keep the gate and the atomic rename.Two tests in
background::status_write_testscover the window:aborting_the_adopting_caller_still_records_the_adopted_workaborting_the_spawning_caller_still_runs_the_spawned_workEach holds the status gate (standing in for a slow concurrent write) plus the live map, asserts the caller parks with the document already visible and the work already owned, then drops the caller future and requires the adopted output, the spawned output, and the terminal
Completeddocument. Restoring the awaited publication makes both fail at the first assertion (no status document was published).Test
background::tests::status_reads_never_observe_a_partially_written_status_fileruns the concurrent read/write loop.background::status_write_tests::aborting_*cover the interrupted-publication windows.Verification
(The warning-budget gate is the one exception on my local host, where
rust-objcopyin toolchain 1.98.1 aborts on a missinglibLLVM.dylib; it reports no warning from this change.)A/B control in the same worktree: reverting only
background.rstoorigin/masterwhile keeping the new test makes it fail in 0.01s (0 passed; 1 failed), and restoring the helper makesbackground::testspass 21 of 21.