Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blob/s3blob: fix data race #3480

Merged
merged 1 commit into from
Aug 29, 2024
Merged

Conversation

arjunnair1997
Copy link
Contributor

w.pr cannot be used to indicate that the pipe has been closed. Instead, we use an atomic.Bool. The problem with using w.pr is that a write to it in the goroutine launched by writer.open can race with a read on w.pr in Close.

Note that it's still possible that w.pr.CloseWithError runs concurrently with w.pr.Close. But this is fine.

Copy link

google-cla bot commented Aug 29, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Contributor

@richardartoul richardartoul left a comment

Choose a reason for hiding this comment

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

LGTM, although wondering if we could just allow w.pr to be closed twice because it looks like thats concurrency safe:

func (p *pipe) closeRead(err error) error {
	if err == nil {
		err = ErrClosedPipe
	}
	p.rerr.Store(err)
	p.once.Do(func() { close(p.done) })
	return nil
}

Copy link
Contributor

@vangent vangent left a comment

Choose a reason for hiding this comment

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

I see the race you are describing, but I don't think this PR completely fixes it. I think it changes the race from "might result in a nil access" to "might result in a double-Close".

It looks like double-Close is actually fine though (https://play.golang.com/p/WvceCRttXVU).

So I think it should be sufficient to drop the "prClosed" entirely, and just remove the "w.pr = nil" line (line 383). WDYT?

@vangent
Copy link
Contributor

vangent commented Aug 29, 2024

Ha, my review raced with @richardartoul :-). Glad we came to the same conclusion though.

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.14%. Comparing base (bb796f8) to head (08ff5df).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3480      +/-   ##
==========================================
- Coverage   73.15%   73.14%   -0.01%     
==========================================
  Files         113      113              
  Lines       14984    14983       -1     
==========================================
- Hits        10961    10960       -1     
  Misses       3260     3260              
  Partials      763      763              

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

w.pr cannot be used to indicate that the pipe has been closed. The problem with
using w.pr is that a write to it in the goroutine launched by writer.open can
race with a read on w.pr in Close.

We just don't write to w.pr in writer.open. The read end of the pipe can still be
closed twice, but that's fine.
@arjunnair1997
Copy link
Contributor Author

arjunnair1997 commented Aug 29, 2024

Yea, I think it could double close the read end of the pipe before my change too, but it's just simpler to not use an atomic.Bool. Updated the pr, please take a look.

@vangent vangent merged commit 2d0cce5 into google:master Aug 29, 2024
6 checks passed
@vangent
Copy link
Contributor

vangent commented Aug 29, 2024

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.

3 participants