Skip to content

Commit

Permalink
fix(stream): wrap close done ch with sync.Once (#2537)
Browse files Browse the repository at this point in the history
Today, `stream.Stream` is structured as:
```go
case <-streamer.Done():
  // call Close()
case <-time.After(fetchDelay):
  // call Fetch()
```
The problem is that it's possible that the `Done()` event is triggered at
the same time as `time.After(fetchDelay)` and in that scenario the
behavior is undeterministic.
It's possible that we call `Fetch` a second time. For the ECS deployment
streamer, this means that we would previous close the `s.done` channel
twice. Wrapping the call with a `sync.Once` removes this concern.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
efekarakus authored Jun 28, 2021
1 parent 4212955 commit 3d97212
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/pkg/stream/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ECSDeploymentStreamer struct {
deploymentCreationTime time.Time

subscribers []chan ECSService
once sync.Once
done chan struct{}
isDone bool
pastEventIDs map[string]bool
Expand Down Expand Up @@ -143,7 +144,12 @@ func (s *ECSDeploymentStreamer) Fetch() (next time.Time, err error) {
deployments = append(deployments, rollingDeploy)
if isDeploymentDone(rollingDeploy, s.deploymentCreationTime) {
// The deployment is done, notify that there is no need for another Fetch call beyond this point.
close(s.done)
// In stream.Stream, it's possible that both the <-Done() event is available as well as another Fetch()
// call. In order to guarantee that we don't try to close the same stream multiple times, we wrap it with a
// sync.Once.
s.once.Do(func() {
close(s.done)
})
}
}
var failureMsgs []string
Expand Down

0 comments on commit 3d97212

Please sign in to comment.