mock: reduce data races in Arguments.Diff for pointer-like arguments
Summary
Fixes Arguments.Diff causing data races when mock arguments (pointers, maps) are concurrently modified by other goroutines.
Problem
Arguments.Diff uses fmt.Sprintf("%v") to format arguments for diff output. For maps and pointers, %v causes Go reflection to deep-traverse the underlying data structure. When another goroutine concurrently modifies that same map or pointer, this produces:
- Maps: fatal error: concurrent map iteration and map write (crashes the entire test process, unrecoverable)
- Pointers: race detector failures under go test -race
This is a real-world issue. Mattermost hit this with *sql.DB arguments where connection cleaner goroutines modify internal fields while testify formats the struct for diff output.
Solution
Introduces formatArg() helper that uses %p (address only) for pointer and map types, bypassing deep-traversal entirely. Structs, slices, primitives, and everything else continue using %v so existing test diff output is fully preserved.
Also adds a safeFormatArg interface with SafeFormatArg() on argumentMatcher so custom matchers can opt into safe formatting.
Type coverage:
- Map: %p (address only) to avoid fatal concurrent iteration
- Ptr: %p (address only) to avoid races on pointed-to data
- Everything else: %v (unchanged) since value types cannot race
Tests
All existing 90+ tests continue to pass.
Related
- Incomplete fix for #1866
- Prior art: #1865 (closed, different approach)
Thanks for the review! Addressed all 3 points:
- Removed the
safeFormatArginterface - replaced with direct type assertion for argumentMatcher since we only need one implementation - Added Slice and Chan to the %%p formatting (they were missing alongside Map/Pointer per your note)
- Added test coverage with 4 new tests verifying safe formatting behavior
formatArg now cleanly handles Map, Ptr, Slice, and Chan with %%p, all other types with %%v. The interface is gone.
CI is green on all 4 commits. All 3 review points addressed:
- Interface removed (commit fb9485c) -safeFormatArg interface is gone, replaced with direct type assertion for argumentMatcher
- Tests added -Test_Arguments_Diff_RaceSafeFormatting, _Slice, _Pointer, and _formatArg verify the fix works correctly
- Slice + Chan handled (commit fb9485c) -formatArg now uses %p for reflect.Slice and reflect.Chan too
Ready for re-review. @dolmen -can you take another look?
Unless I'm wrong your PR try to fix
- https://github.com/stretchr/testify/issues/1597
You should mention in in your PR description
Thanks for the detailed review, you're right, the previous tests only validated formatArg in isolation and didn't actually exercise the Arguments.Diff path.
I've added three regression tests that directly call MethodCalled (which triggers findExpectedCall -> Arguments.Diff -> formatArg) with concurrently modified pointer/slice/map arguments:
- Test_CallMockWithConcurrentlyModifiedPointerArg
- Test_CallMockWithConcurrentlyModifiedSliceArg
- Test_CallMockWithConcurrentlyModifiedMapArg
All three pass with go test -race.
If someone removes the formatArg call from Diff in the future, these tests will fail (unlike the old formatArg-only tests). This matches the pattern from #1598 that you referenced.
I'm unsure how this PR differs from #1598 that was opened by @petergardfjall and that was discussed with some interesting feedback from @brackendawson
- https://github.com/stretchr/testify/pull/1598
@catfishgg you didn't reply to this.
Your PR is very close to #1598
Maybe you opened things without seeing #1598 existed, or you wanted to reactivate the idea of fixing something that is broken.
You're right, both PRs tackle the same issue. The difference is scope - #1598 handles pointers only, while #1895 covers pointers, maps, slices, and channels.
On the AI slop point - fair call. I've pushed a cleanup that removes the assert.NotPanics noise and trims the over-explanatory comments. Tests still pass with -race. The concurrent regression tests are what actually matter here.