From 889e3f1ac334022980a822c049ecee827afe61bb Mon Sep 17 00:00:00 2001 From: Markus Sommer Date: Fri, 21 May 2021 14:50:25 +0200 Subject: [PATCH] Fix panics in the mtcp-client 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. --- pkg/cla/mtcp/client.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/cla/mtcp/client.go b/pkg/cla/mtcp/client.go index 8972094f..a5b4c358 100644 --- a/pkg/cla/mtcp/client.go +++ b/pkg/cla/mtcp/client.go @@ -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 }