File tree Expand file tree Collapse file tree
tests/OpenClaw.Shared.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -474,11 +474,26 @@ protected async Task CloseWebSocketAsync()
474474 if ( ws ? . State != WebSocketState . Open )
475475 return ;
476476
477- await _sendLock . WaitAsync ( _cts . Token ) ;
477+ try
478+ {
479+ await _sendLock . WaitAsync ( _cts . Token ) ;
480+ }
481+ catch ( OperationCanceledException )
482+ {
483+ // Shutdown canceled the wait; no close ownership was acquired.
484+ return ;
485+ }
486+ catch ( ObjectDisposedException )
487+ {
488+ // Send lock or lifetime token was disposed during shutdown.
489+ return ;
490+ }
491+
478492 try
479493 {
480494 if ( ws . State == WebSocketState . Open )
481495 {
496+ // Preserve normal graceful close; concurrent Dispose aborts the socket and callers contain that failure.
482497 await ws . CloseAsync (
483498 WebSocketCloseStatus . NormalClosure ,
484499 "Disconnecting" ,
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ public async Task CloseWebSocketAsync_WaitsForInFlightSend()
4747 }
4848
4949 [ Fact ]
50- public async Task CloseWebSocketAsync_DisposeCancelsWaitBehindInFlightSend ( )
50+ public async Task CloseWebSocketAsync_DisposeCompletesQueuedCloseWithoutException ( )
5151 {
5252 using var server = new LoopbackWebSocketServer ( ) ;
5353 await server . StartAsync ( ) ;
@@ -68,7 +68,8 @@ public async Task CloseWebSocketAsync_DisposeCancelsWaitBehindInFlightSend()
6868 client . Dispose ( ) ;
6969
7070 await sendTask . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
71- await WaitForCloseCompletionAsync ( closeTask ) ;
71+ await closeTask . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
72+ Assert . True ( closeTask . IsCompletedSuccessfully ) ;
7273 }
7374 finally
7475 {
@@ -99,14 +100,6 @@ await socket.CloseOutputAsync(
99100 }
100101 }
101102
102- private static async Task WaitForCloseCompletionAsync ( Task closeTask )
103- {
104- var closeCompletion = await Task . WhenAny ( closeTask , Task . Delay ( TimeSpan . FromSeconds ( 2 ) ) ) ;
105- Assert . Same ( closeTask , closeCompletion ) ;
106- if ( closeTask . IsFaulted )
107- Assert . IsAssignableFrom < OperationCanceledException > ( closeTask . Exception ! . GetBaseException ( ) ) ;
108- }
109-
110103 private static SemaphoreSlim GetSendLock ( WebSocketClientBase client ) =>
111104 ( SemaphoreSlim ) typeof ( WebSocketClientBase )
112105 . GetField ( "_sendLock" , BindingFlags . Instance | BindingFlags . NonPublic ) !
You can’t perform that action at this time.
0 commit comments