Repository navigation
fix(relay): read the clock at the expiry check, not before the stored read - #1108
Conversation
🦋 Changeset detectedLatest commit: 3245656 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
[Medium impact] This PR appears safe to merge.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
Closes nothing by keyword:
baseisrewrite/main, not the repository's default branch. #953 is closed by hand after the merge.A stored read could deliver an event that had already expired.
handlers/subscribe.rscapturednowonce, before the loop that walks the stored records, and passed that value to every record's expiry check. The per-event authorization beside it is genuinely per event — the guard is rebuilt for each record, which is what the comment there promises — so the clock was the only thing held across the read. With LMDB values read lazily in batches, the gap is real work, not a theoretical window: a record that expired while earlier records were still streaming went to the client anyway. The TypeScript capture readsDate.now()inside the predicate.The change
Two hunks in
crates/nostream-relay/src/handlers/subscribe.rs, 75 lines apart:before the loop, and at the check
The clock seam already existed (
HandlerContext.clock), so this is a call site moving, not a new abstraction.The oracle, and the two ways it could have been fake
The test drives a stored read where the second record's expiry falls between the first record's frame and the second record's own check: two stored events, a
TestClockadvanced 10 s between two fixture permits (gate_reads_per_item()), nosleepanywhere. Red at2c151f2a, with the fix present and the base production file restored:The three frames are the first record (
0729ff5e…,["expiration","1700003600"]), the second record that expired mid-read (498f766a…,["expiration","1700000005"]) — delivered, which is the defect — and["EOSE","expiry"]. Green at head:1 passed, and twenty repeated runs each printed1 passed.First way it could have been fake — a count. A test that only counts frames passes a wrong delivery (two frames, wrong one included). Measured: with the predicate inverted and the assertions reduced to
collected.len() == 2, the test goes green. The assertions compare event ids per frame, and that is what discriminates.Second way — a milliseconds/seconds slip.
context.now_ms() as i64makesnow~1.7e12, so every expiring record looks expired, including the one that must be delivered. That is why the first record carries an expiration an hour ahead (1700003600) rather than none: under the slip the test fails atthe first record arrived: [](left: 0/right: 1), measured independently by the builder, the code lens and the approver.Nothing here weakens an existing test:
crates/nostream-relay/tests/connection.rsgains 178 lines and deletes none.Validation
The four ignored are the
#[ignore]d Postgres-backed storage targets, unchanged by this branch.What the review did not look at
review-plan.json. The two hunks contain no authorization line:read_authorization_guard(), the restricted-read set and theAUTHhandling sit unchanged 30-odd lines below, and the new tests run withnip42: Noneso no restriction is in play. The extra clock read is anAtomicI64load on a path that already pays a mutex-guarded LMDB read and an existing per-record guard read. All thirteen rendered-surface globs were counted: 0.ran, at this sha (static/round-1.json). That is a verdict, not an absence of evidence.Notes the lenses raised, not fixed here
handlers/mod.rs'snow_secsdoc or an ADR line; both are outside this job's three paths.