Skip to content

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

@zancas

Description

@zancas

Parent: #1051

Today

Both packages/zaino-serve/src/server/jsonrpc.rs:86-95 and …/grpc.rs:49-58 build an explicit polling future to integrate with their underlying server's shutdown plumbing (jsonrpsee's stop race, tonic's serve_with_shutdown):

let shutdown_check_status = status.clone();
let mut shutdown_check_interval = interval(Duration::from_millis(100));
let shutdown_signal = async move {
    loop {
        shutdown_check_interval.tick().await;
        if shutdown_check_status.load() == StatusType::Closing {
            break;
        }
    }
};

This block exists only because NamedAtomicStatus lacks a wake primitive. Observed shutdown latency from close() to signal completion: 0–100 ms (~50 ms typical).

Proposed change

  • Add cancel_token: CancellationToken to both JsonRpcServer and TonicServer (zaino-serve already gets tokio-util transitively via the new dep added on DRY: extract shared DbCore lifecycle scaffolding from DbV0/DbV1 (~70 LOC) #1049's branch — verify).
  • Replace each polling closure with:
    let token = self.cancel_token.clone();
    let shutdown_signal = async move { token.cancelled().await };
  • close() calls self.cancel_token.cancel() AND still stores StatusType::Closing (the status flag remains the externally observable state indicator and gates status() reads).
  • Drop continues to abort() as a backstop for the "dropped without close" path.

Acceptance criteria

  • The shutdown_check_interval polling closure is gone from both jsonrpc.rs and grpc.rs.
  • New unit test asserts the shutdown-signal future completes within ~20 ms after close(). The test fails against the current polling pattern and passes after migration.
  • Existing server-shutdown integration tests remain green.

Out of scope

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureArchitectural improvementscode-smellCode smells that need attention

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions