Skip to content

Improve cancellation safety and cooperative shutdown for Tokio async code #302

Description

@henry0715-dev

Background

crusher still has Tokio async paths where shutdown/reload/reconnect can leave work partially abandoned or state inconsistent.

Key candidates:

  • top-level spawned tasks are not always joined to completion
  • some paths hold Mutex/RwLock guards across .await
  • important shared state (active_policy_list, delete_policy_ids, INGEST_CHANNEL, LAST_TRANSFER_TIME) is directly shared instead of actor-owned
  • timestamp persistence is not guaranteed to be safely flushed during shutdown

Goal

Apply cancellation safety to crusher's Tokio async code so that the system has cooperative cancellation, safe drain behavior, and restart-time state consistency.

Scope

  1. Identify vulnerable paths
  • audit lock-across-await code
  • list long-running and detached spawned tasks
  • identify work that can be abandoned during shutdown
  1. Define cancellation policy
  • evaluate CancellationToken / TaskTracker
  • define shutdown requested / draining / completed phases
  • add a common mechanism to verify all child tasks have stopped
  1. Improve shared-state ownership
  • ban Mutex/RwLock guards across .await
  • move important shared state to actors or single-owner tasks
  • redesign the lock-based SendStream serialization path
  1. Harden drain and restart consistency
  • guarantee pending-work drain and timestamp flush on shutdown
  • make timestamp-file persistence safer
  • add restart consistency tests

Acceptance Criteria

  • no detached tasks remain after shutdown/reload/reconnect
  • core lock-across-await paths are removed or structurally redesigned
  • top-level shutdown completion means real child-task drain completion
  • policy/stream/timestamp state is managed consistently
  • tests cover cancellation, shutdown, and restart scenarios

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions