mock: fix data race in Arguments.Diff for pointer-like arguments#1865
Closed
pavelzeman wants to merge 1 commit intostretchr:masterfrom
Closed
mock: fix data race in Arguments.Diff for pointer-like arguments#1865pavelzeman wants to merge 1 commit intostretchr:masterfrom
pavelzeman wants to merge 1 commit intostretchr:masterfrom
Conversation
b0cfab8 to
47e2cd4
Compare
Arguments.Diff uses fmt.Sprintf("%v") to format arguments for diff output.
When arguments contain pointers, maps, slices, or other reference types that
are concurrently modified by other goroutines, this causes data races:
- For maps: a fatal "concurrent map iteration and map write" error that
crashes the entire test process (non-recoverable)
- For pointers/slices: data races detectable by -race, and potential panics
This commit introduces safeFormatArg() which handles each case:
- Maps: uses %p (address-only) since map iteration cannot be made safe
without external synchronization
- Pointers/slices: attempts %v with panic recovery, falls back to %p
- All other types: unchanged %v behavior (value types, no race possible)
This preserves the full %v diff output for the common case (non-concurrent
arguments, value types, pointers/slices without races) while preventing
testify itself from being the source of data races in user tests.
Fixes stretchr#1597
Related: stretchr#1598 (alternative approach using %p for all pointers)
47e2cd4 to
5f251b6
Compare
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.
Summary
Fixes
Arguments.Diffcausing data races when mock arguments (pointers, maps, slices) are concurrently modified by other goroutines.Problem
Arguments.Diffusesfmt.Sprintf("%v")to format arguments for diff output. This traverses the underlying data structure, which races with concurrent writers:fatal error: concurrent map iteration and map write— crashes the entire test process (non-recoverable byrecover())go test -raceThis is a real-world issue. For example, Mattermost hit this with
*sql.DBarguments — the database connection cleaner goroutine modifies internal fields while testify formats the struct for diff output.Solution
Introduces
safeFormatArg()with targeted handling per kind:Map%p(address only)Ptr,Slice%vwith panic recovery →%pfallback%v(unchanged)This preserves the full
%vdiff output for the common case (non-concurrent arguments, value types, pointers/slices without active races) while preventing testify from being the source of data races or fatal crashes.Tests
Three new race-condition tests:
Test_Arguments_Diff_ConcurrentPointerModification— adapted from mock: avoid data races in Arguments.Diff #1598, pointer struct modified by another goroutineTest_Arguments_Diff_ConcurrentMapModification— raised by @brackendawson in mock: avoid data races in Arguments.Diff #1598 review, map modified by another goroutineTest_Arguments_Diff_ConcurrentSliceModification— slice modified by another goroutineAll three crash/race without the fix and pass cleanly with it.
Alternatives Considered
%pfor allreflect.Ptrkinds. Doesn't cover maps (fatal) or slices. Loses diff output even when arguments aren't being concurrently modified.copyArgument()itself races for maps (iterating to copy triggers the same fatal error). Would require external synchronization that testify can't enforce.MethodCalled: Same map iteration problem, plus significant performance overhead for every mock call.Related
Fixes #1597
Related to #1598 (alternative approach, open since 2024)