Skip to content

refactor(engine): channel-based drain shutdown for delete emitter (ATU-4 #2381) - #4401

Merged
oferchen merged 1 commit into
masterfrom
feature/atu-4-channel-drain
May 18, 2026
Merged

oferchen merged 1 commit into
masterfrom
feature/atu-4-channel-drain

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Replace Arc::try_unwrap for the cursor handoff in DeleteContext::into_emitter with a crossbeam-channel producer/consumer pair. Workers (clones of Arc<DeleteContext>) enqueue cursor observations; the drain takes self by value, drops the master sender, and blocks on rx.recv() -> Err to assemble an owned DirTraversalCursor without ever calling try_unwrap on a shared handle.
  • Add two regression tests under crates/engine/src/delete/context.rs::tests:
    • channel_drain_completes_when_workers_drop_normally - 8 producer threads, all joined; drain finishes without blocking and counts every observation.
    • channel_drain_completes_when_worker_panics_mid_send - one worker panics inside catch_unwind after enqueueing; the drain still completes and the surviving worker's observation is delivered.
  • Keep the diagnostic helper alive as try_unwrap_or_log (with structured tracing::warn! carrying strong_count/weak_count/kind); the plan-map side still routes through it as the documented transition-era fallback for fragile drain sites.
  • Add DeleteContext::cursor_snapshot() for tests and diagnostics that need mid-flight cursor inspection; it drains and re-enqueues observations so the eventual drain still sees the full set. Existing callers that touched the old pub cursor: Arc<Mutex<...>> field are updated to use the accessor.

Test plan

  • cargo fmt --all clean (whitespace + line lengths verified manually; full fmt run reserved for CI per project policy)
  • New regression tests cover the normal-shutdown and worker-panic shutdown paths
  • No public API change beyond pub cursor -> pub fn cursor_snapshot(); the four call sites that previously accessed the field directly (one in transfer, three in-file tests) now use the accessor
  • No new dependencies; uses the already-present crossbeam-channel workspace dep
  • No unsafe, no #[allow(...)] clippy waivers, no em-dashes

Closes ATU-4 (#2381).

@oferchen
oferchen force-pushed the feature/atu-4-channel-drain branch 2 times, most recently from 664e49e to 9a003da Compare May 18, 2026 01:14
…ap at delete emitter (ATU-4 #2381)

Convert the cursor handoff in `DeleteContext::into_emitter` from
`Arc::try_unwrap(self.cursor)` plus `Mutex::into_inner` to a
crossbeam-channel producer/consumer pair.

Workers (clones of `Arc<DeleteContext>`) call `observe_segment_for_delete`
and `observe_directory`, which enqueue an owned `CursorObservation`
onto the producer side. The drain (`into_emitter`) takes `self` by
value - by Rust ownership rules every Arc clone has been released by
that point - then drops the master sender to close the channel and
blocks on `rx.recv()` until it returns `Err`. Each observation is
folded into a freshly built `DirTraversalCursor`. No `try_unwrap` on
the cursor handle; no `Mutex::into_inner` panic on poison.

The plan-map side keeps `Arc::try_unwrap`, now wrapped in a
`try_unwrap_or_log` helper that emits a structured `tracing::warn!`
with strong/weak counts before returning the original `Arc`. This is
the documented transition-era fallback for fragile drain sites that
have not yet adopted channel-shutdown handoff (per
`docs/architecture/drain-error-recovery.md`).

Tests added:

- `channel_drain_completes_when_workers_drop_normally` - spawns 8
  producer threads, joins them all, asserts the drain finishes without
  blocking and counts every worker observation.
- `channel_drain_completes_when_worker_panics_mid_send` - one worker
  panics inside `catch_unwind` after sending its observation; the
  drain still completes and the surviving worker's observation is
  delivered.

For tests and diagnostics that want mid-flight cursor inspection,
`cursor_snapshot()` drains the channel, replays the observations into
a private cursor, then re-enqueues them so the eventual drain still
sees the full set. Three internal tests and one transfer-side
integration test now access the cursor through that accessor.

`DirTraversalCursor` and its `Frame` derive `Clone` so the snapshot
helper can rebuild a private view without changing the original.

Closes ATU-4 (#2381).
@oferchen
oferchen force-pushed the feature/atu-4-channel-drain branch from 9a003da to 684acea Compare May 18, 2026 01:17
@oferchen
oferchen merged commit 2c4ef05 into master May 18, 2026
3 of 8 checks passed
oferchen added a commit that referenced this pull request May 18, 2026
…ap at delete emitter (ATU-4 #2381) (#4401)

Convert the cursor handoff in `DeleteContext::into_emitter` from
`Arc::try_unwrap(self.cursor)` plus `Mutex::into_inner` to a
crossbeam-channel producer/consumer pair.

Workers (clones of `Arc<DeleteContext>`) call `observe_segment_for_delete`
and `observe_directory`, which enqueue an owned `CursorObservation`
onto the producer side. The drain (`into_emitter`) takes `self` by
value - by Rust ownership rules every Arc clone has been released by
that point - then drops the master sender to close the channel and
blocks on `rx.recv()` until it returns `Err`. Each observation is
folded into a freshly built `DirTraversalCursor`. No `try_unwrap` on
the cursor handle; no `Mutex::into_inner` panic on poison.

The plan-map side keeps `Arc::try_unwrap`, now wrapped in a
`try_unwrap_or_log` helper that emits a structured `tracing::warn!`
with strong/weak counts before returning the original `Arc`. This is
the documented transition-era fallback for fragile drain sites that
have not yet adopted channel-shutdown handoff (per
`docs/architecture/drain-error-recovery.md`).

Tests added:

- `channel_drain_completes_when_workers_drop_normally` - spawns 8
  producer threads, joins them all, asserts the drain finishes without
  blocking and counts every worker observation.
- `channel_drain_completes_when_worker_panics_mid_send` - one worker
  panics inside `catch_unwind` after sending its observation; the
  drain still completes and the surviving worker's observation is
  delivered.

For tests and diagnostics that want mid-flight cursor inspection,
`cursor_snapshot()` drains the channel, replays the observations into
a private cursor, then re-enqueues them so the eventual drain still
sees the full set. Three internal tests and one transfer-side
integration test now access the cursor through that accessor.

`DirTraversalCursor` and its `Frame` derive `Clone` so the snapshot
helper can rebuild a private view without changing the original.

Closes ATU-4 (#2381).
oferchen added a commit that referenced this pull request May 18, 2026
…ap at delete emitter (ATU-4 #2381) (#4401)

Convert the cursor handoff in `DeleteContext::into_emitter` from
`Arc::try_unwrap(self.cursor)` plus `Mutex::into_inner` to a
crossbeam-channel producer/consumer pair.

Workers (clones of `Arc<DeleteContext>`) call `observe_segment_for_delete`
and `observe_directory`, which enqueue an owned `CursorObservation`
onto the producer side. The drain (`into_emitter`) takes `self` by
value - by Rust ownership rules every Arc clone has been released by
that point - then drops the master sender to close the channel and
blocks on `rx.recv()` until it returns `Err`. Each observation is
folded into a freshly built `DirTraversalCursor`. No `try_unwrap` on
the cursor handle; no `Mutex::into_inner` panic on poison.

The plan-map side keeps `Arc::try_unwrap`, now wrapped in a
`try_unwrap_or_log` helper that emits a structured `tracing::warn!`
with strong/weak counts before returning the original `Arc`. This is
the documented transition-era fallback for fragile drain sites that
have not yet adopted channel-shutdown handoff (per
`docs/architecture/drain-error-recovery.md`).

Tests added:

- `channel_drain_completes_when_workers_drop_normally` - spawns 8
  producer threads, joins them all, asserts the drain finishes without
  blocking and counts every worker observation.
- `channel_drain_completes_when_worker_panics_mid_send` - one worker
  panics inside `catch_unwind` after sending its observation; the
  drain still completes and the surviving worker's observation is
  delivered.

For tests and diagnostics that want mid-flight cursor inspection,
`cursor_snapshot()` drains the channel, replays the observations into
a private cursor, then re-enqueues them so the eventual drain still
sees the full set. Three internal tests and one transfer-side
integration test now access the cursor through that accessor.

`DirTraversalCursor` and its `Frame` derive `Clone` so the snapshot
helper can rebuild a private view without changing the original.

Closes ATU-4 (#2381).
@oferchen
oferchen deleted the feature/atu-4-channel-drain branch May 19, 2026 15:09
oferchen added a commit that referenced this pull request Aug 24, 2026
…ap at delete emitter (ATU-4 #2381) (#4401)

Convert the cursor handoff in `DeleteContext::into_emitter` from
`Arc::try_unwrap(self.cursor)` plus `Mutex::into_inner` to a
crossbeam-channel producer/consumer pair.

Workers (clones of `Arc<DeleteContext>`) call `observe_segment_for_delete`
and `observe_directory`, which enqueue an owned `CursorObservation`
onto the producer side. The drain (`into_emitter`) takes `self` by
value - by Rust ownership rules every Arc clone has been released by
that point - then drops the master sender to close the channel and
blocks on `rx.recv()` until it returns `Err`. Each observation is
folded into a freshly built `DirTraversalCursor`. No `try_unwrap` on
the cursor handle; no `Mutex::into_inner` panic on poison.

The plan-map side keeps `Arc::try_unwrap`, now wrapped in a
`try_unwrap_or_log` helper that emits a structured `tracing::warn!`
with strong/weak counts before returning the original `Arc`. This is
the documented transition-era fallback for fragile drain sites that
have not yet adopted channel-shutdown handoff (per
`docs/architecture/drain-error-recovery.md`).

Tests added:

- `channel_drain_completes_when_workers_drop_normally` - spawns 8
  producer threads, joins them all, asserts the drain finishes without
  blocking and counts every worker observation.
- `channel_drain_completes_when_worker_panics_mid_send` - one worker
  panics inside `catch_unwind` after sending its observation; the
  drain still completes and the surviving worker's observation is
  delivered.

For tests and diagnostics that want mid-flight cursor inspection,
`cursor_snapshot()` drains the channel, replays the observations into
a private cursor, then re-enqueues them so the eventual drain still
sees the full set. Three internal tests and one transfer-side
integration test now access the cursor through that accessor.

`DirTraversalCursor` and its `Frame` derive `Clone` so the snapshot
helper can rebuild a private view without changing the original.

Closes ATU-4 (#2381).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant