Skip to content

Commit 0369df2

Browse files
committed
Always report errors before closing the channel
Otherwise the call to: msg, err := disc.waitMessage(..) may return both msg and err as nil, breaking the contract.
1 parent f305b10 commit 0369df2

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Diff for: client.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,7 @@ func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessag
138138

139139
for {
140140
var msg discoveryMessage
141-
if err := decoder.Decode(&msg); errors.Is(err, io.EOF) {
142-
// This is fine :flames: we exit gracefully
143-
closeAndReportError(nil)
144-
return
145-
} else if err != nil {
141+
if err := decoder.Decode(&msg); err != nil {
146142
closeAndReportError(err)
147143
return
148144
}
@@ -184,7 +180,10 @@ func (disc *Client) waitMessage(timeout time.Duration) (*discoveryMessage, error
184180
select {
185181
case msg := <-disc.incomingMessagesChan:
186182
if msg == nil {
187-
return nil, disc.incomingMessagesError
183+
disc.statusMutex.Lock()
184+
err := disc.incomingMessagesError
185+
disc.statusMutex.Unlock()
186+
return nil, err
188187
}
189188
return msg, nil
190189
case <-time.After(timeout):

0 commit comments

Comments
 (0)