-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Description
There appears to be a potential race condition involving s.garbageCollectionTicker.Stop() in the Serve() method. Specifically:
-
In garbage collection part of the code:
case <-s.garbageCollectionStop: if s.garbageCollectionTicker != nil { s.garbageCollectionTicker.Stop() } break
-
In serve:
if s.heartbeatExpiration > 0 { if s.garbageCollectionTicker != nil { s.garbageCollectionTicker.Stop() } s.garbageCollectionTicker = time.NewTicker(s.heartbeatExpiration) s.garbageCollectionStop = make(chan bool, 1) go s.clientGarbageCollection() }
Problem
It seems there is no guarantee that s.garbageCollectionTicker.Stop() from the first snippet will not run after s.garbageCollectionTicker is renewed in the second snippet. This could lead to unintended behavior such as stopping the newly created ticker instead of the old one.
Suggestion
To ensure proper behavior, we may need synchronization mechanisms (e.g., a mutex) or a different design pattern to guarantee that Stop() is called on the intended ticker instance. Or Call Stop() directly in the Serve() method at the point where we can guarantee it is executed before renewing the ticker.