Skip to content

fix(async)!: return a deep copy from container get - #13

Merged
josuegiron merged 3 commits into
masterfrom
fix/async-get-deep-copy
Nov 21, 2026
Merged

josuegiron merged 3 commits into
masterfrom
fix/async-get-deep-copy

Conversation

@josuegiron

@josuegiron josuegiron commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Container.Get documented 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 called Get for the key, so two readers writing what they received raced: silent corruption for slices, fatal error: concurrent map writes for maps.

Scope: async only, 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-based deepCopy. 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.
  • Get looks 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 before Convert. JSON fallback, key validation and error texts are unchanged.
  • Documented limits on Get: unexported struct fields (including exported fields promoted from an embedded unexported struct) are copied shallowly (no unsafe), pointers to structs with unexported fields are shared, and map keys, channels, functions and unsafe pointers are shared. Set/TrySet doc: do not modify a value after storing it. doc.go and README (async section + Compatibility bullet) updated; ADR docs/adr/0001-async-get-deep-copy.md records the decision.
  • async/async_container_bench_test.go (first commit, so it runs on the baseline too).

Tests

  • Race tests: two goroutines Get a stored []int{0} / map[string][]int and write to the result; DATA RACE before the change, clean after, and a later Get returns the original content.
  • Table test: mutating what Get returned (slice, map of slices, pointer, struct, []interface{} into interface{}, map[string]interface{}, named slice → []int, []int → *[4]int, nil references, scalars) does not change a later Get. Reference-kind cases failed before the change.
  • Cycles (self-referencing pointer and []interface{}), unexported fields, and per-kind unit tests of the copier and hasReferences.
  • Existing tests and the fuzz seed corpus pass unchanged.

Verification (local)

  • gofmt -l . empty; go vet ./... clean; go test -race -count=1 ./... ok; -fuzz FuzzContainerGetSliceIntoArray -fuzztime 10s ok; govulncheck ./... no vulnerabilities. Coverage of async: 91%.

Benchmarks (benchstat, n=5)

Sub-benchmark ns/op before ns/op after B/op after allocs before → after
int 93.7 117.1 1 1 → 1
flat_struct 101.2 208.3 1 1 → 1
slice_1k_int 98.4 5632 8217 1 → 3
map_100 96.1 10679 3576 1 → 7
nested_struct 97.9 1603 488 1 → 10

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 took map_100 from 205 to 7 allocs/op.

Release note

Container.Get now returns a deep copy of the stored value. Code that relied on Get returning 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

josuegiron and others added 3 commits November 21, 2026 20:20
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>
@josuegiron
josuegiron merged commit 7937a44 into master Nov 21, 2026
2 checks passed
@josuegiron
josuegiron deleted the fix/async-get-deep-copy branch November 21, 2026 20:31
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.

1 participant