Skip to content

[Audit] Registry: a missing space.journal opens as an empty store, and the next reclaim erases every segment #194

Closed

Description

@saworbit

Context

Since #129 the capsule journal (space.journal) is the durable record of which capsules, buckets and keys exist, and space.db is only an index rebuilt from it. #184 made every command except create and serve-s3 refuse to open a store that isn't there (#166). Both changes landed this week.

Severity: medium. It needs space.journal to be missing, for example after a partial copy or restore, or a backup that only took space.db and space.nvram. But when that happens, the store doesn't fail. It opens as empty, and the next reclaim pass deletes every segment that was still on disk. The data could have been recovered from the index and the data log, and afterwards it can't.

Discovery Path

I was reading CapsuleRegistry::open (crates/capsule-registry/src/lib.rs) next to open_registry in crates/spacectl/src/main.rs, to see what Open::Existing protects against. open_registry accepts a store directory if either space.journal or space.db exists. CapsuleRegistry::open then calls Journal::open, which creates a new, empty journal when the file is missing.

Failure Mechanism

  1. Journal::open creates a new space.journal with a fresh store id and generation 0.
  2. The journal position stored in space.db doesn't match it, so CapsuleRegistry::open takes the Some(_) arm. It logs "the registry index doesn't match the capsule journal; rebuilding it", calls clear_index() and replays the empty journal. The index now has no capsules, buckets or keys.
  3. bind_store sees an empty journal, so instead of refusing a journal from a different store, it adopts the data log's store id (adopt_store_id). The data log and the new journal now look like a matching pair.
  4. recount_references sets every segment's reference count to 0 ("the data log holds segments no capsule references").
  5. The next spacectl compact or scheduled reclaim, including the pass serve-s3 runs at startup by default (--reclaim-interval 300), compacts space.nvram down to its FILE record.

The None if store.has_records() arm already refuses a populated space.db that has no journal position. A populated index whose journal has just been created is the same situation, but it goes to the rebuild arm instead.

Reproducible Counterexample

At 4e78771, debug build, Linux:

$ spacectl create -f in.bin        # 300 000 random bytes, run twice
$ spacectl create -f in2.bin       # 200 000 random bytes
$ spacectl list                    # 3 capsules
$ mv space.journal space.journal.bak
$ spacectl list
WARN capsule_registry: the registry index doesn't match the capsule journal; rebuilding it journal=./space.journal
(no capsules)
$ spacectl compact
space.nvram: reclaimed 500284 bytes of 500376, now generation 1
$ ls -l space.nvram
-rw-r--r-- 1 box box 92 ... space.nvram

You don't need compact. In a new store with one 6 MB capsule, after rm space.journal, just starting the server erases it:

$ ls -l space.nvram        # 6000376 bytes
$ rm space.journal
$ spacectl serve-s3 --port 18091      # stopped after 4 s
INFO nvram_sim: compacted the data log path=./space.nvram generation=1 reclaimed=6000284
INFO protocol_s3::server: reclaimed space collected=0 compacted_bytes=6000284
$ ls -l space.nvram        # 92 bytes

A storage root under SPACE_STORAGE_ROOT follows the same path, with garbage_collect deleting the segment files. I read that path but didn't run it.

Upstream Reference & Modern Standard

  • docs/on-disk-format.md in this repo says the journal is the durable record and the index can be rebuilt from it. Nothing in it says a missing journal means an empty store.
  • PostgreSQL 18, pg_resetwal (https://www.postgresql.org/docs/current/app-pgresetwal.html): when a cluster's WAL or control data is missing, the server refuses to start. Resetting it is a separate, explicit last-resort tool that needs -f, and the docs warn not to run any data-modifying operations until the data has been dumped. A store should fail closed in the same way when the file it treats as its source of truth is gone.

Proposed Resolution

  • In CapsuleRegistry::open, refuse to open when space.db has a journal position or records and Journal::open had to create the journal file. The error should say that space.journal is missing and that the store's capsules can't be trusted without it. Journal::open could report whether it created the file, or the registry could check that the file exists before opening it.
  • In bind_store, don't let an empty journal adopt the id of a data log or storage root that already holds segments while the index says it held capsules. Refuse it, or require an explicit flag.
  • Make open_registry(Open::Existing) require space.journal, not "space.journal or space.db".
  • Add a test: create a capsule, delete the journal, and assert that open fails and that space.nvram is unchanged after spacectl compact and a serve-s3 startup reclaim.
  • If a recovery path is wanted later, it can be an explicit spacectl subcommand that rebuilds a journal from the index. Opening the store should never do that silently.

Activity

  1. added
    bug
    on Nov 14, 2026
  2. saworbit commented on Nov 14, 2026

    @saworbit
    OwnerAuthor

    I'd fix this one straight after #200. It's silent data loss, and it comes from two reasonable changes meeting: the journal becoming the source of truth (#129) and the reclaim that deletes segments nothing references (#97). Refusing to reclaim, or to open at all, when space.journal is missing but segments exist would stop the damage even before the full fix.

    Related: #191 is folding the data-log and journal compaction swaps together. What a missing file means should be the same rule in both.

  3. saworbit commented on Nov 14, 2026

    @saworbit
    OwnerAuthor

    Done in c75d888 (#206). The registry now refuses to open a space.db that has been used when space.journal is missing, before creating anything. A new journal also won't pair with a data log or storage root that already holds segments, which covers losing space.db as well. Either way the store doesn't open and nothing in it changes, so neither compact nor the reclaim serve-s3 runs at startup gets the chance. Your repro now exits 1 and space.nvram keeps its 6000376 bytes. The changelog is updated. This closes when #206 merges.

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

    bugcore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions