Skip to content

helperize shutdown signal#1054

Merged
AloeareV merged 9 commits intodevfrom
use_CT_in_jsonrpc
Apr 29, 2026
Merged

helperize shutdown signal#1054
AloeareV merged 9 commits intodevfrom
use_CT_in_jsonrpc

Conversation

@zancas
Copy link
Copy Markdown
Member

@zancas zancas commented Apr 24, 2026

Pre-requires: #1050

Fixes: #1052

zancas added 6 commits April 24, 2026 14:28
…bV1 (#1049)

  Move the ~70 LOC of byte-identical lifecycle scaffolding (status,
  wait_until_ready, clean_trailing, zaino_db_handler_sleep, shutdown)
  and the open_or_create_db helper from db/v0.rs and db/v1.rs into a
  single pub(super) trait DbLifecycle and a free fn in the parent db.rs.

  DbV0 and DbV1 now impl DbLifecycle via four field getters; their
  DbCore::{status, shutdown} delegate to the trait. The duplicated
  tokio::select! shutdown/abort block and the three-way
  sleep-or-maintenance body now live in one place.

  DbBackend::{status, shutdown} in db.rs use fully-qualified DbCore::
  paths to disambiguate the two traits now in scope on DbV0/DbV1.

  #1033 (notify_one race) preserved verbatim on DbLifecycle::shutdown —
  fixing it is now a single-call-site change.

  Parent: #862

  Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… race

  DbLifecycle::shutdown signals waiters with Notify::notify_one, which
  wakes at most one task and stores at most one permit. With N > 1
  tasks awaiting the same shutdown_notify, N-1 are stranded.

  Adds a maximally narrow test in db.rs::shutdown that impls
  DbLifecycle on a minimal FakeDb, spawns N=3 tasks each registering
  interest via Notified::enable + Barrier, then asserts every waiter
  completes within 200ms after shutdown returns.

  As intended, this test uniquely fails among the 226 in-package
  tests (225 passed, 1 failed, 2 skipped). The fix — switching the
  signaling primitive to a wake-all mechanism (watch, flag +
  notify_waiters, CancellationToken) — will flip it green and is
  tracked in #1033.

  Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
  Replaces Arc<Notify> + notify_one with tokio_util CancellationToken in
  DbLifecycle. notify_one wakes at most one waiter and stores at most
  one permit, so N>1 background tasks awaiting the same shutdown_notify
  were stranded after shutdown. cancel() wakes every current waiter and
  persists state for late subscribers; cancelled() handles the
  late-poll case without an explicit register-before-wait dance.

  Adds tokio-util = "0.7" to the workspace and zaino-state crate.

  Trait change: shutdown_notify() -> &Arc<Notify> becomes cancel_token()
  -> &CancellationToken. The two consumer sites (DbLifecycle::shutdown
  notifier and zaino_db_handler_sleep waiter) and every Self {...} clone
  literal across db/v0.rs, db/v1.rs, db/v1/write_core.rs, and
  db/v1/compact_block.rs are updated to match.

  Regression test from #1049 (db::shutdown::wakes_every_shutdown_waiter)
  now passes — flips from the unique failure to a green invariant.

  Closes #1033.

  Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
  Hoists the inline status-polling closure in JsonRpcServer::spawn into
  a private async fn so the next commit can target it with a latency
  regression test before swapping the body for CancellationToken.

  Pure refactor: same 100ms-tick polling body, same call site
  semantics, no API change.

  Refs #1051 (parent tracking issue).

  Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@zancas zancas requested a review from idky137 April 24, 2026 23:45
@zancas zancas marked this pull request as ready for review April 27, 2026 16:35
@zancas zancas changed the title Use ct in jsonrpc Use tokio_util Cancellation Token in jsonrpc Apr 28, 2026
@zancas zancas changed the title Use tokio_util Cancellation Token in jsonrpc helperize shutdown signal Apr 29, 2026
Copy link
Copy Markdown
Contributor

@AloeareV AloeareV left a comment

Choose a reason for hiding this comment

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

Nice!

@AloeareV AloeareV merged commit 1f52ff7 into dev Apr 29, 2026
15 of 17 checks passed
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.

refactor(zaino-serve): replace shutdown_signal polling with CancellationToken

3 participants