diff --git a/protocol/connections/peerapp.go b/protocol/connections/peerapp.go index bfc2c3e..17ace0c 100644 --- a/protocol/connections/peerapp.go +++ b/protocol/connections/peerapp.go @@ -153,7 +153,21 @@ func (pa *PeerApp) SendMessage(message model2.PeerMessage) error { } if err == nil { - return pa.connection.Send(serialized) + err = pa.connection.Send(serialized) + + // at this point we have tried to send a message to a peer only to find that something went wrong. + // we don't know *what* went wrong - the most likely explanation is the peer went offline in the time between + // sending the message and it arriving in the engine to be sent. Other explanations include problems with Tor, + // a dropped wifi connection. + // Regardless, we error out this message and close this peer app assuming it cannot be used again. + // We expect that cwtch will eventually recreate this connection and the app. + if err != nil { + // close any associated sockets + pa.connection.Close() + // tell cwtch this connection is no longer valid + pa.OnClose(err.Error()) + } + return err } return err }