Skip to content

fix: avoid send on closed channel in progress messenger - #2126

Merged
TerryHowe merged 1 commit into
oras-project:mainfrom
siddartha018:fix-progress-messenger-send-on-closed-channel
Aug 8, 2026
Merged

TerryHowe merged 1 commit into
oras-project:mainfrom
siddartha018:fix-progress-messenger-send-on-closed-channel

Conversation

@siddartha018

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

oras copy can panic with send on closed channel in the progress display.

The race, precisely:

  • messenger.Close() did m.update <- updateStatusEndTime(); close(m.update). It is called from Reader.StopTracker(), which runs via defer r.StopTracker() in referenceGraphTarget.PushReference / Push — i.e. as soon as the push call returns.
  • The tracked reader handed to the push is the HTTP request body. net/http's persistConn.writeLoop goroutine keeps reading it after the round trip has returned. When one of those late reads returns a non-EOF error, readTracker.Read calls tracker.Fail(err), which was an unguarded m.update <- updateStatusError(err).
  • So the closing goroutine (the caller of PushReference) and the sending goroutine (net/http writeLoop) are unsynchronized, and a send can land after the close. That is exactly the reported stack: writeLoop → doBodyCopy → readTracker.Read → messenger.Fail. writeTracker.Write has the same Fail path.

The existing closed bool only short-circuited a second Close(); it was unsynchronized and did not guard Update/Fail.

The fix: the messenger no longer closes the update channel at all. It owns a done channel that Close closes (once, via sync.Once), every blocking send selects on done, and the drain goroutine in manager.newTracker now stops on done after applying whatever is still buffered. A late Update/Fail therefore takes the done branch and drops the update instead of panicking. Close sends the end-time update before closing done, so the final status is never lost.

Note on the approach: adding a select on done while still closing the update channel would not have fixed it. select commits to a ready send case and then performs the send, so a concurrent close in that window still panics. Not closing the channel is what actually serializes the two goroutines — the only cross-goroutine transition left is close(done), and a closed done is permanently ready, so senders can neither panic nor block. A mutex held across the blocking send was rejected as it couples sender liveness to the drain goroutine.

Reproduced before the fix with go test -race (drain goroutine + a goroutine calling Fail while Close runs):

==================
WARNING: DATA RACE
Read at 0x00c00013b6d0 by goroutine 9:
  runtime.chansend1()
  oras.land/oras/cmd/oras/internal/display/status/progress.(*messenger).Fail()
      cmd/oras/internal/display/status/progress/messenger.go:46 +0xa4
...
panic: send on closed channel

Test_messenger_Close_concurrentUpdate is the regression test for it; it fails if the channel close is put back.

Verified: go build ./..., go vet ./..., go test -race ./... (all packages pass), golangci-lint run ./... (0 issues).

Which issue(s) this PR fixes:
Fixes #2125

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update? — no, all internal packages, no user-facing or API change
  • Does this introduce breaking changes that would require an announcement or bumping the major version? — no
  • Do all new files have an appropriate license header? — no new files

messenger.Close() closed the update channel, but the tracked reader that
feeds Update/Fail can outlive the call that closes the tracker. The most
common case is the net/http transport write loop, which keeps reading the
request body after the round trip returns: PushReference closes the tracker
via defer r.StopTracker(), then a late read hits an error, calls
readTracker.Fail, and sends on the now-closed channel, panicking the
process.

Stop closing the update channel. The messenger now owns a done channel that
Close closes, every blocking send selects on it, and the drain goroutine
uses it to stop after applying whatever is still buffered. Late updates are
dropped instead of panicking, and Close stays idempotent via sync.Once.

Selecting on done while still closing the update channel would not have been
enough: select picks a ready send case before the send happens, so a close
in that window still panics.

Signed-off-by: harshasiddartha <[email protected]>

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@codecov

codecov Bot commented Aug 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.40%. Comparing base (9b2b5cd) to head (7843825).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2126      +/-   ##
==========================================
+ Coverage   87.36%   87.40%   +0.04%     
==========================================
  Files         139      139              
  Lines        5526     5544      +18     
==========================================
+ Hits         4828     4846      +18     
  Misses        419      419              
  Partials      279      279              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TerryHowe
TerryHowe merged commit 3648cfb into oras-project:main Aug 8, 2026
9 checks passed
@TerryHowe

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants