mirror of
https://github.com/fosrl/gerbil.git
synced 2026-03-22 12:54:30 -05:00
[PR #36] fix: relay race condition in WireGuard session management #35
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/fosrl/gerbil/pull/36
Author: @LaurenceJJones
Created: 11/13/2025
Status: 🔄 Open
Base:
main← Head:fix-wg-session-race-condition📝 Commits (3)
ee27bf3Fix race condition in WireGuard session managementa3f9a89Refactor WireGuard session locking and remove unused methodse282715Merge branch 'main' into fix-wg-session-race-condition📊 Changes
1 file changed (+41 additions, -14 deletions)
View changed files
📝
relay/relay.go(+41 -14)📄 Description
Community Contribution License Agreement
By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.
Description
The race condition existed because while sync.Map is thread-safe for map operations (Load, Store, Delete, Range), it does not provide thread-safety for the data stored within it. When WireGuardSession structs were stored as pointers in the sync.Map, multiple goroutines could:
This fix adds a sync.RWMutex to each WireGuardSession struct to protect concurrent access to its fields. All field access now goes through thread-safe methods that properly acquire/release the mutex.
Changes:
How to test?
Understanding the Race Condition
The race condition occurs in these scenarios:
Cleanup goroutine reads
LastSeen: ThecleanupIdleSessions()function periodically readssession.LastSeento check if sessions should be removed.Packet handler updates
LastSeen: ThehandleWireGuardPacket()function updatessession.LastSeen = time.Now()when processing transport data packets.Both happen concurrently: When both operations occur simultaneously on the same session pointer, the race detector detects unsynchronized access to the
LastSeenfield.Notes
An easier fix could be here but I didnt want to change the type against the sync.map in case a pointer was chosen for a reason, in short instead of storing a pointer, storing the struct itself and when updating the any values just replace the key everytime (which you was doing when updating last seen, but since it was a pointer that was unnecessary). May cause some extra garbage collection but in this case cause the struct is tiny would be not noticeable.
let me know if you rather have atomic updates against the sync.map instead of locks on the struct.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.