Skip to content

[sentry:7847994825] A download superseded by a second download() with the same task id kept running after its body ended and published the shared staging file. The abort signal was consult - #113

Merged
Ooooze merged 3 commits into
mainfrom
codex/sentry-fix/7847994825
Nov 18, 2026
Merged

Ooooze merged 3 commits into
mainfrom
codex/sentry-fix/7847994825

Conversation

@Ooooze

@Ooooze Ooooze commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

A download superseded by a second download() with the same task id kept running after its body ended and published the shared staging file. The abort signal was consulted only inside the chunk branch of downloadOne's read loop, so an attempt whose body had already ended — or that was blocked in reader.read() when the abort arrived and then saw done — reached the publish block: it renamed <save>.tmp to <save> and deleted <save>.url, then failed its own post-transfer verification with Validation cancelled. The verification-failure branch of Downloader.run then removed that published file and its directory, so the live attempt's own post-transfer stat(tmpPath) threw ENOENT and the download failed as [disk_device_lost] ENOENT — exactly the reported event (component/operation downloads/download, crash frames downloadOne line 783 / diskFailure line 240).

Repair: one guard in downloadOne immediately after the transfer loop closes and before the persisted-size check, if (signal.aborted) throw new Error(DOWNLOAD_CANCELLED). A superseded or cancelled attempt now stands back and leaves the .tmp partial to the newcomer (or to the existing downloaded === expectedSize resume branch). No message, tag, telemetry, filter or public contract changes; aborted attempts remain unreported because download() only calls onFailure when the task's own signal is not aborted. Task.superseded stays a bookkeeping flag and is deliberately not used for the guard, since cancellation must behave identically.

Verification: src/downloads/downloader-supersede-race.test.ts failed on the base at the named assertion (got Validation cancelled where Download cancelled is required) and passes with the patch, asserting the newcomer's bytes are published, its staging file and .url sidecar are consumed, and no ENOENT occurs. One English ADR added and linked from the decision index.

Evidence:
Event 6767e23901504679bc38f0c107d83522 (atomic-chat-core@0.23.4, dist c21d779): component=downloads, operation=download, failure_reason=disk_device_lost, cause_code=ENOENT, exceptions [ENOENT "Underlying failure", Error "downloads download: disk_device_lost"], warning, failure_category operational.
Stack frames: src/downloads/downloader.ts:240 diskFailure, :784, :783 downloadOne — line 783 is the post-transfer await stat(tmpPath).catch(...) and 784 its throw disk(e).
Base behaviour of Downloader.download sets existing.superseded = true and aborts the old controller; downloadOne consulted signal.aborted only after a chunk arrived and in sleeps, never between the end of the body and the publish block (rename(tmpPath, savePath) + rm(urlPath)).
Downloader.run's verification-failure branch removes the file (rm(savePath, { force: true })) and its directory (rmdir(dirname(savePath))), which converts a superseded attempt's late publish into the live attempt's ENOENT at its own stat(tmpPath).
download() reports only when !task.controller.signal.aborted, so the superseded attempt is silent and the live attempt is the one that reaches Sentry — matching the single reported event shape.
Test run on the unmodified base: src/downloads/downloader-supersede-race.test.ts failed with "expected [Function] to throw error including 'Download cancelled' but got 'Validation cancelled'" at the named assertion.
Same test after the patch: 1 passed, 0 failed (whole file green).
The release diff for atomic-chat-core@0.23.4#c21d7796d3b2 contains no hunk in src/downloads/downloader.ts, so release and base share this code.
docs/decisions/2026-11-15-a-superseded-download-does-not-publish.md added and one bullet appended to docs/decisions/INDEX.md.

Regression tests: src/downloads/downloader-supersede-race.test.ts

Full CI and human review are required.

Report: http://127.0.0.1:8080/tenants/707d0855-80ab-4e1f-a156-f1c4546cbf52/runs/18dea3c1-a2b8-436e-8f99-bfbec540d412

Sentry: https://atomic-chat.sentry.io/issues/7847994825/

@Ooooze
Ooooze merged commit 5b67227 into main Nov 18, 2026
29 of 30 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.

1 participant