Skip to content

fix(node): return Elapsed from db_bounded before polling a spent deadline - #587

Closed
costajohnt wants to merge 1 commit into
Twigpine:mainfrom
costajohnt:fix/585-db-bounded-spent-deadline
Closed

costajohnt wants to merge 1 commit into
Twigpine:mainfrom
costajohnt:fix/585-db-bounded-spent-deadline

Conversation

@costajohnt

@costajohnt costajohnt commented Nov 13, 2026 •

Copy link
Copy Markdown

Summary

db_bounded now returns BoundedDbError::Elapsed right away when the deadline is already spent, without polling the wrapped future. Before this, a future that was ready on its first poll could return a value even though the deadline had passed.

Motivation & context

db_bounded passed the remaining time straight to tokio::time::timeout. With a zero remainder, tokio still polls the inner future once before it checks the timer, so a fast local query could win. That's what made discovery_record_elapsed_leaves_neither_row flaky in CI. The helper's docs and db_bounded_elapsed_deadline_errors_promptly both say a spent deadline should error without starting the work, so this makes the code do that.

Production impact is small since db_record_deadline floors the budget for durability writes, but the read paths in both pin loops go through the same helper.

Fixes #585

Kind of change

  • Bug fix
  • Feature
  • Security fix
  • Docs
  • Tests / CI
  • Refactor (no behavior change)
  • Breaking or protocol change (issue required first)

What changed

  • crates/gitlawb-node/src/ipfs_pin.rs: db_bounded checks left.is_zero() and returns Elapsed before calling timeout.
  • New test db_bounded_spent_deadline_does_not_poll_a_ready_future: a spent deadline with a future that is ready on first poll. It asserts the elapsed arm and that the future was never polled. It fails on main with Ok(7), the same output as in the issue.
  • Fixed the doc comment on discovery_record_elapsed_leaves_neither_row, which said the future never starts under timeout(0, fut). That wasn't true before this change.

How a reviewer can verify

cargo test -p gitlawb-node --lib -- db_bounded
DATABASE_URL=... cargo test -p gitlawb-node --lib -- discovery_record_elapsed_leaves_neither_row

The new test fails deterministically with the guard removed (Ok(7)) and passes with it. I couldn't get the DB-backed flake to show up on my machine (0 of 60 runs failed with or without the fix, even with 8 busy loops running), so the direct evidence is the ready-future test. After the fix, the DB test can't reach the poll at all, so the race has nowhere to happen.

Before you request review

  • Scope is one logical change; no unrelated churn
  • cargo test -p gitlawb-node passes locally (full workspace not run)
  • New behavior is covered by tests (required for fixes)
  • cargo fmt --all and cargo clippy -p gitlawb-node --all-targets -- -D warnings are clean
  • Commit titles use Conventional Commits (feat(...), fix(...), docs(...))
  • Docs / .env.example updated if behavior or config changed (or N/A)
  • Checked existing PRs so this isn't a duplicate

Notes for reviewers

The comment on pin_skip_branch_marker_write_needs_the_record_floor mentions the poll-first behavior for a remainder that is about zero. I left it alone because it's still accurate when a little time is left. If the remainder hits exactly zero, the unfloored write now gets cut off, which only makes the floor matter more.

Summary by CodeRabbit

  • Bug Fixes
    • Database operations now return a timeout error immediately when their deadline has already expired.
  • Tests
    • Added coverage for expired deadlines and clarified what an existing test demonstrates.

…line

`tokio::time::timeout` polls the inner future once before it checks the
timer, so with a zero remainder a future that is ready on its first poll
(a fast local query) returned a value instead of `BoundedDbError::Elapsed`.
That made `discovery_record_elapsed_leaves_neither_row` flaky in CI.

Return `Elapsed` up front when no budget is left, and pin the behavior
with a ready-future test that also asserts the future is never polled.

Fixes Twigpine#585
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When db_bounded has no time remaining, it returns BoundedDbError::Elapsed without polling the wrapped future. A test verifies this behavior, and existing test documentation clarifies that the check does not prove transactionality.

Changes: Spent-deadline handling

Layer / File(s) Summary
Zero-budget return and test
crates/gitlawb-node/src/ipfs_pin.rs
db_bounded now returns Elapsed before calling timeout when no time remains. A test checks that a ready future is not polled. The discovery-record test documentation clarifies that its elapsed-deadline check does not demonstrate transactionality.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested labels: crate:node, kind:bug

Suggested reviewers: beardthelion

Merge Risk: Minimal · up to 6e7e2

The change makes a spent database deadline return an elapsed error without starting the work. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: db_bounded now returns BoundedDbError::Elapsed before polling a spent deadline. The raw summary, objectives, and description all describe this same guard in crates/git…
Description check Passed The description follows the repository template. It has a Summary, Motivation and context with a linked issue (Fixes #585), a checked Kind of change, a What changed list naming the crate and file, a H…
Linked Issues check Passed Issue #585 requires that a spent deadline return BoundedDbError::Elapsed without starting the work. The diff at the reviewed head adds if left.is_zero() { return Err(BoundedDbError::Elapsed); } in…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches only crates/gitlawb-node/src/ipfs_pin.rs. Changes are: the zero-remainder guard in db_bounded; a new test for that guard; and a doc-comment correctio…
Full details: Title check

Explanation

The title names the main change: db_bounded now returns BoundedDbError::Elapsed before polling a spent deadline. The raw summary, objectives, and description all describe this same guard in crates/gitlawb-node/src/ipfs_pin.rs. The title is specific, follows Conventional Commits, and stays under a reasonable length.

Full details: Description check

Explanation

The description follows the repository template. It has a Summary, Motivation and context with a linked issue (Fixes #585), a checked Kind of change, a What changed list naming the crate and file, a How a reviewer can verify section with commands, and the review checklist. It explains the root cause (tokio polls the inner future once before the timer check) and the test evidence. The Protocol & signing impact block was removed, which the template allows when the change does not touch the protocol. The Notes for reviewers section is filled in.

Full details: Linked Issues check

Explanation

Issue #585 requires that a spent deadline return BoundedDbError::Elapsed without starting the work. The diff at the reviewed head adds if left.is_zero() { return Err(BoundedDbError::Elapsed); } in db_bounded (crates/gitlawb-node/src/ipfs_pin.rs) before tokio::time::timeout is called, so the wrapped future is never polled when no budget remains. The new test db_bounded_spent_deadline_does_not_poll_a_ready_future covers this case directly. It uses a future that is ready on first poll and asserts both the Elapsed arm and that the future was not polled. The issue's remaining items are reproduction or verification steps: the loaded run of discovery_record_elapsed_leaves_neither_row was not repeated in this review, and the DB-backed test was not executed here. Those are test-run evidence, not additional coding requirements. The ready-future test covers the coding behavior.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches only crates/gitlawb-node/src/ipfs_pin.rs. Changes are: the zero-remainder guard in db_bounded; a new test for that guard; and a doc-comment correction on discovery_record_elapsed_leaves_neither_row. The comment correction is needed because its old text described the timeout(0, fut) path that the guard now replaces. No unrelated code, configuration, or files changed. The PR does not change the floor comment on pin_skip_branch_marker_write_needs_the_record_floor, which the PR description states is intentional.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e50cd487-f99f-4592-a657-64e2f901833b
Commits

Reviewing files that changed from the base of the PR and between 6f99019 and 6e7e2a0.

Files selected for processing: 1

  • crates/gitlawb-node/src/ipfs_pin.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.

@beardthelion

Copy link
Copy Markdown
Collaborator

Thanks @costajohnt. The same early return in db_bounded and the same ready-future test already landed on main in #586, which closed #585, so there is nothing left to merge here. Closing this one.

@costajohnt
costajohnt deleted the fix/585-db-bounded-spent-deadline branch November 13, 2026 16:48
@costajohnt

Copy link
Copy Markdown
Author

Makes sense, thanks for letting me know. Glad it got fixed either way.

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.

db_bounded polls the future when the deadline is already spent, making discovery_record_elapsed_leaves_neither_row flaky

2 participants