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
go/common: Make DefaultSink thread-safe #12485
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
Changelog[uncommitted] (2023-03-24)Bug Fixes
|
This was referenced Mar 23, 2023
Frassle
reviewed
Mar 24, 2023
diag.DefaultSink is not safe for concurrent use. Whether we use it with os.File or with a bytes.Buffer, both targets do not synchronize their writes, so this leaves room for interleaving of messages. Making diag.DefaultSink thread-safe is generally desirable, but is also a requirement for #12438 so that it can run upgrades concurrently and print warnings as tasks fail. This makes the result of DefaultSink thread-safe by adding a mutex around the targeted stdout and stderr. Note: As a special case, when stdout and stderr are the same we want to use the same mutex for them. This matches the behavior of [os/exec.Cmd][1] // If Stdout and Stderr are the same writer, and have a type that can // be compared with ==, at most one goroutine at a time will call Write. Stdout io.Writer Stderr io.Writer [1]: https://pkg.go.dev/os/exec#Cmd A writer implementation can be anything, and is not guaranteed to be comparable with `==`. When that happens, the `==` will panic. To prevent regressions, we have to handle that case and treat uncomparable writers as different. This is also similar to how os/exec does it: https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/os/exec/exec.go;l=475
abhinav
force-pushed
the
abhinav/diag-sink-race
branch
from
March 24, 2023 15:34
a89e761
to
bae69f4
Compare
Frassle
approved these changes
Mar 24, 2023
bors merge |
bors bot
added a commit
that referenced
this pull request
Mar 24, 2023
12485: go/common: Make DefaultSink thread-safe r=abhinav a=abhinav diag.DefaultSink is not safe for concurrent use. Whether we use it with os.File or with a bytes.Buffer, both targets do not synchronize their writes, so this leaves room for interleaving of messages. Making diag.DefaultSink thread-safe is generally desirable, but is also a requirement for #12438 so that it can run upgrades concurrently and print warnings as tasks fail. This makes the result of DefaultSink thread-safe by adding a mutex around the targeted stdout and stderr. Note: As a special case, when stdout and stderr are the same we want to use the same mutex for them. This matches the behavior of [os/exec.Cmd][1] // If Stdout and Stderr are the same writer, and have a type that can // be compared with ==, at most one goroutine at a time will call Write. Stdout io.Writer Stderr io.Writer [1]: https://pkg.go.dev/os/exec#Cmd A writer implementation can be anything, and is not guaranteed to be comparable with `==`. When that happens, the `==` will panic. To prevent regressions, we have to handle that case and treat uncomparable writers as different. This is also similar to how os/exec does it: https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/os/exec/exec.go;l=475 Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
Build failed: |
bors retry |
bors bot
added a commit
that referenced
this pull request
Mar 24, 2023
12485: go/common: Make DefaultSink thread-safe r=abhinav a=abhinav diag.DefaultSink is not safe for concurrent use. Whether we use it with os.File or with a bytes.Buffer, both targets do not synchronize their writes, so this leaves room for interleaving of messages. Making diag.DefaultSink thread-safe is generally desirable, but is also a requirement for #12438 so that it can run upgrades concurrently and print warnings as tasks fail. This makes the result of DefaultSink thread-safe by adding a mutex around the targeted stdout and stderr. Note: As a special case, when stdout and stderr are the same we want to use the same mutex for them. This matches the behavior of [os/exec.Cmd][1] // If Stdout and Stderr are the same writer, and have a type that can // be compared with ==, at most one goroutine at a time will call Write. Stdout io.Writer Stderr io.Writer [1]: https://pkg.go.dev/os/exec#Cmd A writer implementation can be anything, and is not guaranteed to be comparable with `==`. When that happens, the `==` will panic. To prevent regressions, we have to handle that case and treat uncomparable writers as different. This is also similar to how os/exec does it: https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/os/exec/exec.go;l=475 Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
Build failed: |
bors retry |
Build succeeded: |
This was referenced Mar 24, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
diag.DefaultSink is not safe for concurrent use.
Whether we use it with os.File or with a bytes.Buffer,
both targets do not synchronize their writes,
so this leaves room for interleaving of messages.
Making diag.DefaultSink thread-safe is generally desirable,
but is also a requirement for #12438
so that it can run upgrades concurrently
and print warnings as tasks fail.
This makes the result of DefaultSink thread-safe
by adding a mutex around the targeted stdout and stderr.
Note:
As a special case, when stdout and stderr are the same
we want to use the same mutex for them.
This matches the behavior of os/exec.Cmd
A writer implementation can be anything,
and is not guaranteed to be comparable with
==
.When that happens, the
==
will panic.To prevent regressions, we have to handle that case
and treat uncomparable writers as different.
This is also similar to how os/exec does it:
https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/os/exec/exec.go;l=475