Skip to content

A worker keeps every deleted device in memory and in status.get for its whole life #1410

Closed

Description

@V3RON

A worker keeps every deleted device row in memory and in state.db for its whole life, and status.get lists all of them. Both grow with every device the worker has ever made. #1409 saw 351 deleted rows next to 13 live ones after about 31 h.

  • src/core/registry.ts:174: #devices is loaded with every row and no path removes one. A deletion only sets state: "deleted".
  • src/core/registry.ts:199: snapshot clones every row, deleted ones included, on each read.
  • src/daemon/dispatcher.ts:415: status.get answers snapshot.devices, so the gateway's refresh of each worker's view carries them all too.

Nothing needs an old deleted record. Almost every reader filters state !== "deleted". The only rows that point at one are lease_grants (removed with their request after lease.requestRetentionMs, src/core/registry-rows.ts:389) and the device's own device_enrolled_apps.

Found while triaging #1409.

Fix (decided by the maintainer, 2026-11-07)

A deleted device record expires, the way a settled lease request does (lease.requestRetentionMs).

  • New config key devices.deletedRetentionMs: how long a deleted record is kept, counted from when it was deleted. Positive number of ms, default 3600000 (1 h). Smaller than lease.requestRetentionMs is refused at config load.
  • devices gets a deleted_at column (schema migration). A row already deleted at the first start after the upgrade gets that start's time.
  • The registry drops an expired record from memory and deletes its devices and device_enrolled_apps rows, in one commit, at startup and on each new device.deleted. No event for the removal.
  • A user sees a deleted device in status.get and the console for up to the retention time, and then it is gone; asking about its id after that answers as for an unknown device.
  • ADR 0022 gets an Amended: line: a deleted record stays for devices.deletedRetentionMs, not for good. The core manual line "deleted records stay in state.db" changes to match.
  • Docs: the configuration reference gets the key; the physical-devices page says a removed phone stays in the answer for devices.deletedRetentionMs.

Done when

  • A deleted record older than devices.deletedRetentionMs is gone from status.get and from state.db after the next startup or the next device.deleted.
  • A deleted record younger than that is still listed.
  • A row deleted before the upgrade is kept for one retention period after the first start.
  • devices.deletedRetentionMs below lease.requestRetentionMs is refused at config load.
  • ADR 0022, the core manual and the two website pages say the same as the code.

Tests

  • Replace the triage test on bug/1410-repro (it pins a cap of 100, which was not taken) with: 3 devices deleted at t0, t0+30 min, t0+70 min; at t0+90 min with the default the first is gone from memory and state.db, the other two remain.
  • Migration: a database with deleted rows and no deleted_at loads, and the rows survive one retention period.
  • Config: the refused value names both keys in its error.

Written by an agent.

Activity

  1. added
    bug:new
    on Nov 7, 2026
  2. self-assigned this
    on Nov 7, 2026
  3. V3RON commented on Nov 7, 2026

    @V3RON
    ContributorAuthor

    Reproduction

    Confirmed. This is a gap: nothing says deleted rows stay in memory or in status.get, and nothing bounds them. The test's expectation is a proposal: keep 100 deleted records.

    src/core/registry-state-db.test.ts > "the snapshot holds every live device and only the 100 most recently deleted ones, while state.db keeps every deleted row, before and after a restart" (branch bug/1410-repro). The fixture has 2 live devices and 103 deleted ones. One of the deleted devices is created first and deleted last.

    pnpm exec vitest run --project unit src/core/registry-state-db.test.ts -t "100 most recently"
    AssertionError: expected [ 'dev_10', 'dev_100', …(101) ] to deeply equal [ 'dev_10', 'dev_100', …(98) ]
    

    Root cause

    src/core/registry.ts:993 (#commit) sets #devices = committed and drops nothing. src/core/registry.ts:185 loads every row from SELECT * FROM devices. snapshot (:194) copies them all, and status.get (src/daemon/dispatcher.ts:415) passes snapshot.devices through as it is. Writing a device deleted is a state change, not a removal, so every device the worker ever made stays in memory. The symptom shows in status.get and in the gateway's view of the worker.

    Simplest fix

    Proposed shape: the registry keeps in memory only the 100 most recently deleted records. At runtime it tracks deletion order in memory. On load it keeps the last 100 by rowid. state.db keeps every row, as the core manual requires.

    # apply #boundDeleted in Registry (load and #commit), then:
    pnpm exec vitest run --project unit src/core/          -> 53 files, 1304 passed
    pnpm exec vitest run --project unit src/daemon src/leasing -> 60 files passed (relay test timed out once under load, #1412; passes alone)
    

    Alternatives rejected

    • Filter deleted out of status.get only: memory still grows, and the e2e waitForDeviceState(…, "deleted") checks rely on seeing them.
    • Delete the rows from state.db past the cap: this breaks the core manual's "deleted records stay in state.db".
    • A config key like lease.maxRequestRecords: this adds config surface where a constant is enough.

    Risk

    • An evicted id now throws UnknownDeviceError from #requireDeviceRecord. Check callers that act on old ids.
    • After a restart the kept set follows creation order, not deletion order. A long-lived phone that was unenrolled late can drop out.
    • physical-devices.mdx says "a removed phone stays in the answer". It needs "for a while".

    Fast track

    • Defect: no — a gap; the cap of 100 is a proposal
    • Fix tried: yes
    • Nothing public added or broken: no — status.get stops listing older deleted devices
    • No ADR or rule changed: no — not checked against ADR 0022 §4 "the record stays"
    • One fix: no — the cap value and where to filter change what users see

    Side findings

    Written by an agent.

  4. removed their assignment
    on Nov 7, 2026
  5. V3RON commented on Nov 7, 2026

    @V3RON
    ContributorAuthor

    Spec updated: the fix is now a retention time for deleted records (devices.deletedRetentionMs, 1 h), not a cap of 100.

    Written by an agent.

  6. added
    bug:ready
    and removed
    bug:new
    on Nov 7, 2026
  7. self-assigned this
    on Nov 7, 2026
  8. V3RON commented on Nov 7, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: #1415 (#1410)
    Parked: none
    Waiting: none
    Decided: none

    Written by an agent.

  9. added a commit that references this issue on Nov 10, 2026
    e43701c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions