Skip to content

Commit fff7e34

Browse files
committed
Add error handling while proxying ADB with WebSocket
Ignore errors that is expected to be happened - WebSocket / TCP closure while executing io.Copy(...)
1 parent c1971ae commit fff7e34

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

frontend/src/liboperator/operator/operator.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net/url"
2828
"os"
2929
"strconv"
30+
"strings"
3031

3132
apiv1 "github.com/google/android-cuttlefish/frontend/src/liboperator/api/v1"
3233
"github.com/gorilla/mux"
@@ -540,10 +541,17 @@ func adbProxy(w http.ResponseWriter, r *http.Request, pool *DevicePool) {
540541

541542
// Redirect WebSocket to ADB tcp socket
542543
go func() {
543-
io.Copy(wsWrapper, tcpConn)
544-
wsWrapper.Close()
544+
// TODO: Replace with checking net.ErrClosed after Go 1.16
545+
if _, err := io.Copy(wsWrapper, tcpConn); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
546+
log.Print("Error while io.Copy from ADB to WebSocket: ", err)
547+
}
548+
if err = wsWrapper.Close(); err != nil {
549+
log.Print("Error while closing WebSocket: ", err)
550+
}
545551
}()
546-
io.Copy(tcpConn, wsWrapper)
552+
if _, err = io.Copy(tcpConn, wsWrapper); err != nil {
553+
log.Print("Error while io.Copy from WebSocket to ADB: ", err)
554+
}
547555
}
548556

549557
// Wrapper for implementing io.ReadWriteCloser of websocket.Conn
@@ -561,6 +569,9 @@ func (w *wsIoWrapper) Read(p []byte) (int, error) {
561569
if w.buf == nil || w.pos >= len(w.buf) {
562570
_, readBuf, err := w.wsConn.ReadMessage()
563571
if err != nil {
572+
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
573+
return 0, io.EOF
574+
}
564575
return 0, err
565576
}
566577
w.buf = readBuf

0 commit comments

Comments
 (0)