Skip to content

Commit

Permalink
Shut down WebSocket after write error
Browse files Browse the repository at this point in the history
Co-authored-by: Maciek Sakrejda <[email protected]>
  • Loading branch information
seanlinsley and msakrejda committed Nov 21, 2024
1 parent 451d13f commit de98ea7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions runner/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,14 @@ func connect(ctx context.Context, server *state.Server, globalCollectionOpts sta
for {
select {
case <-connCtx.Done():
socket := server.WebSocket.Swap(nil)
if socket != nil {
err = socket.Close()
if err != nil {
logger.PrintWarning("Error closing websocket: %s", err)
}
}
closeConnection(server, logger)
return
case snapshot := <-server.SnapshotStream:
logger.PrintVerbose("Uploading snapshot to websocket")
err = conn.WriteMessage(websocket.BinaryMessage, snapshot)
if err != nil {
logger.PrintError("Error uploading snapshot: %s", err)
cancelConn()
closeConnection(server, logger)
return
}
}
Expand Down Expand Up @@ -158,3 +152,13 @@ func connect(ctx context.Context, server *state.Server, globalCollectionOpts sta
}
}()
}

func closeConnection(server *state.Server, logger *util.Logger) {
socket := server.WebSocket.Swap(nil)
if socket != nil {
err := socket.Close()
if err != nil {
logger.PrintWarning("Error closing websocket: %s", err)
}
}
}

0 comments on commit de98ea7

Please sign in to comment.