Skip to content

panic: send on closed channel in progress messenger during oras copy #2125

Closed

Description

@TerryHowe

What happened in your environment?

oras copy can crash with panic: send on closed channel in the progress/display code. Reported in #2101 (see this comment) while running a plain oras copy docker.io/library/nginx:1.31.2 localhost:5000/nginx:1.31.2.

panic: send on closed channel

goroutine 69 [running]:
oras.land/oras/cmd/oras/internal/display/status/progress.(*messenger).Fail(...)
	oras.land/oras/cmd/oras/internal/display/status/progress/messenger.go:46 +0x74
oras.land/oras/internal/progress.(*readTracker).Read(...)
	oras.land/oras/internal/progress/tracker.go:105 +0xf3
io.discard.ReadFrom(...)
io.copyBuffer(...)
net/http.(*transferWriter).doBodyCopy(...)
net/http.(*transferWriter).writeBody(...)
net/http.(*Request).write(...)
net/http.(*persistConn).writeLoop(...)
	net/http/transport.go:2600 +0x174
created by net/http.(*Transport).dialConn

Root cause

Use-after-close race on the progress channel:

  • Each tracked blob wraps its reader via progress.TrackReader (internal/progress/tracker.go). Reads drive readTracker.Read, which on a non-EOF error calls tracker.Fail(err) (internal/progress/tracker.go:107-111).
  • tracker.Fail → messenger.Fail does an unguarded send: m.update <- updateStatusError(err) (cmd/oras/internal/display/status/progress/messenger.go:46). messenger.Update (lines 31/39) has the same exposure.
  • The channel is closed by messenger.Close() (messenger.go:56), invoked from Reader.StopTracker() (track/reader.go:69) via defer r.StopTracker() in referenceGraphTarget.PushReference/Push (track/target.go:102) — this fires as soon as the push call returns.
  • The race: net/http's persistConn.writeLoop goroutine keeps reading the request-body reader (the tracked reader) after the round-trip returned and Close() already closed the channel. When that late read hits EOF/error, Fail sends on the closed channel → panic. The stack trace confirms exactly this path (writeLoop → doBodyCopy → readTracker.Read → messenger.Fail).

The messenger struct already has a closed bool field (messenger.go:23), but it only guards Close() itself — not Update/Fail, and it is not synchronized.

Affected code

  • cmd/oras/internal/display/status/progress/messenger.go:44-48 — Fail: the unguarded m.update <- that panics. Primary fix site.
  • cmd/oras/internal/display/status/progress/messenger.go:28-42 — Update: same exposure (blocking sends at lines 31, 39).
  • cmd/oras/internal/display/status/progress/messenger.go:50-59 — Close: closes the channel; closed flag is unsynchronized.
  • internal/progress/tracker.go:96-113 — readTracker.Read: calls tracker.Fail(err) from the net/http writeLoop goroutine — the trigger.
  • internal/progress/tracker.go:150-168 — writeTracker.Write: analogous Fail path; a fix should cover it too.
  • cmd/oras/internal/display/status/track/reader.go:67-76 — StopTracker/StopManager: where tracker.Close() ends the channel's lifetime.
  • cmd/oras/internal/display/status/track/target.go:97-111 — PushReference with defer r.StopTracker().
  • cmd/oras/internal/display/status/progress/manager.go:125-136 — newTracker: single drain goroutine + bufferSize=1; the concurrency context a fix must respect.

Suggested approach

  1. Make late sends no-ops instead of panics. Add a done chan struct{} to messenger; Close closes done. In Update and Fail, wrap every send in:
    select {
    case m.update <- upd:
    case <-m.done:
        // tracker already closed; drop the update
    }
    This avoids both the panic and a lock-across-blocking-send deadlock. (The StateTransmitting case already uses a non-blocking select; extend the same discipline to the blocking cases and to Fail.)
  2. Ensure Close ordering/idempotency so a concurrent Fail/Update cannot send after close(m.update) — close done first, then drain.
  3. Add a -race regression test in messenger_test.go: hammer Fail/Update from a goroutine while the main goroutine calls Close, assert no panic.

Notes / risk

  • All internal packages — no public API impact, no backward-compat concerns.
  • Real risk is concurrency correctness: bufferSize = 1 with a single drain goroutine (manager.go), so a naive mutex held across a blocking send can deadlock against Close. Prefer the done-channel select over a mutex.
  • Coverage gap: the progress package has tests but none exercise the post-close send race.

Context

Split out from #2101. That issue also reported a separate hang (oras copy --from-oci-layout stalling after 100% with an EOF on the blob PUT), which appears to be a registry/transport-timeout or oras-go retry behavior rather than a CLI bug. This issue tracks only the send on closed channel panic (part B of the analysis in #2101).

What is the version of your ORAS CLI?

Reproduced against 1.3.2 / main. Panic path present in current main.

Are you willing to submit PRs to fix it?

  • Yes, I am willing to fix it.

Activity

  1. added
    help wanted
    on Aug 5, 2026
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

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions