Skip to content

Commit df234ab

Browse files
committed
- Crash fix for using invalid / already disposed cancellation token
1 parent 39728c2 commit df234ab

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

GenOnlineService/Constants.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,13 @@ public async Task SendAsync(byte[] buffer, WebSocketMessageType messageType, Can
11531153
}
11541154
*/
11551155

1156-
using var cts = CancellationTokenSource.CreateLinkedTokenSource(externalToken);
1157-
cts.CancelAfter(TimeSpan.FromMilliseconds(500));
1156+
// Do not link to externalToken: if the parent CTS fires while the linked
1157+
// CTS is being disposed by the 'using' block, the parent callback fires on
1158+
// an already-disposed object -> ObjectDisposedException. The loop guard in
1159+
// TickWebsocket already checks the token, so a point-in-time check here is enough.
1160+
if (externalToken.IsCancellationRequested)
1161+
return;
1162+
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
11581163
await m_SockInternal.SendAsync(buffer, messageType, true, cts.Token);
11591164
}
11601165
catch
@@ -1171,7 +1176,7 @@ public async Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDes
11711176
try
11721177
{
11731178
// dont wait forever, certain situations can cause that in ASP.NET
1174-
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
1179+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
11751180
await m_SockInternal.CloseAsync(closeStatus, statusDescription, cts.Token);
11761181
}
11771182
catch

0 commit comments

Comments
 (0)