Skip to content

Conversation

@dwisiswant0
Copy link
Member

Proof

patch:

$ go test -v -race -run "^TestSyncLockMapConcurrent$/^Get$" ./maps/
=== RUN   TestSyncLockMapConcurrent
=== RUN   TestSyncLockMapConcurrent/Get
--- PASS: TestSyncLockMapConcurrent (0.11s)
    --- PASS: TestSyncLockMapConcurrent/Get (0.11s)
PASS
ok  	github.com/projectdiscovery/utils/maps	1.128s

main:

$ git cherry-pick de8deb0
$ go test -v -race -run "^TestSyncLockMapConcurrent$/^Get$" ./maps/
=== RUN   TestSyncLockMapConcurrent
=== RUN   TestSyncLockMapConcurrent/Get
==================
WARNING: DATA RACE
Read at 0x00c000130458 by goroutine 11:
  github.com/projectdiscovery/utils/maps.(*SyncLockMap[go.shape.string,go.shape.string]).triggerCleanupIfNeeded()
      /home/dw1/Development/PD/utils/maps/synclock_map.go:76 +0x74
  github.com/projectdiscovery/utils/maps.(*SyncLockMap[go.shape.string,go.shape.string]).Get()
      /home/dw1/Development/PD/utils/maps/synclock_map.go:184 +0x287
  github.com/projectdiscovery/utils/maps.TestSyncLockMapConcurrent.func1.1()
      /home/dw1/Development/PD/utils/maps/synclock_map_test.go:354 +0xea

Previous write at 0x00c000130458 by goroutine 18:
  github.com/projectdiscovery/utils/maps.(*SyncLockMap[go.shape.string,go.shape.string]).triggerCleanupIfNeeded()
      /home/dw1/Development/PD/utils/maps/synclock_map.go:81 +0xd8
  github.com/projectdiscovery/utils/maps.(*SyncLockMap[go.shape.string,go.shape.string]).Get()
      /home/dw1/Development/PD/utils/maps/synclock_map.go:184 +0x287
  github.com/projectdiscovery/utils/maps.TestSyncLockMapConcurrent.func1.1()
      /home/dw1/Development/PD/utils/maps/synclock_map_test.go:354 +0xea

Goroutine 11 (running) created at:
  github.com/projectdiscovery/utils/maps.TestSyncLockMapConcurrent.func1()
      /home/dw1/Development/PD/utils/maps/synclock_map_test.go:351 +0x153
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1934 +0x21c
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1997 +0x44

Goroutine 18 (running) created at:
  github.com/projectdiscovery/utils/maps.TestSyncLockMapConcurrent.func1()
      /home/dw1/Development/PD/utils/maps/synclock_map_test.go:351 +0x153
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1934 +0x21c
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1997 +0x44
==================
    testing.go:1617: race detected during execution of test
--- FAIL: TestSyncLockMapConcurrent (0.11s)
    --- FAIL: TestSyncLockMapConcurrent/Get (0.11s)
FAIL
FAIL	github.com/projectdiscovery/utils/maps	0.129s
FAIL

The race was detected when multiple goroutines
concurrently called `(*SyncLockMap[K, V]).Get()`
or `(*SyncLockMap[K, V]).Set()`, each triggering
`(*SyncLockMap[K, V]).triggerCleanupIfNeeded()`
which had unsynchronized access to the [time.Time]
field.

Fixes #704

Signed-off-by: Dwi Siswanto <[email protected]>
@dwisiswant0 dwisiswant0 merged commit 2bf5582 into main Nov 19, 2025
9 of 11 checks passed
@dwisiswant0 dwisiswant0 deleted the dwisiswant0/fix/mapsutil/data-race-in-SyncLockMap-cleanup branch November 19, 2025 15:03
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.

mapsutil: Data race in (*SyncLockMap[K, V]).triggerCleanupIfNeeded() causes sporadic test failures

3 participants