Skip to content

[Bug]: Host lease: a stalled or clock-skewed holder silently rejoins after another host took over; skew also blocks hosts far past the 20 s TTL #978

Closed

Description

@jimhoyd

Security-relevant (data integrity: two hosts serving one SQLite database), severity: medium. Found in an adversarial review of #949 (#941).

Runtime revision and environment

origin/main ba9f3204, Node 26.10, macOS; the lease is driven directly with fake clocks and probes via joinHostLease's now/probe options.

What happens

packages/core/src/host-lease.ts joinHostLease checks for a foreign peer only when joining (other = joining ? peers.find(foreign) : undefined). Afterwards each heartbeat deletes expired rows and upserts its own row. Heartbeat errors are swallowed without a log. Expiry compares the observer's wall clock with a deadline the writer computed from its own clock. Three consequences, all observed:

  1. A stalled holder comes back silently. Host A joins, then stalls longer than the TTL (VM suspend, SIGSTOP, a blocked event loop, 20 s of failed beats). Host B joins legitimately, deleting A's expired row. On A's next beat it re-inserts its row and keeps serving. Observed rows afterwards: [{host:'host-a',…},{host:'host-b',…}], and neither side refused or logged anything.
  2. A joiner whose clock is ahead evicts a live holder. If B's clock is more than the TTL ahead, B deletes A's live row and joins (observed rows: [{host:'host-b',…}]). A then re-inserts, as in (1).
  3. A crashed host whose clock was ahead blocks every other host for the size of the skew. Observed with A's clock one day ahead and its connection closed without close(): B is refused with "If that host is gone, its lease expires within 20 s of its last heartbeat", but the refusal lasts a day, and nothing tells the operator how to clear it.

On macOS, Windows and Linux filesystems missing from the network list, this lease is the only guard against multi-host SQLite.

Minimal reproduction

const clock = { a: base, b: base };
const A = await joinHostLease(dbA, { table: 't', what: 'store', now: () => clock.a, probe: { hostname: () => 'host-a', bootId: async () => 'boot-a' } });
clock.b = base + 25_000;                       // A has not beaten for more than the TTL
const B = await joinHostLease(dbB, { table: 't', what: 'store', now: () => clock.b, probe: { hostname: () => 'host-b', bootId: async () => 'boot-b' } }); // joins
clock.a = base + 25_000;
await sleep(SERVER_LEASE.heartbeatMs + 300);   // A's heartbeat
// SELECT host FROM t  ->  host-a, host-b   (both serving)

For (3): A joins with now: () => base + 86_400_000 and its connection is closed without close(). B joins with now: () => base and is refused.

Suggested fix

  • If a beat finds its own row gone, or a live foreign peer present, treat the lease as lost: fail closed (stop serving), log loudly, and do not re-insert.
  • Log heartbeat failures.
  • Judge a peer's liveness by whether its heartbeat_at changes, observed on the observer's monotonic clock, rather than by comparing wall clocks across hosts. Or at least name clock skew in the refusal when a foreign row's expires_at is beyond now + ttl.
  • Document how an operator clears a stale row.
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