pulumi/sdk/go/common
Abhinav Gupta bae69f4c7c
go/common: Make DefaultSink thread-safe
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
2023-03-24 08:34:35 -07:00
..
apitype all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
constant Send update metadata about update plans 2022-10-12 22:55:59 +01:00
diag go/common: Make DefaultSink thread-safe 2023-03-24 08:34:35 -07:00
display Moving previewDigest to sdk/go/common/display, and exporting it. (#9886) 2022-06-27 09:08:06 -05:00
encoding all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
env Handle recursive types 2023-01-13 11:28:13 -08:00
resource all: Fix revive issues 2023-03-21 08:55:11 -07:00
testing sdk/common: Add testing/diagtest package 2023-03-13 12:48:24 -07:00
tokens all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
tools all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
util all: Fix revive issues 2023-03-21 08:55:11 -07:00
version move pkg/version -> sdk/go/common/version 2020-03-18 15:25:25 -07:00
workspace Merge #12461 2023-03-21 17:46:13 +00:00