From 41686eb1755ecb9b2d58b6a5f12865293442e0c2 Mon Sep 17 00:00:00 2001 From: Gorka Elexgaray Date: Wed, 10 Feb 2021 14:53:54 +0100 Subject: [PATCH] FIX random crash: Catch ObjectDispose exceptions on EndReceiveFrom --- src/Main/Shared/SystemNetSockets/UdpSocket.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Main/Shared/SystemNetSockets/UdpSocket.cs b/src/Main/Shared/SystemNetSockets/UdpSocket.cs index 0cfa140..63fbaeb 100644 --- a/src/Main/Shared/SystemNetSockets/UdpSocket.cs +++ b/src/Main/Shared/SystemNetSockets/UdpSocket.cs @@ -73,9 +73,22 @@ public System.Threading.Tasks.Task ReceiveAsync() try { _Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, System.Net.Sockets.SocketFlags.None, ref state.EndPoint, - new AsyncCallback((result) => ProcessResponse(state, () => state.Socket.EndReceiveFrom(result, ref state.EndPoint))), state); + new AsyncCallback((result) => ProcessResponse(state, + () => + { + try + { + return state.Socket.EndReceiveFrom(result, ref state.EndPoint); + } + catch (ObjectDisposedException) + { + if (!this.IsDisposed) throw; + } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected. + return 0; + } )) + , state); } - catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected. + catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } #endif