Skip to content

Potential Race Condition with garbageCollectionTicker.Stop() in Serve() Method #3

@beka-birhanu

Description

@beka-birhanu

Description

There appears to be a potential race condition involving s.garbageCollectionTicker.Stop() in the Serve() method. Specifically:

  1. In garbage collection part of the code:

    case <-s.garbageCollectionStop:
        if s.garbageCollectionTicker != nil {
            s.garbageCollectionTicker.Stop()
        }
        break
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions