Skip to content

Commit cec496e

Browse files
committed
Fix possible panic in websocket handling
1 parent daa9991 commit cec496e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

ship/websocket.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ func (w *websocketConnection) close() {
173173
w.shipWriteChannel = nil
174174
}
175175

176-
w.mux.Unlock()
177-
178176
if w.conn != nil {
179177
w.conn.Close()
180178
}
181179

182180
w.isConnectionClosed = true
181+
182+
w.mux.Unlock()
183183
})
184184
}
185185

@@ -193,10 +193,13 @@ func (w *websocketConnection) InitDataProcessing(dataProcessing ShipDataProcessi
193193

194194
// write a message to the websocket connection
195195
func (w *websocketConnection) WriteMessageToDataConnection(message []byte) error {
196-
if w.conn == nil {
196+
if w.conn == nil || w.shipWriteChannel == nil || w.isConnClosed() {
197197
return errors.New("connection is not initialized")
198198
}
199199

200+
w.mux.Lock()
201+
defer w.mux.Unlock()
202+
200203
w.shipWriteChannel <- message
201204
return nil
202205
}

0 commit comments

Comments
 (0)