Skip to content

Commit

Permalink
fix: KcpConnection.Disconnect sets open=false BEFORE calling OnDiscon…
Browse files Browse the repository at this point in the history
…nected to prevent deadlocks like MirrorNetworking/Mirror#2357
  • Loading branch information
vis2k committed Oct 22, 2020
1 parent 88c1de4 commit 9a1a82e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,27 @@ public void Handshake()
/// </summary>
public virtual void Disconnect()
{
// do nothing if already closed
if (!open) return;

// set as closed BEFORE calling OnDisconnected event.
// this avoids potential deadlocks like this Mirror bug:
// https://github.com/vis2k/Mirror/issues/2357
// where OnDisconnected->Mirror.OnDisconnected->ServerDisconnect->
// Connection.Disconnect->Kcp.Disconnect->OnDisconnected->
// DEADLOCK
open = false;

// send a disconnect message and disconnect
if (open && socket.Connected)
if (socket.Connected)
{
try
{
Send(Goodby);
kcp.Flush();

// set as not open

// call OnDisconnected event, even if we manually
// disconnected
OnDisconnected?.Invoke();
Expand All @@ -270,10 +283,6 @@ public virtual void Disconnect()
// were disconnected
}
}
open = false;

// EOF is now available
//dataAvailable?.TrySetResult();
}

// get remote endpoint
Expand Down

0 comments on commit 9a1a82e

Please sign in to comment.