Skip to content

fix(kernel): wait out shared-registry locks on lease renewal - #689

Open
khaliqgant wants to merge 2 commits into
mainfrom
fix/relayflowd-journal-lock
Open

khaliqgant wants to merge 2 commits into
mainfrom
fix/relayflowd-journal-lock

Conversation

@khaliqgant

@khaliqgant khaliqgant commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Review flows on a loaded Linux host failed lease renewal on the one relayflowd.sqlite3 shared by adversarial-review, context-review, and ci:

journal_write_failed: persist renewed lease deadline: SQLite journal failed: database is locked

Steps then ended as run_terminal … cannot accept mutations. Seen with @relayflows/runtime-linux-x64 2.0.55 at load 20–60.

The registry was already WAL with a 5s busy_timeout, but every short-lived open re-ran PRAGMA journal_mode=WAL (an exclusive lock the busy handler does not cover, retried for about 100ms) and re-executed schema DDL. The run journal set WAL and left busy_timeout at 0.

Every open now waits 15 seconds, switches to WAL only when the file is not already WAL, and skips registry DDL once the schema is current. A write that still cannot commit is returned. It is not swallowed. There is no fleet concurrency cap.

Follow-up 7a569cfc keeps lease_ttl_ms measured from the instant the daemon set the deadline, before the durable write. The worker adds that duration to the time it sent the heartbeat. Measuring after the write counted the wait twice: a 14s lock left the worker about 2s on a lease the daemon had just extended. The worker deadline is unchanged when the write is fast.

Validation

cargo test -p relayflowd-journal --offline -- --test-threads=8 from kernel/: 35 passed, including:

  • renew_deadline_commits_while_another_connection_holds_a_read
  • renew_deadline_waits_out_a_short_write_instead_of_failing_locked
  • renew_deadline_does_not_swallow_a_write_that_cannot_commit
  • failed_commit_is_returned_not_swallowed

With the timeout forced to zero, the wait-out test fails with SQLite journal failed: database is locked.

cargo test -p relayflowd --offline --lib -- heartbeat_ttl_keeps_the_full_lease_across_a_slow_durable_write heartbeat_carries_the_remaining_lease_as_a_duration --test-threads=1: both passed. The new test holds BEGIN IMMEDIATE for 2 seconds across the heartbeat and requires lease_ttl_ms >= 29_000.

Agent session: 01a19b64-f33d-7f63-bb7f-eb61212c7809

A review fleet sharing one relayflowd registry failed heartbeats with
`journal_write_failed: persist renewed lease deadline: SQLite journal failed: database is locked`,
and the step then ended terminal. The registry was already WAL with a 5s
busy_timeout, but every short-lived open re-ran `PRAGMA journal_mode=WAL`
(an exclusive lock the busy handler does not cover; the retry is ~100ms)
and re-executed schema DDL. Under load that writer queue outlasted 5s.
The run journal set WAL and never armed a busy_timeout, so a second
connection failed immediately.

Every open now arms a 15s busy_timeout — inside the 30s protocol timeout
and the slack on a 30s lease — and switches to WAL only when the file is
not already WAL. Registry DDL runs only when the schema is not current.
Read-only opens get the same timeout. A renewal that still cannot commit
returns the SQLite error; it is not swallowed.

No fleet concurrency cap. One WAL writer plus the wait serializes the
heartbeats; a cap would only hide a lock that is still too long.

Regression: renew_deadline commits while another connection holds a read,
and waits out a short IMMEDIATE write instead of returning database is
locked. With the timeout forced to zero that test fails with
`SQLite journal failed: database is locked`.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T09:44:11.066011Z 55214d2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
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: e265a3da-a6fd-4971-bf18-1816689fc547
Commits

Reviewing files that changed from the base of the PR and between 5abbb7a and 55214d2.

Files selected for processing: 3
  • kernel/relayflowd-journal/src/lib.rs
  • kernel/relayflowd-journal/src/registry.rs
  • kernel/relayflowd-journal/src/sqlite.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The journal crate adds shared SQLite connection helpers for busy timeouts and WAL setup. Journal and registry opens use those helpers. Registry schema setup now runs only when required tables or the expected column are missing.

Changes: SQLite Journal Setup

Layer / File(s) Summary
Shared SQLite WAL setup
kernel/relayflowd-journal/src/sqlite.rs
Adds a 15-second busy timeout, bounded WAL-switch retries, and helpers for WAL validation and read-only connection timeouts.
Journal connection configuration
kernel/relayflowd-journal/src/lib.rs
Applies the shared helpers to journal opens. Writable configuration validates WAL mode and enables foreign keys. A non-WAL mode returns JournalNotWal.
Registry open and schema checks
kernel/relayflowd-journal/src/registry.rs
Uses shared WAL setup and checks for five tables and the event_dedupe.boot_id column before running schema setup. Adds tests for busy timeouts, WAL mode, and lease-renewal behavior.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: kjgbot, miyaontherelay

Merge Risk: Low · up to 55214

An uncommon lock sequence could delay a heartbeat until its renewed lease has expired. The change is mergeable with owner awareness of this edge case.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: preventing shared-registry lock failures during lease renewal.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the problem (lease renewal failing with 'database is locked' on a shared relayflowd.sqlite3), the fix (15-second busy timeout on every open, WAL switch only when needed, skipp…
Full details: Description check

Explanation

The description explains the problem (lease renewal failing with 'database is locked' on a shared relayflowd.sqlite3), the fix (15-second busy timeout on every open, WAL switch only when needed, skipping registry DDL when the schema is current, returning rather than swallowing failed writes), a follow-up commit that changes lease TTL measurement, and the tests run. This maps directly to the raw summary's changes in lib.rs, registry.rs, and sqlite.rs, so the description is clearly related to the changeset.

Full details: Docstring Coverage

Explanation

Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the WAL at dawn
Then sets the timeout before it’s gone
The tables stand in tidy rows
A lease renews as SQLite flows
Soft paws thump: the checks are done

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

View guided diff | Turn on auto-fix | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; the previous lease-duration issue is fixed and no new blocking issue remains.

Summary

The PR lets SQLite connections wait out shared-registry locks while keeping write failures visible.

  • SQLite opens wait out ordinary locks and skip repeated setup work.
  • Heartbeat replies keep the full renewed lease duration after a slow write.

Diagram

sequenceDiagram
  participant Worker
  participant Server
  participant Registry as SQLite registry
  Worker->>Worker: Record request send time
  Worker->>Server: step.heartbeat
  Server->>Server: Capture now and extend lease
  Server->>Registry: Persist renewed deadline
  Note over Registry: Wait for a short write lock to clear
  Registry-->>Server: Commit or return error
  Server-->>Worker: Deadline and duration measured from captured now
  Worker->>Worker: Add duration to request send time
Loading

Reviews (2) · Last reviewed commit: "Keep a renewed lease's duration measured..." · Reviewed by Greptile

Comment thread kernel/relayflowd-journal/src/sqlite.rs Resolved
The worker adds lease_ttl_ms to the time it sent the heartbeat. Measuring that duration after the durable write subtracted a long lock twice, so the worker could abandon a lease the daemon had just extended.
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.

1 participant