← Back to Pull requests

shim/task: make stdio FIFO copying interruptible on darwin #311

Open
unknown opened this pull request with 508 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 13, 2026 22:56 UTC
Author

On macOS, Go keeps FIFOs opened with os.OpenFile off the runtime poller (golang/go#24164). A goroutine parked in read(2) or write(2) on a stdio FIFO then pins an OS thread that Close cannot interrupt, and macOS can also fail to wake a reader when the last writer closes. ioShutdown also never closed any FIFO descriptor, so these goroutines survive a forced teardown.

This adds copyStreamsPolled, selected on darwin only. Linux and Windows copyStreams are unchanged apart from the stdinStreamWriteCloser interface moving to the shared file.

  • Every FIFO is opened O_NONBLOCK and wrapped with os.NewFile, which registers it with the poller, so Close and deadlines interrupt a parked Read or Write. Opens never block: the stdin reader is opened before the shim's write reference, and the output writer retries ENXIO until the client's reader exists.

  • The stdin contract is unchanged: a client closing its own end is a detach, and EOF follows CloseIO only after every writer is gone and buffered bytes were forwarded in order. EOF is found by read(2) returning 0, and once CloseIO was requested the pump re-arms a short read deadline before every read, so it never depends on the last-writer-close wakeup macOS can lose. The re-arm is always a future deadline: a read on an expired one returns without calling read(2) and would drop buffered bytes.

  • copyStreams implementations may return an abort function that ioShutdown calls after its bounded waits to close every FIFO descriptor. An aborted stdin does not send the in-band EOF, so truncated input never looks like a clean end.
  • The new code is compiled everywhere except Windows so Linux CI exercises its logic; only darwin selects it (startCopy). forwardIO gains a forwardIOWith seam so the tests can drive the real ioShutdown against either implementation. Tests use real FIFOs and an in-memory stream stand-in, and cover detach/re-attach, CloseIO ordering, CloseIO before the client closes, forced shutdown of a parked stdin reader and of a writer blocked on a full FIFO, and the lost-wakeup and expired-deadline behaviour with a fake reader.

    Related: containerd/fifo#63 (the O_RDWR variant of the same workaround in the fifo library).

    Testing

    Run on Linux only. macOS execution is unverified; io_copystreams_darwin_test.go and the kqueue behaviour need the macOS CI job (or a Mac).

  • gofmt clean; go vet ./internal/shim/task/... clean for linux, darwin (arm64, amd64) and windows.

  • go test ./internal/shim/task/... and the same with -race pass on Linux; GOOS=darwin go test -c compiles.

  • golangci-lint on the darwin build: 0 issues (the linux run crashed inside staticcheck on go1.27, a tool problem, not a finding).

  • The author also ran seven mutation checks (each break makes the suite fail) and -race -count=10.
  • Unverified on macOS, please look at the CI result:

  • kqueue registration of read-only and write-only FIFO descriptors; if it is refused the open fails loudly rather than falling back.

  • A non-blocking write-only open succeeding while a reader is asleep in open(2).

  • Data and write-space events being reliable; only the last-writer-close event is worked around.
  • Behaviour differences on darwin


  • EOF can take up to 20 ms longer when the client still holds its write end after CloseIO.

  • The output rendezvous can add up to 50 ms if the client's reader attaches late.

  • After a forced teardown a client's stdin write gets EPIPE (before, it succeeded into a leaked reader).
  • Not changed (pre-existing, noticed while testing)


  • Linux: the stdin copy can hang when CloseIO arrives before any client attaches (blocking open in a helper goroutine).

  • When stdout and stderr are the same regular file, the stdout goroutine closes the file under the stderr one.

  • Linux's ioShutdown still cannot release parked copies; the abort hook makes that a small follow-up.
  • AI-assisted (opencode, anthropic/claude-sonnet-5-5, xhigh); reviewed by the submitter's assistant, not yet run on macOS hardware.

    Diff: 508 lines (+381 / -127) across 6 files Reviewer: ericcurtin Review duration: 11206m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 508 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.373
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #311"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.4% · Safe to merge
    25.4%