Skip to content

Commit

Permalink
Fix race condition for adding new channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nish Krishnan committed Nov 12, 2021
1 parent 0f9b435 commit 60e3d59
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions server/handlers/project_command_output_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,22 @@ func (p *AsyncProjectCommandOutputHandler) clearLogLines(pull string) {
}

func (p *AsyncProjectCommandOutputHandler) addChan(ch chan string, pull string) {
p.receiverBuffersLock.Lock()
if p.receiverBuffers[pull] == nil {
p.receiverBuffers[pull] = map[chan string]bool{}
}
p.receiverBuffers[pull][ch] = true
p.receiverBuffersLock.Unlock()

p.projectOutputBuffersLock.RLock()
buffer := p.projectOutputBuffers[pull]
p.projectOutputBuffersLock.RUnlock()

for _, line := range buffer {
ch <- line
}

// add the channel to our registry after we backfill the contents of the buffer,
// to prevent new messages coming in interleaving with this backfill.
p.receiverBuffersLock.Lock()
if p.receiverBuffers[pull] == nil {
p.receiverBuffers[pull] = map[chan string]bool{}
}
p.receiverBuffers[pull][ch] = true
p.receiverBuffersLock.Unlock()
}

//Add log line to buffer and send to all current channels
Expand Down

0 comments on commit 60e3d59

Please sign in to comment.