From bc4838578e8e103601280ae7ca89b613e112909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Tue, 13 Aug 2024 22:57:36 +0200 Subject: [PATCH] Handling null client connection while sending server response. --- Wino.Server/ServerContext.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Wino.Server/ServerContext.cs b/Wino.Server/ServerContext.cs index cf2ce174..2334804f 100644 --- a/Wino.Server/ServerContext.cs +++ b/Wino.Server/ServerContext.cs @@ -219,7 +219,20 @@ namespace Wino.Server { MessageConstants.MessageDataTypeKey, message.GetType().Name } }; - await connection.SendMessageAsync(set); + try + { + await connection.SendMessageAsync(set); + } + catch (InvalidOperationException) + { + // Connection might've been disposed during the SendMessageAsync call. + // This is a safe way to handle the exception. + // We don't lock the connection since this request may take sometime to complete. + } + catch (Exception exception) + { + Log.Error(exception, "SendMessageAsync threw an exception"); + } } private void OnConnectionClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)