Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Fix panics in the mtcp-client
Browse files Browse the repository at this point in the history
If the write to reportChan is deferred, and reportChang has already been
closed, then the resulting panic won't be recovered by the deferred
recover-function.
  • Loading branch information
CryptoCopter committed May 21, 2021
1 parent 3c47d4a commit 889e3f1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/cla/mtcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,42 @@ func (client *MTCPClient) Send(bndl bpv7.Bundle) (err error) {
err = fmt.Errorf("MTCPClient.Send: %v", r)
}

// In case of an error, report our failure upstream
if err != nil {
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
}
client.mutex.Unlock()
}()

client.mutex.Lock()
defer client.mutex.Unlock()

connWriter := bufio.NewWriter(client.conn)

buff := new(bytes.Buffer)
if cborErr := cboring.Marshal(&bndl, buff); cborErr != nil {
err = cborErr
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
return
}

if bsErr := cboring.WriteByteStringLen(uint64(buff.Len()), connWriter); bsErr != nil {
err = bsErr
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
return
}

if _, plErr := buff.WriteTo(connWriter); plErr != nil {
err = plErr
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
return
}

if flushErr := connWriter.Flush(); flushErr != nil {
err = flushErr
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
return
}

// Check if the connection is still alive with an empty, unbuffered packet
if probeErr := cboring.WriteByteStringLen(0, client.conn); probeErr != nil {
err = probeErr
client.reportChan <- cla.NewConvergencePeerDisappeared(client, client.GetPeerEndpointID())
return
}

Expand Down

0 comments on commit 889e3f1

Please sign in to comment.