fix(storage): Make Writer thread-safe.#12753
Merged
tritone merged 1 commit intogoogleapis:mainfrom Aug 22, 2025
Merged
Conversation
storage.Writer took an assumption that CloseWithError() could be called more than once, and was thread-safe with respect to concurrent Write(), Flush(), and Close() calls. This was not honored in the refactor in googleapis#12422. Modify Writer so that it is thread-safe to provide these behaviors, and support repeated Close() and CloseWithError() calls. To address this, we start the sender goroutine earlier, and gather the first buffer in that goroutine. It's possible that some workloads which gather less than one buffer worth of data with a sequence of small writes will observe a performance hit here, since those writes used to be direct copies but will now be a channel ping-pong. If that's an issue, it could be improved by wrapping the buffer in a mutex and doing more explicit concurrency control.
tritone
approved these changes
Aug 22, 2025
gcf-merge-on-green bot
pushed a commit
that referenced
this pull request
Sep 23, 2025
🤖 I have created a release *beep* *boop* --- ## [1.57.0](https://togithub.com/googleapis/google-cloud-go/compare/storage/v1.56.1...storage/v1.57.0) (2025-09-23) ### Features * **storage/control:** Add new GetIamPolicy, SetIamPolicy, and TestIamPermissions RPCs ([d73f912](https://togithub.com/googleapis/google-cloud-go/commit/d73f9123be77bb3278f48d510cd0fb22feb605bc)) * **storage:** Post support dynamic key name ([#12677](https://togithub.com/googleapis/google-cloud-go/issues/12677)) ([9e761f9](https://togithub.com/googleapis/google-cloud-go/commit/9e761f961a2c4351b3e0793ed655314ac5853903)) * **storage:** WithMeterProvider allows custom meter provider configuration ([#12668](https://togithub.com/googleapis/google-cloud-go/issues/12668)) ([7f574b0](https://togithub.com/googleapis/google-cloud-go/commit/7f574b01e0b454c1ef5c13e6a58075e394ee990d)) ### Bug Fixes * **storage:** Free buffers in Bidi Reader ([#12839](https://togithub.com/googleapis/google-cloud-go/issues/12839)) ([bc247fd](https://togithub.com/googleapis/google-cloud-go/commit/bc247fdc3f5234a8bd6934e58d5b0b578f1335cb)) * **storage:** Make Writer thread-safe. ([#12753](https://togithub.com/googleapis/google-cloud-go/issues/12753)) ([9ea380b](https://togithub.com/googleapis/google-cloud-go/commit/9ea380bea5b980a9054d201be4f315a195da2182)) * **storage:** No progress report for oneshot write ([#12746](https://togithub.com/googleapis/google-cloud-go/issues/12746)) ([b97c286](https://togithub.com/googleapis/google-cloud-go/commit/b97c286ec369a10a81b1a8a3a1aae18b46d2dfbc)) ### Performance Improvements * **storage:** Pipeline gRPC writes ([#12422](https://togithub.com/googleapis/google-cloud-go/issues/12422)) ([1f2c5fe](https://togithub.com/googleapis/google-cloud-go/commit/1f2c5fe2843724302086fe04cb8dab8b515969c5)) --- This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
This file contains hidden or 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
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.
storage.Writer took an assumption that CloseWithError() could be called more than once, and was thread-safe with respect to concurrent Write(), Flush(), and Close() calls. This was not honored in the refactor in #12422.
Modify Writer so that it is thread-safe to provide these behaviors, and support repeated Close() and CloseWithError() calls.
To address this, we start the sender goroutine earlier, and gather the first buffer in that goroutine. It's possible that some workloads which gather less than one buffer worth of data with a sequence of small writes will observe a performance hit here, since those writes used to be direct copies but will now be a channel ping-pong. If that's an issue, it could be improved by wrapping the buffer in a mutex and doing more explicit concurrency control. Throughput for large writes is
not affected.
Fixes #12752