Repository navigation
fix(async)!: return a deep copy from container get - #13
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Get handed back the stored slice, map or pointer itself, so every caller shared it with the container and with each other, and writes to what Get returned raced. Get now deep-copies the stored value before the assign and convert paths. BREAKING CHANGE: Container.Get returns a deep copy of the stored value instead of sharing slices, maps and pointed-to data. Unexported struct fields are copied shallowly; map keys, channels, functions and unsafe pointers stay shared. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copying the pointee of types such as *strings.Builder or *bytes.Buffer breaks or duplicates their internal state, so such pointers are now shared as-is. Get copies outside the read lock so writers do not wait on a copy. Document the limits in the README and record the decision in ADR 0001. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Container.Getdocumented that it "copies the stored value", but it assigned (or converted) the stored value shallowly. Slices, maps and pointers were therefore shared between the container and every goroutine that calledGetfor the key, so two readers writing what they received raced: silent corruption for slices,fatal error: concurrent map writesfor maps.Scope:
asynconly, one module. Single PR, not part of a stack; nothing else planned on top. Behavior change for the next MINOR release (v0.2.0).Changes
async/deep_copy.go(new, unexported): reflect-baseddeepCopy. Pointers get a new pointee, except pointers to structs with unexported fields, which are shared as-is (such types own internal state, e.g.*strings.Builder,*bytes.Buffer, mutex holders, that a copy would break); slices a new backing array (len == cap); maps a new map with deep-copied values; arrays, structs (exported fields) and interfaces (dynamic type preserved) are copied element-wise. Nil stays nil. A memo keyed by type + address (+ len/cap for slices) keeps shared references shared in the copy and terminates on cycles. Fast path: types with no pointer, slice, map or interface reachable return unchanged.Getlooks up the value and error under the read lock and copies after releasing it, so a slow copy does not block writers.assignValue: both the assign and convert paths copy first; the slice-to-array length check still runs beforeConvert. JSON fallback, key validation and error texts are unchanged.Get: unexported struct fields (including exported fields promoted from an embedded unexported struct) are copied shallowly (nounsafe), pointers to structs with unexported fields are shared, and map keys, channels, functions and unsafe pointers are shared.Set/TrySetdoc: do not modify a value after storing it.doc.goand README (asyncsection + Compatibility bullet) updated; ADRdocs/adr/0001-async-get-deep-copy.mdrecords the decision.async/async_container_bench_test.go(first commit, so it runs on the baseline too).Tests
Geta stored[]int{0}/map[string][]intand write to the result;DATA RACEbefore the change, clean after, and a laterGetreturns the original content.Getreturned (slice, map of slices, pointer, struct,[]interface{}intointerface{},map[string]interface{}, named slice →[]int,[]int→*[4]int, nil references, scalars) does not change a laterGet. Reference-kind cases failed before the change.[]interface{}), unexported fields, and per-kind unit tests of the copier andhasReferences.Verification (local)
gofmt -l .empty;go vet ./...clean;go test -race -count=1 ./...ok;-fuzz FuzzContainerGetSliceIntoArray -fuzztime 10sok;govulncheck ./...no vulnerabilities. Coverage ofasync: 91%.Benchmarks (
benchstat, n=5)Scalars and flat structs allocate nothing extra; their time rises from walking the type on each
Get(a per-type cache would need package-level state, so it was left out). Reference types pay for the copy, which is the fix. The map copy reuses its key/value holders (SetIterKey/SetIterValue), which tookmap_100from 205 to 7 allocs/op.Release note
Container.Getnow returns a deep copy of the stored value. Code that relied onGetreturning the same slice, map or pointer the producer stored (to observe later writes) must change; unexported struct fields and pointers to structs with unexported fields are still shared.🤖 Generated with Claude Code