Skip to content

fix(relay): read the clock at the expiry check, not before the stored read - #1108

Merged
cameri merged 2 commits into
rewrite/mainfrom
factory/027-expiry-clock
Nov 10, 2026
Merged

cameri merged 2 commits into
rewrite/mainfrom
factory/027-expiry-clock

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

Closes nothing by keyword: base is rewrite/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.rs captured now once, 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 reads Date.now() inside the predicate.

The change

Two hunks in crates/nostream-relay/src/handlers/subscribe.rs, 75 lines apart:

-    let now = context.now_secs();

before the loop, and at the check

-                if expiration::is_expired_event(&event, now) {
+                // The clock is asked when the record is judged, not once before the loop, so a
+                // record that expires while earlier ones are still being read is not delivered.
+                if expiration::is_expired_event(&event, context.now_secs()) {

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 TestClock advanced 10 s between two fixture permits (gate_reads_per_item()), no sleep anywhere. Red at 2c151f2a, with the fix present and the base production file restored:

running 1 test
assertion `left == right` failed at crates/nostream-relay/tests/connection.rs:721
  left: 3
 right: 2
test result: FAILED. 0 passed; 1 failed;       (exit 101)

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 printed 1 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 i64 makes now ~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 at the 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.rs gains 178 lines and deletes none.

Validation

cargo fmt --all --check                                               exit 0
cargo clippy --workspace --all-targets --all-features -D warnings      exit 0
cargo clippy --workspace --lib --bins --all-features (strict five)     exit 0
cargo test --workspace --all-features      1247 passed / 0 failed / 4 ignored   (80 targets)
cargo build --workspace --all-features --release                       exit 0
cargo test -p nostream-relay --test connection                         18 passed

The four ignored are the #[ignore]d Postgres-backed storage targets, unchanged by this branch.

What the review did not look at

  • security, performance, ux, ui stood down with checkable reasons in review-plan.json. The two hunks contain no authorization line: read_authorization_guard(), the restricted-read set and the AUTH handling sit unchanged 30-odd lines below, and the new tests run with nip42: None so no restriction is in play. The extra clock read is an AtomicI64 load 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.
  • The static stage ran and found nothing — 0 findings from 1 provider whose status is ran, at this sha (static/round-1.json). That is a verdict, not an absence of evidence.

Notes the lenses raised, not fixed here

  • The new comment states the rule at the check site, but a future batched read in another handler could reintroduce the shape without seeing it. The durable carriers are handlers/mod.rs's now_secs doc or an ADR line; both are outside this job's three paths.
  • Two clock reads now happen in one loop iteration (this check and the guard's own). They cannot disagree: there is no suspension point between them, so both land inside one poll — a structural argument, not a probability.
  • The red arm reads as a frame-count mismatch first and as "the expired record was delivered" second. A one-line reorder of the diagnostic would say it in the failure message.

@changeset-bot

changeset-bot Bot commented Nov 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3245656

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When 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

@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

Greptile CLI Reused the Greptile CLI review of 3245656 by Ricardo Arturo Cabral Mejia. The code matches this PR, so no new review ran.

[Medium impact] This PR appears safe to merge.

Summary

Stored reads now check the current time for each event instead of reusing the time from the start of the read.

  • Stored-event subscriptions skip events that expire during a long read.

Reviews (1) · Last reviewed commit: "fix(relay): read the clock at the expiry..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 14cd6438-39dd-4236-998c-ae334d27915d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@cameri
cameri merged commit b42f833 into rewrite/main Nov 10, 2026
16 checks passed
@cameri
cameri deleted the factory/027-expiry-clock branch November 10, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants