-
Notifications
You must be signed in to change notification settings - Fork 11
fix(kvm): close browser websocket immediately on AMT disconnect #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -215,7 +215,13 @@ func (uc *UseCase) ListenToDevice(deviceConnection *DeviceConnection) { | |||||
| conn := deviceConnection.Conn | ||||||
|
|
||||||
| defer func() { | ||||||
| // Clean up on exit | ||||||
| // Notify the browser immediately so the UI updates without waiting for | ||||||
| // ListenToBrowser to unblock on its ReadMessage call. | ||||||
| _ = deviceConnection.Conn.WriteMessage( | ||||||
| websocket.CloseMessage, | ||||||
| websocket.FormatCloseMessage(websocket.CloseNormalClosure, "AMT session ended"), | ||||||
| ) | ||||||
| _ = deviceConnection.Conn.Close() | ||||||
| deviceConnection.cancel() | ||||||
| }() | ||||||
|
|
||||||
|
|
@@ -650,7 +656,7 @@ func writeLength(buf *bytes.Buffer, challenge *client.AuthChallenge, response st | |||||
| return ErrLengthLimit // If total length is too large, throws an error and stops here | ||||||
| } | ||||||
|
|
||||||
| length := uint32(totalLength) //nolint:gosec // Ignore potential integer overflow here as overflow is validated earlier in code | ||||||
| length := uint32(totalLength) // overflow validated above | ||||||
|
||||||
| length := uint32(totalLength) // overflow validated above | |
| length := uint32(totalLength) //nolint:gosec // overflow validated above via explicit bounds check |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,15 +2,22 @@ package devices | |||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "bytes" | ||||||||||||||
| "context" | ||||||||||||||
| "errors" | ||||||||||||||
| "io" | ||||||||||||||
| "math" | ||||||||||||||
| "strings" | ||||||||||||||
| "testing" | ||||||||||||||
| "time" | ||||||||||||||
|
|
||||||||||||||
| "github.com/gorilla/websocket" | ||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||
|
|
||||||||||||||
| "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman" | ||||||||||||||
| "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/client" | ||||||||||||||
|
|
||||||||||||||
| "github.com/device-management-toolkit/console/internal/entity" | ||||||||||||||
| "github.com/device-management-toolkit/console/pkg/logger" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| func TestProcessBrowserData(t *testing.T) { | ||||||||||||||
|
|
@@ -834,3 +841,84 @@ func TestRandomValueHexErrorCase(t *testing.T) { | |||||||||||||
| require.NoError(t, err) | ||||||||||||||
| require.Empty(t, result) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // spyRedirection is a minimal Redirection stub whose RedirectListen returns | ||||||||||||||
| // an error to simulate AMT dropping the TCP connection. | ||||||||||||||
| type spyRedirection struct { | ||||||||||||||
| listenErr error | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyRedirection) SetupWsmanClient(_ entity.Device, _, _ bool) (wsman.Messages, error) { | ||||||||||||||
| return wsman.Messages{}, nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyRedirection) RedirectConnect(_ context.Context, _ *DeviceConnection) error { return nil } | ||||||||||||||
| func (s *spyRedirection) RedirectClose(_ context.Context, _ *DeviceConnection) error { return nil } | ||||||||||||||
| func (s *spyRedirection) RedirectSend(_ context.Context, _ *DeviceConnection, _ []byte) error { | ||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyRedirection) RedirectListen(_ context.Context, _ *DeviceConnection) ([]byte, error) { | ||||||||||||||
| return nil, s.listenErr | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // spyWebSocketConn is a minimal spy that records WriteMessage and Close calls | ||||||||||||||
| // without importing the mocks package (which would create an import cycle for | ||||||||||||||
| // the internal test package). | ||||||||||||||
| type spyWebSocketConn struct { | ||||||||||||||
| writeMessageCalled bool | ||||||||||||||
| writeMessageType int | ||||||||||||||
| closeCalled bool | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyWebSocketConn) WriteMessage(messageType int, _ []byte) error { | ||||||||||||||
| s.writeMessageCalled = true | ||||||||||||||
| s.writeMessageType = messageType | ||||||||||||||
|
|
||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyWebSocketConn) ReadMessage() (messageType int, p []byte, err error) { | ||||||||||||||
| return 0, nil, errors.New("spy: not reading") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (s *spyWebSocketConn) Close() error { | ||||||||||||||
| s.closeCalled = true | ||||||||||||||
|
|
||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func TestListenToDeviceClosesWebSocketOnAMTDisconnect(t *testing.T) { | ||||||||||||||
| t.Parallel() | ||||||||||||||
|
|
||||||||||||||
| spy := &spyWebSocketConn{} | ||||||||||||||
| ctx, cancel := context.WithCancel(context.Background()) | ||||||||||||||
|
|
||||||||||||||
| deviceConnection := &DeviceConnection{ | ||||||||||||||
| Conn: spy, | ||||||||||||||
| Mode: "kvm", | ||||||||||||||
| Device: entityDevice(), | ||||||||||||||
| ctx: ctx, | ||||||||||||||
| cancel: cancel, | ||||||||||||||
| healthTicker: time.NewTicker(HeartbeatInterval), | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| t.Cleanup(func() { | |
| cancel() | |
| deviceConnection.healthTicker.Stop() | |
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In ListenToDevice, the function writes device data using the locally captured
conn := deviceConnection.Conn, but the new deferred cleanup closesdeviceConnection.Conndirectly. SincedeviceConnection.Conncan be updated elsewhere (e.g., reconnect path), this can lead to closing a different WebSocket than the one used for writes (and potentially leaving the original connection open). Consider using a single, consistent reference for both writing and cleanup (e.g., closeconn, or access/updateConnunder a lock and always use the current value).