Repository navigation
Description
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 drivereadTracker.Read, which on a non-EOF error callstracker.Fail(err)(internal/progress/tracker.go:107-111). tracker.Fail→messenger.Faildoes 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 fromReader.StopTracker()(track/reader.go:69) viadefer r.StopTracker()inreferenceGraphTarget.PushReference/Push(track/target.go:102) — this fires as soon as the push call returns. - The race:
net/http'spersistConn.writeLoopgoroutine keeps reading the request-body reader (the tracked reader) after the round-trip returned andClose()already closed the channel. When that late read hitsEOF/error,Failsends 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 unguardedm.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;closedflag is unsynchronized.internal/progress/tracker.go:96-113—readTracker.Read: callstracker.Fail(err)from thenet/httpwriteLoop goroutine — the trigger.internal/progress/tracker.go:150-168—writeTracker.Write: analogousFailpath; a fix should cover it too.cmd/oras/internal/display/status/track/reader.go:67-76—StopTracker/StopManager: wheretracker.Close()ends the channel's lifetime.cmd/oras/internal/display/status/track/target.go:97-111—PushReferencewithdefer 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
- Make late sends no-ops instead of panics. Add a
done chan struct{}tomessenger;Closeclosesdone. InUpdateandFail, wrap every send in:This avoids both the panic and a lock-across-blocking-send deadlock. (Theselect { case m.update <- upd: case <-m.done: // tracker already closed; drop the update }
StateTransmittingcase already uses a non-blockingselect; extend the same discipline to the blocking cases and toFail.) - Ensure
Closeordering/idempotency so a concurrentFail/Updatecannot send afterclose(m.update)— closedonefirst, then drain. - Add a
-raceregression test inmessenger_test.go: hammerFail/Updatefrom a goroutine while the main goroutine callsClose, assert no panic.
Notes / risk
- All internal packages — no public API impact, no backward-compat concerns.
- Real risk is concurrency correctness:
bufferSize = 1with a single drain goroutine (manager.go), so a naive mutex held across a blocking send can deadlock againstClose. Prefer thedone-channelselectover 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