Skip to content

[soundness] hy-check(channel_liveness): use-after-close, double-close, use-after-drain, and double-drain bypass linear SPSC channel typestate lifecycle invariants (#4200 regression) #4346

Open

Description

@seenorht

Summary

In compiler/crates/hy-check/src/channel_liveness.rs, SPSC channel capabilities (hy.send_cap[T], hy.recv_cap[T]) maintain a ChannelTypestate enum (Fresh, Active, Closed, Drained, Moved). However, transitions from terminal states (Closed and Drained) are entirely unverified in operation check-sites:

  1. Use-After-Close (Method Send): Calling tx.send(val) after tx.close() is silently accepted. In Go/Hybrid runtime semantics, sending on a closed channel produces a fatal runtime panic (panic: send on closed channel).
  2. Use-After-Close (Statement Send): tx <- val and select { case tx <- val: } check only is_moved(&cap), ignoring tx_state == ChannelTypestate::Closed. They silently accept sending on a closed capability.
  3. Double-Close: Calling tx.close() a second time on an already closed send capability is silently accepted. In Go/Hybrid runtime semantics, closing an already closed channel produces a fatal runtime panic (panic: close of closed channel).
  4. Use-After-Drain (Method Recv): Calling rx.recv() after rx.drain() is silently accepted.
  5. Use-After-Drain (Expression Receive): <-rx and select { case <-rx: } check only is_moved(&cap), ignoring rx_state == ChannelTypestate::Drained. They silently accept receiving from a drained capability.
  6. Double-Drain: Calling rx.drain() a second time on an already drained receive capability is silently accepted.

This directly violates GUARANTEES.md Row 13 ("Concurrency and Sendability") and SPEC §10 (Track W-026, #4200), which requires static typestate enforcement preventing runtime channel lifecycle panics.

Target Commit

Commit 7cf29833 (upstream/main, Session 183).

Minimal Repro 1: Send After Close (tx.close() then tx.send())

fn main() {
    tx, rx := hy.pipe[int]()
    tx.close()
    tx.send(42)
    rx.drain()
}

Expected: Rejected (HYB-E3001: cannot send on closed linear capability 'tx').
Actual: Accepted (verdict Accept, produces runtime panic on emitted Go code).

Minimal Repro 2: Statement Send After Close (tx.close() then tx <- 42)

fn main() {
    tx, rx := hy.pipe[int]()
    tx.close()
    tx <- 42
    rx.drain()
}

Expected: Rejected (HYB-E3001: cannot send on closed linear send capability 'tx').
Actual: Accepted (verdict Accept).

Minimal Repro 3: Double-Close (tx.close() twice)

fn main() {
    tx, rx := hy.pipe[int]()
    tx.close()
    tx.close()
    rx.drain()
}

Expected: Rejected (HYB-E4004: linear send capability 'tx' was already closed).
Actual: Accepted (verdict Accept, produces runtime panic on emitted Go code).

Minimal Repro 4: Receive After Drain (rx.drain() then rx.recv())

fn main() {
    tx, rx := hy.pipe[int]()
    tx.close()
    rx.drain()
    v := rx.recv()
    _ = v
}

Expected: Rejected (HYB-E3001: cannot receive from drained linear capability 'rx').
Actual: Accepted (verdict Accept).

Proposed Fix

In compiler/crates/hy-check/src/channel_liveness.rs:

  1. Add is_tx_closed(&self, name: &str) -> bool and is_rx_drained(&self, name: &str) -> bool helpers.
  2. In check_stmt (Stmt::Send and Stmt::Select send/recv arms), reject operations when capability is Closed or Drained.
  3. In check_expr (Expr::Call for close, drain, send, recv), enforce typestate preconditions before updating state:
    • close: verify !is_tx_closed, emit HYB-E4004 on double-close.
    • drain: verify !is_rx_drained, emit HYB-E4005 on double-drain.
    • send: verify !is_tx_closed, emit HYB-E3001 on send-after-close.
    • recv: verify !is_rx_drained, emit HYB-E3001 on recv-after-drain.
  4. In Expr::Receive, verify !is_rx_drained, emit HYB-E3001 on recv-after-drain.

Companion PR with full regression test battery in channel_liveness_4200.rs will be opened shortly.

Activity

  1. added 5 commits that reference this issue on Nov 10, 2026
    ffa3b51
    a1813d2
    d020b07
    cb2b55d
    9a6b372
  2. added a commit that references this issue on Nov 19, 2026
    8242ccb
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions