Skip to content

B1: Write safety and finalisation #89

Closed

Description

@Claptar

Part of #87.

wave 1 · 0.17.4 · size L · starts after: B0 (#88) (both edit cli.py)

Goal. No command can lose the user's data and still exit 0. The AGENTS.md write rules hold for every writer, not only most of them.

Owns: storage/__init__.py (Store.close only), util/staging.py, util/path.py, commands/{export,import_data,create,subset,recast,split}.py, cli.py (shared options), core/subset.py (subset_h5ad orchestration only), tests/test_write_safety.py

Approach:

  • Store.close re-raises unless an exception is already propagating, for both the HDF5 close and the Zarr consolidation (F003, F070). That one change is what makes staging trustworthy.
  • Add one output-check helper that every writer calls before anything is opened:
    • same_path, plus os.path.samefile when the output exists, so case-only aliases are caught (F082);
    • a store output that names an existing non-store directory is refused (F037, F074);
    • --inplace together with -o is refused (F095);
    • a symlinked store passed to --inplace is resolved, so the link itself is kept (F073).
  • Route export, import and create through staged_outputs and that helper (F023, F024, F074).
  • An empty selection is refused before staging, never written (F016).
  • Put a shared --chunk option with min=1 on every command (F015, which also covers concat and recast).
  • Staging robustness:
    • an exclusive lock on the output's .adata-tmp name, so two concurrent runs cannot both proceed (F083);
    • SIGTERM turned into the existing BaseException cleanup (F145);
    • fsync before the swap, on --inplace only (F137, plausible).
  • A full disk aborts the HDF5 library itself (F022), which cannot be fixed here. Mitigate it with a free-space preflight against the estimated output size and a troubleshooting note.
  • Tests:
    • every class in F093;
    • subset and split keep a Zarr v2 source at v2 (F088).

Findings

Ref Sev Finding Where
F003 C Store.close swallows HDF5 flush/close errors, so staging promotes an unreadable store with exit 0, and --inplace deletes the source src/adata/storage/__init__.py:36
F015 H A zero or negative --chunk silently writes all-zero dense X and layers, or crashes with a range() error src/adata/core/subset.py:451
F016 H A name file that matches nothing writes an empty store, and with --inplace replaces the source with it src/adata/core/subset.py:1055
F022 H A full disk during an HDF5 write often kills the process by signal (SIGABRT inside H5Ocopy, SIGSEGV/SIGBUS after the Error line), skipping … src/adata/storage/__init__.py:864
F023 H export -o naming the input store truncates and destroys it (no same_path check); a refused export also clobbers an existing output src/adata/commands/export.py:25
F024 H import subcommands write stores without staging: --inplace failures destroy source elements, -o overwrites an existing output before … src/adata/commands/import_data.py:41
F037 H subset/recast -o naming an existing ordinary directory silently deletes it (rmtree) and replaces it with an HDF5 file src/adata/util/staging.py:79
F070 M Zarr consolidate errors are swallowed: an import into a v2 store keeps the stale .zmetadata, so anndata cannot see the imported element, … src/adata/storage/__init__.py:55
F073 M --inplace replaces a symlinked store's link with a private regular file (the real store is unchanged) and broadens permissions (640 → 644) src/adata/util/staging.py:80
F074 M create --force rmtree's any existing directory before writing, and is not staged (a failed write leaves a broken store where the old one … src/adata/commands/create.py:82
F082 M same_path misses case-insensitive and hard-link aliases, so subset/recast/split silently replace the input src/adata/util/path.py:17
F083 M Concurrent runs writing the same output race past the leftover check: Zarr outputs exit 0 with cell barcodes lost or unreadable src/adata/util/staging.py:62
F088 M No test catches subset or split upgrading a Zarr v2 source to v3; the split 'defaults to the source version' test cannot fail tests/test_commands_phase2.py:783
F093 M Whole classes of input are untested: write safety for export/import/create, --chunk <= 0, repeated names, empty name lists tests/test_write_safety.py:1
F095 L --inplace combined with -o silently ignores -o and modifies the source (for import, unstaged) src/adata/cli.py:362
F137 L No fsync before the swap: --inplace deletes the original while the new store may still be only in the page cache (plausible) src/adata/util/staging.py:100
F145 L Scheduler SIGTERM bypasses staging cleanup, and split then makes the user clear one leftover per rerun src/adata/util/staging.py:62

Done when one PR on fix/b1 closes this, with a regression test against an oracle for every finding, and docs and changelog in the same PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions