Skip to content

Commit

Permalink
fix: NetworkClient.Disconnect avoids NullReferenceException if Transp…
Browse files Browse the repository at this point in the history
…ort ClientDisconnect calls OnDisconnected immediately. Related to: #2353
  • Loading branch information
miwarnec committed Oct 21, 2020
1 parent 71dc3a2 commit d3f1ee9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,14 @@ public static void Disconnect()
if (connection != null)
{
connection.Disconnect();
connection.Dispose();
// connection.Disconnect calls Transport.ClientDisconnect.
// if transport calls Mirror's OnDisconnected event immediately
// before returning, then we end up in a circular call:
// https://github.com/vis2k/Mirror/issues/2353
// in which case connection becomes null after returning here.
// in other words, the following Dispose call needs to use '.?'
// to avoid NullReferenceExceptions.
connection?.Dispose();
connection = null;
RemoveTransportHandlers();
}
Expand Down

0 comments on commit d3f1ee9

Please sign in to comment.