Skip to content

Commit 5059a92

Browse files
committed
fix: suppress canceled close lock waits
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 92e601e0-3207-42f8-878a-5fc7efae1479
1 parent 4377f32 commit 5059a92

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎src/OpenClaw.Shared/WebSocketClientBase.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff 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",

‎tests/OpenClaw.Shared.Tests/WebSocketCloseSerializationTests.cs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff 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)!

0 commit comments

Comments
 (0)