From 58ae29d1ebf1da9ecaed6a18d487a961901cd1a4 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 16 Dec 2020 10:18:05 +0100 Subject: [PATCH] Small fixes after #2087 - Add guard against cancellationToken being null - Use cancellationToken for all webSocket operations - Use no cancellation token for semaphore release - Log TaskCanceledException on user-debugging level --- ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs b/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs index b7076eb87..e8984fcd1 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs @@ -50,6 +50,10 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [ProducesResponseType(typeof(IEnumerable>), (int) HttpStatusCode.OK)] [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)] public async Task NLogGet(CancellationToken cancellationToken) { + if (cancellationToken == null) { + throw new ArgumentNullException(nameof(cancellationToken)); + } + if (HttpContext == null) { throw new InvalidOperationException(nameof(HttpContext)); } @@ -82,7 +86,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { WebSocketReceiveResult result = await webSocket.ReceiveAsync(Array.Empty(), cancellationToken).ConfigureAwait(false); if (result.MessageType != WebSocketMessageType.Close) { - await webSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "You're not supposed to be sending any message but Close!", CancellationToken.None).ConfigureAwait(false); + await webSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "You're not supposed to be sending any message but Close!", cancellationToken).ConfigureAwait(false); break; } @@ -93,13 +97,15 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } } finally { if (ActiveLogWebSockets.TryRemove(webSocket, out SemaphoreSlim? closedSemaphore)) { - await closedSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); // Ensure that our semaphore is truly closed by now + await closedSemaphore.WaitAsync(CancellationToken.None).ConfigureAwait(false); // Ensure that our semaphore is truly closed by now closedSemaphore.Dispose(); } } + } catch (TaskCanceledException e) { + ASF.ArchiLogger.LogGenericDebuggingException(e); } catch (WebSocketException e) { ASF.ArchiLogger.LogGenericDebuggingException(e); - } catch (TaskCanceledException) { } + } return new EmptyResult(); }