Commit 19f2f56
fix: Add mutex protection to prevent data races in datasource cache (#433)
### Problem
The `CachedDatasource` implementation contained a data race condition
where multiple goroutines could concurrently access and modify the
shared `cache` map without proper synchronization. This could lead to
unpredictable behavior, potential crashes, or data corruption in
high-concurrency environments.
The bug has been reproduced by adding a unit test for this scenario and
running the tests using the `-race` option to `go test` command as shown
below:
```sh
❯ go test -race ./pkg/...
? github.com/grafana/github-datasource/pkg [no test files]
? github.com/grafana/github-datasource/pkg/dfutil [no test files]
? github.com/grafana/github-datasource/pkg/errors [no test files]
ok github.com/grafana/github-datasource/pkg/github 1.372s
ok github.com/grafana/github-datasource/pkg/github/client (cached)
ok github.com/grafana/github-datasource/pkg/github/projects 1.572s
? github.com/grafana/github-datasource/pkg/httputil [no test files]
? github.com/grafana/github-datasource/pkg/models [no test files]
==================
WARNING: DATA RACE
Write at 0x00c00039a7b0 by goroutine 33:
runtime.mapaccess2()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/internal/runtime/maps/runtime_swiss.go:117 +0x2dc
github.com/grafana/github-datasource/pkg/plugin.(*CachedDatasource).saveCache()
/Users/dgiagio/Devel/github-datasource/pkg/plugin/datasource_caching.go:89 +0x148
github.com/grafana/github-datasource/pkg/plugin.TestWithCaching.func2()
/Users/dgiagio/Devel/github-datasource/pkg/plugin/datasource_caching_test.go:32 +0xc4
testing.tRunner()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/testing/testing.go:1792 +0x180
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/testing/testing.go:1851 +0x40
Previous write at 0x00c00039a7b0 by goroutine 32:
runtime.mapaccess2()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/internal/runtime/maps/runtime_swiss.go:117 +0x2dc
github.com/grafana/github-datasource/pkg/plugin.(*CachedDatasource).saveCache()
/Users/dgiagio/Devel/github-datasource/pkg/plugin/datasource_caching.go:89 +0x148
github.com/grafana/github-datasource/pkg/plugin.TestWithCaching.func1()
/Users/dgiagio/Devel/github-datasource/pkg/plugin/datasource_caching_test.go:24 +0xc4
testing.tRunner()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/testing/testing.go:1792 +0x180
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.24.1/libexec/src/testing/testing.go:1851 +0x40
```
### Changes
This PR adds proper concurrency handling to the `CachedDatasource`
struct:
- Added a read-write mutex (`sync.RWMutex`) field to protect concurrent
access to the cache map
- Implemented appropriate locking in the following methods:
- `getCache`: Uses read lock (`RLock`/`RUnlock`) for read-only access
- `saveCache`: Uses write lock (`Lock`/`Unlock`) when modifying the
cache
- `Cleanup`: Uses write lock when removing expired entries
### Testing
Added comprehensive unit tests in `datasource_caching_test.go` that
verify concurrent access safety:
- Concurrent reads from empty cache
- Concurrent writes to and reads from the cache
- Multiple concurrent reads from the cache
These tests ensure the cache operations remain thread-safe under
concurrent access patterns.
### Impact
This change improves stability and reliability of the GitHub datasource
plugin by eliminating potential race conditions, making it more robust
in high-concurrency environments.
---------
Co-authored-by: Zoltán Bedi <[email protected]>1 parent 8852825 commit 19f2f56
File tree
3 files changed
+150
-1
lines changed- .changeset
- pkg/plugin
3 files changed
+150
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
| |||
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
58 | 64 | | |
59 | 65 | | |
60 | 66 | | |
| |||
77 | 83 | | |
78 | 84 | | |
79 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
80 | 89 | | |
81 | 90 | | |
82 | 91 | | |
| |||
277 | 286 | | |
278 | 287 | | |
279 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
280 | 292 | | |
281 | 293 | | |
282 | 294 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
0 commit comments