Skip to content

Admit Zarr recordings against scratch disk and reclaim killed workers' scratch - #1638

Merged
neuromechanist merged 23 commits into
devfrom
fix/zarr-scratch-admission
Oct 7, 2026
Merged

neuromechanist merged 23 commits into
devfrom
fix/zarr-scratch-admission

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Why

nm000276 (SWEC iEEG, 40 recordings of 8.6 to 177 GiB, 3031 GiB in all; the 28 still pending are 22 to 177 GiB) sits at 12 of 40 converted.
The cause is scratch disk exhaustion on the conversion node, not memory:

  • /mnt/local reached 100% (608 MiB free) and 27 of the 28 pending recordings report [Errno 28] No space left on device; the 28th reports its worker as killed. The same pattern repeats in the Sep 29, Oct 4, Oct 5 and Oct 6 runs.
  • A streamed recording peaks at about 2.83x its bytes on scratch (raw download, float32 memmap, int16 memmap and views at the end of pass 2), measured on sub-03: raw 114 GB, float32 memmap 114 GB, int16 memmap 57 GB, views 38 GB.
  • Admission charged RAM only, and the cron runs ZARR_JOBS=24, so two giant recordings fill the volume.
  • On Oct 6 the first worker pool died at the same second the volume filled. A killed worker never runs convert_one's finally, so its scratch stayed on disk (535 GiB across sub-02 and sub-03) and every recording queued behind it failed with ENOSPC.
  • Memory was not the cause: MemAvailable never fell below 45 GiB in any run window.

What changes

Scratch is admitted like RAM, and a deferral is neither an attempt nor a conversion.

  • Reclaim. Each recording gets its own memmap directory beside its store. A pool break reclaims the in-flight recordings' scratch, kills the dead workers' orphaned aws s3 cp children, and prints the free space before and after (a full volume kills workers with SIGBUS, which the verdict reads as out of memory; it now names a full scratch disk). The orphan scan reads /proc/<pid>/cmdline on Linux (the first version used ps, which truncates args and failed on Ubuntu), says when it could not scan or read nothing, and waits for the killed processes to exit. Deletes are measured after the fact and a failed one is an ::error::, not counted as freed; the line compares the volume's free space with what was removed. Scratch paths are checked on real paths, so a symlink cannot make a reclaim delete elsewhere.
  • Admission. A recording is charged 3x its bytes when streamed (2x otherwise) against free space plus what its in-flight recordings hold, minus 10 GiB, re-read every round. An unreadable size is charged at least 16 GiB. The gate fails closed (last good budget, or nothing, and one warning) when the volume cannot be read. With nothing in flight, admission re-samples the volume three times before deferring; each deferred recording is logged with its charge, the budget and the volume, and one that exceeds the whole volume is an ::error::. Bad ZARR_SCRATCH_* settings no longer break the import: main() refuses to run and reports the message as a failed, non-deterministic callback.
  • Index and queue. A deferred recording keeps its published store when the index is current (same commit, engine and biosigIO, and provenance that matches or cannot be checked because the catalog is down); otherwise the stale entry leaves the index and the recording is pending as not_attempted. attempts and last_attempt_utc are kept and last_error says why. A typed failure on record stays only under a current index. The callback counts deferred recordings as not attempted, so the queue's mark_done advances no retry round for them (tested through the real mark_done). The incremental path holds the commit back, and a --clean run that keeps stores carries their producer-manifest entries too.
  • An unchanged run writes nothing to S3 and still posts ready. A run that defers everything and finds the index already says so leaves the index, manifest and events.parquet alone, so a --clean run no longer republishes the dataset without the stores it could not rebuild and a recording that can never fit no longer rewrites them every hour. Its callback restates the index's numbers and the commit the index names and is posted like any other: every round starts with a converting signal that sets zarr_status to pending, and without a terminal callback a dataset serving stores would stay pending, lose its index URL and drop out of the has-Zarr filter, the MCP readiness check and the website tag. The known cost is that a ready body restamps zarr_converted_at and re-queues the recording-stats sweep every hour for a dataset that can never fit. The proper fix is an unchanged status in the webhook, a backend change recorded in the docs PR as a follow-up.
  • One settings check per run. generate_zarr.py --check-env validates the ZARR_SCRATCH_* settings (exit 1 with the message) and hallu-zarr.sh runs it once before any queue call or dataset, stopping the whole run with the queue and D1 untouched. Checked per dataset instead, a typo would post failed and burn a queue attempt for every dataset the drain visited.
  • The orphan scan is bounded and re-checked. It runs under a 30 second deadline, re-reads each candidate's command line immediately before the kill (a pid reused in between is left alone), waits for the killed processes and reports survivors. A published manifest that is absent or unreadable when kept stores need its entries is now a warning naming the cause.

The design note is in the follow-up docs PR (#1641, draft until its ADR number is taken).

Behavior to know about

  • The 3.0 factor is the float32 measurement plus margin, so an int16 source could still hit ENOSPC. This narrows the risk that scratch was never admission-controlled; it does not remove it.
  • A recording that never fits is still re-queued hourly (a metadata clone and the start-of-run signal, but no publish). A longer delay for capacity deferrals, and a scratch_deferred field plus an "unchanged" status in the webhook and D1, cross components and are written up in the docs PR as follow-ups.
  • On nm000276, sub-02 is charged about 532 GiB against roughly 525 GiB usable on the current volume, so it stays deferred until more space is free or ZARR_SCRATCH_STREAM_FACTOR=2.9 is set.
  • Merging to the branch the conversion node tracks deploys this on hallu within one cron tick.

Testing

  • Full tier (converter stack and pyedflib installed, Python 3.12, pytest): 830 passed, 5 skipped.
  • Fast tier (no converter stack): 676 passed, 159 skipped. The main()-level deferral tests only run in the full tier, which CI's patch-duration-test provides.
  • test_zarr_queue.py (146) and test_hallu_zarr_config.py (134) pass; shellcheck is clean.
  • Real processes: a real SIGKILL, a real orphaned child, an undeletable directory, a scan blocked on a FIFO, a command line rewritten between scan and kill, a process that outlives the signal and one that dies slowly, and the real exporter through convert_one for the memmap location. The /proc reader is also tested against a fixture in the kernel's layout, and the ps fallback against a command line far wider than a terminal.
  • main() through the pool, serial and retry-round paths: an all-deferred --clean run, a stale store, an incremental run, an unreachable catalog, a partial deferral keeping the manifest entry, an unchanged run that posts ready and leaves every S3 object untouched, and callbacks fed through the real queue.
  • The new tests fail when their fix is disabled.
  • Not exercised on hallu itself.

A worker killed outright (SIGKILL, or SIGBUS on a memmap write to a
full volume) never runs convert_one's finally, so its raw download and
float32 memmaps stayed on scratch until the whole dataset run ended.
On nm000276 two killed workers held 535 GiB and every recording behind
them failed with ENOSPC.

Give each recording its own memmap directory next to its store (it
used to share the store's parent with sibling recordings), define a
recording's scratch paths once, and reclaim the in-flight recordings'
scratch right after a pool break, before the suspects re-run.

Tested with real processes that die without cleanup; the new drain
test fails when the reclaim is disabled.
Admission charged RAM only, so 24 workers were admitted into a volume
that two giant recordings fill: a streamed recording peaks at about
2.83x its bytes (raw, float32 memmap, int16 memmap and views at the end
of pass 2), measured on nm000276 sub-03.

Charge each recording 3x its bytes when streamed (2x otherwise)
against free space plus what the run already holds, minus 10 GiB, and
re-read it every admission round. There is no run-alone exception for
disk: a recording that cannot fit with nothing in flight is deferred,
left unreported so the index lists it as not_attempted. That spends no
attempt and no retry round, so a full disk can no longer walk healthy
recordings toward retry_exhausted. The --jobs 1 path defers the same
way.

Tested with real worker processes and, with the converter stack
installed, through main() for the pool, serial and retry-round paths.
All 719 tests in test_generate_zarr.py pass.
Closes the open risk in ADR 0030 (scratch disk is not
admission-controlled) and amends its header to point here, as ADR 0029
does for its own stale paragraph.
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1638

Version: 0.10.14-PR1638.818c803
Commit: 818c803


🤖 Updated on each push.

A factor of 0 or below silently disabled the gate, nan raised
ValueError inside main, inf overflowed int(), and a negative headroom
inflated the budget. Parse the three settings at import and refuse
anything that is not finite and at least 1 (headroom at least 0),
naming the variable.

A recording whose size could not be read (a key without -s, or no
pointer) was sized 0, so its charge was 0 and it was admitted even at
a budget of zero. Report readability alongside the size and charge
such a recording at least 16 GiB, with a warning that names them.
A failed statvfs returned None and turned the scratch gate off, which
let a whole wave in on the one occasion nothing was known about the
disk. Put the budget behind ScratchGate: a failed read keeps the last
good budget, or admits nothing when there has been none, and warns
once instead of every round.

Count what the run holds from the in-flight recordings' own paths
rather than the whole run tree, so debris a killed worker or a failed
delete left behind is not added to the budget as if it were about to
be freed.

Replace the flaky real-volume budget test with exact ones: the formula
on integers, and the gate against an injected volume probe, including
a read that fails mid-run and a budget that changes between rounds.
freed was measured before an ignore_errors delete, so a delete that
failed (probe: 4 MiB reported freed, all of it still on disk) claimed
bytes it had only tried to remove, and convert_one's finally swallowed
the same failures silently.

Collect failures with onexc, re-measure after the delete, and log an
::error:: per failed path with the bytes still on disk. A pool break
now also kills orphaned children of the dead workers (an aws s3 cp
started by a SIGKILLed worker kept writing into unlinked files), and
prints the scratch free space before and after the reclaim: a full
volume kills workers with SIGBUS, which the killed-worker verdict
reported as out of memory. The verdict now names a full scratch disk.

recording_scratch_paths refuses an absolute or ..-climbing primary,
so a reclaim can never name a directory outside the run.

Tests use a real SIGKILL for the dying worker, a real orphaned child,
an undeletable directory, and the real exporter through convert_one to
check its memmap lands in the recording's own .scratch sibling and is
removed.
A single statvfs sample could defer the whole remaining queue when
nothing was in flight, if it landed in another tenant's transient
spike. Look at the volume again SCRATCH_DEFER_RESAMPLES (3) times,
ADMISSION_RECHECK_SECONDS apart, before deferring, in both the pool
and the --jobs 1 drain (now drain_serially, first fit like the pool).

Log one line per deferred recording naming it, its charge, the budget
and the volume's free and total space, and an ::error:: for one whose
charge exceeds the volume minus headroom, which can never fit. The
deferral reaches the caller as {recording: last_error text}, so the
index can carry why. Per-recording lines stop at 200 with a count.

A memory-failed recording held for the serial retry that the retry
cannot fit on scratch is now reported as the memory failure it was,
not left to be filed as not attempted.
Deferral reached the index only by omission, which went wrong four ways.
An all-deferred --clean run republished the dataset with no stores,
since the merge is handed no prior (a previously served store dropped
out, against ADR 0005). A recording already pending kept its
infra_failure reason, so the queue read it as attempted and
mark_done spent a retry round on it. A deferred recording with a
store kept a stale entry, was not listed pending, and advanced the
commit, so it was never rebuilt. And a recording that can never fit
rewrote the index, manifest and events.parquet every hour.

merge_index now takes the deferred recordings and the published index
as a seed. A store the index may keep (same commit, engine, biosigIO
and provenance, via the new index_currency_problem shared with the
retry-round worklist) stays served; a stale one leaves the index and
the recording is pending as not_attempted, attempts and last_attempt
kept, last_error saying why ahead of what it last failed with. The
incremental path holds the commit back as it does for an infra
failure. A run that deferred everything and finds the index already
says so publishes nothing and reports the index's own numbers, with
the deferred recordings counted as not attempted.

Tested through main() and the real zarr_queue.mark_done: the
retry_round after deferrals is unchanged while an attempted failure
still advances it.
0085 to 0087 are claimed by the identifier-screening branches in
flight, so this record takes the next free number; the ADR 0030
amendment line and the decisions index follow it.

Say what the code now does: a deferral reports as not attempted so no
queue retry round advances, an all-deferred run publishes nothing and
what --clean keeps, the gate fails closed, and unknown sizes are
charged a floor. Correct the claims: the record narrows ADR 0030's
open risk rather than closing it, and nm000276 is 40 recordings of
8.6 to 177 GiB (22 to 177 GiB for the 28 pending). Record why the
allocated-bytes walk is not cached, what the orphan kill cannot see,
and the cross-component follow-up (a scratch_deferred field in the
webhook and D1, or a longer queue delay for capacity deferrals).

The ADR index test requires gapless numbers, so it fails on this
branch alone until 0085 to 0087 merge ahead of it.
The first recording fits and the second does not, with B's pointer
declaring 500 GB: the pool path and --jobs 1 each convert A, defer B
with a note and an error naming it, spend no attempt, and report one
pending and one not-attempted recording in the callback.
The ADR index test requires gapless numbers and 0085 to 0087 are
claimed by open identifier-screening PRs, so any number this record
takes fails unit-pure until they land. Take the record, the ADR 0030
amendment and the index entry out of this PR (they move to a draft
docs PR that takes its number after those merge) and drop the ADR
numbers from the comments this PR added.
The orphan scan read `ps -Ao pid=,args=`, which procps truncates to a
terminal width and which returned nothing, silently, when ps was not
there: on Ubuntu 24.04 it missed a live sleeper, and both orphan tests
failed in patch-duration-test.

Read /proc/<pid>/cmdline on Linux (the exact argv, no ps binary) and
fall back to ps -ww with a wide COLUMNS elsewhere. The scan reports an
error when it cannot run, and a pool break warns when it read no
process, instead of returning an empty list. Killed processes are
awaited and any that stay are an error. Matching is unchanged: whole
arguments under the run's own temp directory plus a separator.

The pool-break line no longer calls bytes reclaimed that are only
removed from paths: it compares the volume's free space with them and
warns when a process may still hold the deleted files open.
The containment check in recording_scratch_paths was lexical, so a
symlink inside the run's directory that pointed outside it passed and
a reclaim would delete through the link. Compare real paths, with a
test that a stores symlink to elsewhere is refused and its target left
alone, and that a run root reached through a symlink still works.
A bad ZARR_SCRATCH_* value (ZARR_SCRATCH_HEADROOM_BYTES=10G, say)
raised ValueError at import. Every dataset run and patch_duration
loads this module, so each died before it could write a callback and
nothing said why.

The import now records the message and keeps the default. main()
refuses to run, prints it as an ::error:: and writes a failed,
non-deterministic callback naming the variable, so the failure is
reported and stays retryable once the crontab is fixed. Tested with
a fresh interpreter and with main() as a subprocess.
A deferred recording's typed failure survived even when the index was
stale. A failure is a verdict on the recording as it was read, by the
engine that read it, and it is never retried, so under a stale index
(the data changed, or a newer engine may read it) keeping it published
an obsolete verdict as final.

Under a current index the failure stays. Under a stale one it is
dropped and the recording is pending as not_attempted, and a run that
deferred it no longer counts as leaving the index as is.
An unreadable catalog made the index "not current", so an all-deferred
--clean run dropped the store the index served (reproduced with an
unreachable --api-base: the index went from [A] to [] with a
store_count of 0), where a rebuild would have given A null provenance.

index_currency_problem takes provenance_unknown_ok. A store that is
merely being kept uses it: commit, engine and biosigIO are still
compared, and only the provenance comparison that cannot be made is
skipped. A retry round still treats an unreadable catalog as not
current, since converting only the pending recordings would leave every
other store's provenance unchecked.
Under --clean the manifest merge is handed no prior, so a store the
index kept because its recording was deferred lost its source_key and
size_bytes: a partial deferral served index [A, B] and wrote manifest
[B].

When a --clean run deferred anything, read the published manifest and
carry the entries of exactly the stores the index kept. Every other
path is unchanged.
A run that deferred everything and published nothing sent a ready
callback with this HEAD as its commit while the index kept an older
source_commit, so zarr_source_commit disagreed with the document it
describes. It also restamped zarr_converted_at every hour: the
observability dashboard reads a pending dataset's old
zarr_converted_at as stuck, so a deferral was hiding exactly that.

The body now carries the commit the index names. The webhook has no
"unchanged" status, so rather than change the backend the body marks
itself "post": false: hallu-zarr.sh still reads the queue's numbers
from it, and does not POST it. D1 keeps its last conversion time and
the pending status the start-of-run signal set. Anything but an
explicit false posts, so an older driver is unaffected; the filter is
tested against real jq.
Survivors of mutation testing: handing the gate no in-flight
recordings left every test green. A drain-level test now runs three
real workers against a stated free space that fits two, and the third
must be admitted once the first two have written scratch; it fails
when the gate is given an empty in-flight list. The pool path was
already passing the in-flight recordings, so there was no bug.

End-to-end tests through main() for the unreachable catalog, a partial
deferral under --clean keeping the manifest entry of the served store,
the failure callback counting deferred recordings as not attempted,
and an unchanged run reporting the index's commit and marking itself
not to be posted. Long lines wrapped.
An unchanged all-deferred run marked its callback "post": false and
hallu-zarr.sh did not POST it. But every round begins with a converting
signal that sets zarr_status to pending and clears the failure columns,
so with no terminal callback a dataset serving N stores stayed pending
forever, lost its zarr_index_url, and dropped out of the has_zarr
filter, the MCP readiness check and the website's Zarr tag. Dev ends
such a dataset ready; so does this.

The marker and the script's filter are gone. The run still skips the
index, manifest and events.parquet republish, so no S3 object is
written, and still reports the commit the index names; its ready body is
posted as usual. The known cost is that a ready body restamps
zarr_converted_at and re-queues the stats sweep every hour for a
dataset that can never fit; the proper fix is an unchanged status in
the webhook, recorded as a follow-up in the docs PR. The test now
asserts a ready body and an identical S3 snapshot.
Since validation moved into main(), a bad ZARR_SCRATCH_* value was only
caught per dataset, after the clone: the drain posted failed for every
dataset it visited and burned a queue attempt on each, for a typo that
has nothing to do with them.

generate_zarr.py --check-env validates the settings and exits 0 or 1
with the message, needing no dataset, repository or callback.
hallu-zarr.sh runs it once, after setup and before any queue call or
dataset, and stops the whole run with the message when it fails,
leaving the queue and D1 untouched; the next tick retries once the
crontab is fixed. Tested against the script with a stubbed interpreter
(the queue is never touched; one check per run) and the real
--check-env as a subprocess.
manifest_prior_for turned an absent or unreadable published manifest
into "no entries" without a word, so the kept stores silently lost
their source_key and size from the new manifest.

kept_manifest_seed names the read that failed (or says there was no
manifest) and how many kept stores are affected. It stays non-fatal:
the manifest is producer bookkeeping and the index is already correct.
Tested with a failing reader, an absent manifest and, through main(),
a run with the published manifest removed.
Between the scan and the kill a pid can be freed and reused by an
unrelated process, and the /proc scan, unlike the old ps call, had no
timeout. Each candidate's command line is now read again immediately
before the signal and must still match, and the scan runs under a
30 second deadline so one read that waits on its target cannot hang a
pool break. Both are bounded by a daemon thread: a read cannot be
cancelled, only abandoned.

Tests, with real processes: a command line rewritten between scan and
kill, a pid that vanished, a scan blocked on a FIFO where a cmdline file
should be, a process that outlives the signal (survivors and the
::error:: naming it), and a slow-dying process the wait must outlast.
The signal is injectable so a process that cannot be reaped can be
produced without one.
@neuromechanist
neuromechanist merged commit 254818a into dev Oct 7, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the fix/zarr-scratch-admission branch October 7, 2026 09:07
@neuromechanist neuromechanist mentioned this pull request Oct 7, 2026
4 tasks done
neuromechanist added a commit that referenced this pull request Oct 7, 2026
Describes the Zarr scratch-disk admission fix (#1638), the only change in the release. Tested: bun test test/changelog-format.unit.test.ts.
@neuromechanist neuromechanist mentioned this pull request Oct 7, 2026
2 tasks done

This branch was previously deployed

1 inactive deployment
npm — 818c803a Deployed Oct 7, 2026 by neuromechanist via publish #3146
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