Skip to content

bug: status.Render mutates the status under a read lock (data race between concurrent renders) #2289

Closed

Description

@wleeaf

What happened in your environment?

status.Render in cmd/oras/internal/display/status/progress/status.go holds only s.lock.RLock(), but it writes to the status. It advances the spinner (s.mark.symbol() increments s.mark), and calculateSpeed() appends to the speed window (s.speed.Add, which updates point, next and size).

manager.render() also runs under a read lock (m.lock.RLock()), and it is called from two places: the ticker goroutine, and Track on the caller's goroutine. When several blobs start at once (copy/push/pull run with concurrency 3 by default), several render() calls can run at the same time and render the same status concurrently. The race detector reports this as a data race on status.mark and on the speedWindow fields.

Besides the race report, a lost update on w.size++ can push size past the window's capacity. After that, size never equals len(w.point) again, and Mean() can compute a negative begin index.

What did you expect to happen?

Rendering a status should be safe when render() runs concurrently. Render mutates the status, so it should take the write lock, like the update* functions in the same file.

How can we reproduce it?

Add this test to cmd/oras/internal/display/status/progress and run go test -race -run Test_concurrentTrack -count=20 .:

type lockedConsole struct {
	*mockConsole
	lock sync.Mutex
}

func (c *lockedConsole) NewRow() { c.lock.Lock(); defer c.lock.Unlock(); c.mockConsole.NewRow() }
func (c *lockedConsole) OutputTo(u uint, s string) {
	c.lock.Lock()
	defer c.lock.Unlock()
	c.mockConsole.OutputTo(u, s)
}

func Test_concurrentTrack(t *testing.T) {
	m := newManager(&lockedConsole{mockConsole: newMockConsole(80, 24)}, nil)
	var wg sync.WaitGroup
	for i := range 50 {
		wg.Go(func() {
			tr, err := m.Track(ocispec.Descriptor{MediaType: "application/octet-stream", Size: 10, Digest: digest.FromString(fmt.Sprint(i))})
			if err != nil {
				t.Error(err)
				return
			}
			_ = tr.Close()
		})
	}
	wg.Wait()
	if err := m.Close(); err != nil {
		t.Error(err)
	}
}

The wrapper is only there because the mock console isn't safe for concurrent use (the real console writes straight to the terminal file). On main, WARNING: DATA RACE between two (*status).Render calls (spinner.symbol / speedWindow.Add) appears in most runs (16 of 20 here).

What is the version of your ORAS CLI?

Built from main at 9218890.

What is your OS environment?

Ubuntu 24.04

Are you willing to submit PRs to fix it?

  • Yes, I am willing to fix it.

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions