]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Flush outgoing packets before shutting closing socket
authorMatthew Endsley <mendsley@gmail.com>
Thu, 4 Feb 2021 21:54:15 +0000 (13:54 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Thu, 4 Feb 2021 21:55:01 +0000 (13:55 -0800)
This ensures we at least attempt to send out Disconnect packets to
connected clients when shutting down. Prior to this the send thread
would race the Dispose thread causing indeterminate behavior. Sometimes
disconnect packets would be sent out, sometimes they wouldn't.

Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs

index 46a723a6555286cd4316d82f2f39627f3f5db9d8..84ac5a44eca0642fccaf39765a2719d99db76756 100644 (file)
@@ -366,20 +366,25 @@ namespace Hazel.Udp.FewerThreads
                 kvp.Value.Dispose();
             }
 
+            bool wasActive = this.isActive;
+            this.isActive = false;
+
+            // Flush outgoing packets
+            this.sendQueue?.CompleteAdding();
+            if (wasActive)
+            {
+                this.sendThread.Join();
+            }
+
             try { this.socket.Shutdown(SocketShutdown.Both); } catch { }
             try { this.socket.Close(); } catch { }
             try { this.socket.Dispose(); } catch { }
 
-            bool wasActive = this.isActive;
-            this.isActive = false;
-
             this.receiveQueue?.CompleteAdding();
-            this.sendQueue?.CompleteAdding();
 
             if (wasActive)
             {
                 this.reliablePacketThread.Join();
-                this.sendThread.Join();
                 this.receiveThread.Join();
                 this.processThreads.Join();
             }
@@ -388,7 +393,6 @@ namespace Hazel.Udp.FewerThreads
             this.receiveQueue = null;
             this.sendQueue?.Dispose();
             this.sendQueue = null;
-
         }
 
         public void Dispose()