Skip to content

Fix concurrent-safe route registration (data race in addRoute/Routes)#4524

Open
mehrdadbn9 wants to merge 5 commits intogin-gonic:masterfrom
mehrdadbn9:fix/issue-4457-concurrent-safe-routes-final
Open

Fix concurrent-safe route registration (data race in addRoute/Routes)#4524
mehrdadbn9 wants to merge 5 commits intogin-gonic:masterfrom
mehrdadbn9:fix/issue-4457-concurrent-safe-routes-final

Conversation

@mehrdadbn9
Copy link

This fixes a data race between `addRoute()` and `Routes()` functions where one goroutine appends to `trees` slice while another reads from it.

Root Cause:

  • `addRoute()` reads `engine.trees` to check if method exists, then appends to the slice
  • `Routes()` iterates over `engine.trees` to collect all routes
  • When these run concurrently, it causes a data race

Solution:

  • Added `mu sync.RWMutex` field to `Engine` struct
  • `addRoute()` uses `Lock()` to ensure exclusive write access
  • `Routes()` uses `RLock()` to allow concurrent reads

Testing:

  • Original repro test from issue passes without race detector warnings
  • Full test suite passes with `-race` flag

Related Code:

  • `http.ServeMux` uses similar pattern with `sync.RWMutex` for concurrent safety

Fixes: #4457

@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.27%. Comparing base (3dc1cd6) to head (a7b6562).
⚠️ Report is 246 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4524      +/-   ##
==========================================
+ Coverage   99.21%   99.27%   +0.06%     
==========================================
  Files          42       46       +4     
  Lines        3182     3042     -140     
==========================================
- Hits         3157     3020     -137     
+ Misses         17       15       -2     
+ Partials        8        7       -1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 99.27% <100.00%> (?)
-tags go_json 99.20% <100.00%> (?)
-tags nomsgpack 99.26% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.24 99.27% <100.00%> (?)
go-1.25 99.27% <100.00%> (?)
macos-latest 99.27% <100.00%> (+0.06%) ⬆️
ubuntu-latest 99.27% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mehrdadbn9 mehrdadbn9 force-pushed the fix/issue-4457-concurrent-safe-routes-final branch 3 times, most recently from ad9a991 to 3af7291 Compare February 6, 2026 11:05
@mehrdadbn9
Copy link
Author

"Hi maintainers! 👋

All CI checks are now passing:

  • ✅ Lint
  • ✅ All tests (Ubuntu/macOS, Go 1.24/1.25, including race detector)
  • ✅ Codecov/project
  • ✅ CodeQL
  • ✅ Trivy Security Scan\n\nThis PR fixes a data race between `addRoute()` and `Routes()` by adding proper mutex synchronization. The fix has been tested with the race detector enabled.\n\nWould appreciate a review when you have time. Thank you for your work on gin! 🙏"

@mehrdadbn9 mehrdadbn9 force-pushed the fix/issue-4457-concurrent-safe-routes-final branch 2 times, most recently from 11248ab to 718ce2f Compare February 13, 2026 09:12
- Add sync.RWMutex to Engine struct for thread-safe access
- Protect addRoute() with Lock() for exclusive write access
- Protect Routes() with RLock() for concurrent read access
- Add concurrent test to verify no data race
- Add tests for serveError function coverage

Fixes gin-gonic#4457
@mehrdadbn9 mehrdadbn9 force-pushed the fix/issue-4457-concurrent-safe-routes-final branch from 5c399c1 to edff091 Compare February 15, 2026 05:28
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