Skip to content

Commit

Permalink
Always report errors before closing the channel
Browse files Browse the repository at this point in the history
Otherwise the call to:

   msg, err := disc.waitMessage(..)

may return both msg and err as nil, breaking the contract.
  • Loading branch information
cmaglie committed Sep 13, 2024
1 parent f305b10 commit 0369df2
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessag

for {
var msg discoveryMessage
if err := decoder.Decode(&msg); errors.Is(err, io.EOF) {
// This is fine :flames: we exit gracefully
closeAndReportError(nil)
return
} else if err != nil {
if err := decoder.Decode(&msg); err != nil {
closeAndReportError(err)
return
}
Expand Down Expand Up @@ -184,7 +180,10 @@ func (disc *Client) waitMessage(timeout time.Duration) (*discoveryMessage, error
select {
case msg := <-disc.incomingMessagesChan:
if msg == nil {
return nil, disc.incomingMessagesError
disc.statusMutex.Lock()
err := disc.incomingMessagesError
disc.statusMutex.Unlock()
return nil, err
}
return msg, nil
case <-time.After(timeout):
Expand Down

0 comments on commit 0369df2

Please sign in to comment.