Skip to content

Commit ff3075a

Browse files
fix
Fixes the issue where NetworkManager would not clean up if an exception was thrown while it was starting.
1 parent 6f48d4f commit ff3075a

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,17 @@ public bool StartServer()
13231323
}
13241324
ConnectionManager.LocalClient.ClientId = ServerClientId;
13251325

1326-
Initialize(true);
1326+
try
1327+
{
1328+
Initialize(true);
1329+
}
1330+
catch (Exception ex)
1331+
{
1332+
Debug.LogException(ex);
1333+
// Always shutdown to assure everything is cleaned up
1334+
ShutdownInternal();
1335+
return false;
1336+
}
13271337

13281338
try
13291339
{
@@ -1342,11 +1352,13 @@ public bool StartServer()
13421352

13431353
ConnectionManager.TransportFailureEventHandler(true);
13441354
}
1345-
catch (Exception)
1355+
catch (Exception ex)
13461356
{
1357+
Debug.LogException(ex);
1358+
// Always shutdown to assure everything is cleaned up
1359+
ShutdownInternal();
13471360
ConnectionManager.LocalClient.SetRole(false, false);
13481361
IsListening = false;
1349-
throw;
13501362
}
13511363

13521364
return IsListening;
@@ -1373,7 +1385,16 @@ public bool StartClient()
13731385
return false;
13741386
}
13751387

1376-
Initialize(false);
1388+
try
1389+
{
1390+
Initialize(false);
1391+
}
1392+
catch (Exception ex)
1393+
{
1394+
Debug.LogException(ex);
1395+
ShutdownInternal();
1396+
return false;
1397+
}
13771398

13781399
try
13791400
{
@@ -1419,7 +1440,18 @@ public bool StartHost()
14191440
return false;
14201441
}
14211442

1422-
Initialize(true);
1443+
try
1444+
{
1445+
Initialize(true);
1446+
}
1447+
catch (Exception ex)
1448+
{
1449+
Debug.LogException(ex);
1450+
// Always shutdown to assure everything is cleaned up
1451+
ShutdownInternal();
1452+
return false;
1453+
}
1454+
14231455
try
14241456
{
14251457
IsListening = NetworkConfig.NetworkTransport.StartServer();
@@ -1437,6 +1469,8 @@ public bool StartHost()
14371469
catch (Exception ex)
14381470
{
14391471
Debug.LogException(ex);
1472+
// Always shutdown to assure everything is cleaned up
1473+
ShutdownInternal();
14401474
ConnectionManager.LocalClient.SetRole(false, false);
14411475
IsListening = false;
14421476
}

0 commit comments

Comments
 (0)