Skip to content

fix(channel): drop-guard so a panic cannot skip the exec.finished audit event - #124

Merged
gonzafg2 merged 2 commits into
mainfrom
fix/exec-finished-drop-guard
Jul 19, 2026
Merged

gonzafg2 merged 2 commits into
mainfrom
fix/exec-finished-drop-guard

Conversation

@gonzafg2

Copy link
Copy Markdown
Owner

What

Closes the pre-existing audit gap surfaced by the ADR-0028 adversarial review (#123 discussion): a panic unwinding the connection task between exec.started and exec.finished dropped the Driver without its terminal audit record, violating the ADR-0023 invariant — "every path that produced an exec.started produces an exec.finished".

Lane: plain PR (bug fix). No new governance — ADR-0023 (Accepted) already mandates the invariant; the code just didn't honour it on unwind.

How

impl Drop for Driver calls the existing emit_finished() as a backstop:

  • Normal path: no-op — the finished_emitted latch already guarantees emit-exactly-once.
  • Unwind path: emits the owed event with the conventional exit status (127) the spawn-failure path already uses, matching ADR-0023's convention for children without a wire-level status.
  • Runs before the field drops, so the audit record precedes the child teardown that ChildIo's own Drop triggers (the child was already correctly killed on unwind — only the log record was missing).
  • This is the same mechanism ADR-0028 mandates for the Phase-2 abort path ("a drop-guard that emits the owed event"), which will reuse it as-is — first brick of the runtime workstream, not throwaway.

Verification

Three unit tests with a scoped thread-local tracing subscriber capturing event messages:

  1. drop_emits_owed_exec_finished_on_unwind — panic after exec.started → exactly one exec.finished.
  2. drop_after_normal_emission_does_not_duplicate — explicit emission + drop → still one.
  3. drop_without_exec_emits_nothing — no exec → no event.

Dependency note: the tracing dev-dependency gains the std feature (gates tracing::subscriber::with_default) — no new crate enters the tree. cargo deny was not run locally (not installed); the CI gate verifies it.

Local validation: fmt, clippy -D warnings, full test suite (122 passed), release build — all clean.

Refs #109 (runtime workstream), follows the #123 review.

https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa

…it event

ADR-0023 (Accepted) mandates that every path producing an exec.started
produces an exec.finished, but the event was emitted only on the normal
close path (Driver::emit_finished at the end of the drive loop) — a
panic unwinding the connection task between the two events dropped the
Driver without its terminal audit record, leaving exec.started
unmatched in the trail.

impl Drop for Driver now calls emit_finished as a backstop: a no-op on
the normal path (already-emitted latch), and on unwind it emits the
owed event with the conventional exit status the spawn-failure path
already uses. It runs before the field drops, so the record precedes
the child teardown ChildIo's own Drop triggers. This is also the
mechanism ADR-0028 requires for the Phase-2 abort path, which will
reuse it as-is.

Tests capture audit events through a scoped thread-local subscriber
(tracing dev-dependency gains the std feature — no new crate): panic
after exec.started emits exactly one exec.finished; the normal path
does not duplicate; no exec means no event.

Claude-Session: https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa
Signed-off-by: Gonzalo Fleming <[email protected]>
Copilot AI review requested due to automatic review settings July 19, 2026 15:54
@gonzafg2 gonzafg2 added the bug Something isn't working label Jul 19, 2026
@opencode-agent

Copy link
Copy Markdown

Full test suite passes (92 unit + 30 integration = 122 passed). Clean.


Review: fix/channel-drop-guard-exec-finished — Approved. No findings.

This is a surgical, 3-line Drop backstop that directly implements the ADR-0023 audit invariant: "every path that produced an exec.started produces an exec.finished" (line 47). The normal path already honors it at channel.rs:321; the Drop impl catches the unwind path the normal path can't reach.

What was checked

Rule Verdict
No unsafe (CLAUDE.md #1, ADR-0018) Clean — Drop calls a safe &mut self method; tests use standard AssertUnwindSafe wrapping a fully-owned value
Audit fields mandated (CLAUDE.md audit rule, ADR-0024) emit_finished() preserves authenticated_identity, executing_uid, command (structured), exit_status — the same fields the normal path emits
Emit-exactly-once (threat-model §2.7 integrity) finished_emitted latch (line 551) gates the info! call; test #2 verifies no duplication after explicit-emit + drop
No new dependency (CLAUDE.md #4) Only a std feature enable on the existing tracing dev-dependency — same pattern the binary crate already uses (quantumssh/Cargo.toml:26); zero new crates
command as structured field (ADR-0024, threat-model §5.4.3) Uses %command (Display as field value), not string interpolation — unchanged from existing emit_finished(), not introduced by this PR
Threat-model §4.3 (key material never logged) No key material touched — emit_finished() logs only audit metadata
Tests functional, not stub (CLAUDE.md "no placeholder code") Three targeted unit tests with a scoped capture subscriber exercising panic-unwind, normal-then-drop, and no-exec paths

No regressions

  • Drop is a leaf addition — no upstream code path modified, no protocol surface widened, no negotiation path touched.
  • The finished_emitted flag (set before the info! call, line 553) prevents double-emission even if a subscriber panic were to unwind through emit_finished().
  • cargo fmt, cargo clippy -- -D warnings, cargo test --workspace (122 passed), cargo build --release — all clean per the PR author's report and confirmed here.

github run

@gonzafg2 gonzafg2 self-assigned this Jul 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR closes an audit-log gap in quantumssh-core’s channel driver by ensuring the terminal exec.finished audit event is emitted even if the connection task unwinds (panic) before the normal close path completes, preserving the ADR-0023 invariant that every exec.started has exactly one corresponding exec.finished.

Changes:

  • Add a Drop backstop on Driver that calls the existing emit_finished() (still emit-exactly-once via the existing latch).
  • Add unit tests that install a scoped tracing subscriber to count exec.finished events across unwind vs normal drop vs no-exec scenarios.
  • Enable tracing’s std feature for dev builds to use tracing::subscriber::with_default in tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/quantumssh-core/src/channel.rs Adds Drop-guard emission and unit tests to verify exec.finished is not skipped on unwind and not duplicated.
crates/quantumssh-core/Cargo.toml Enables tracing std feature in dev-dependencies for scoped-subscriber-based tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@claude

claude Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@opencode-agent

Copy link
Copy Markdown

Review: fix/exec-finished-drop-guard — Approved. No findings.

The 3-line Drop backstop (channel.rs:209–213) correctly closes the ADR-0023 audit gap: a panic unwind between exec.started and exec.finished previously dropped the Driver without its terminal record, violating the "every started must have a finished" invariant. The Drop impl delegates to the existing emit_finished(), which already has the finished_emitted latch for emit-exactly-once.

Checklist (CLAUDE.md manifest + threat model):

Rule Verdict
No first-party unsafe Clean — Drop calls &mut self method; tests use AssertUnwindSafe wrapping a fully-owned value
Audit fields mandated (ADR-0024) emit_finished() preserves authenticated_identity, executing_uid, command (structured %command), exit_status
Emit-exactly-once finished_emitted set before info!() (line 553) — subscriber panic during emission won't cause duplicate on re-drop
No new dependency (manifest #4) std feature on existing tracing dev-dependency only; zero new crates; matches the binary crate's own usage
Key material never logged (§4.3) Untouched — emit_finished() logs only audit metadata
Tests functional, not stubs Three targeted unit tests: unwind path, normal-then-drop no-duplicate, no-exec no-event
Pre-auth surface unchanged Drop is a post-auth leaf addition
Transport type-state machine unchanged No protocol surface touched

Mechanics are sound:

  • command.clone() at line 550 is infallible (String::clone), so the finished_emitted latch at line 553 is reachable.
  • Drop order is correct: Drop::drop runs before field destruction, so the audit event precedes ChildIo's own teardown Drop.
  • The with_default subscriber guard wraps catch_unwind, so it's still alive when d is dropped during unwind inside the catch.

2 changed files, 102 additions, 0 deletions. No regressions. No governance impact (plain PR, bug fix lane).

github run

@gonzafg2
gonzafg2 merged commit 33eb0cd into main Jul 19, 2026
11 checks passed
@gonzafg2
gonzafg2 deleted the fix/exec-finished-drop-guard branch July 19, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants