Fixes exceptions on shutdown. (#2219)

This commit is contained in:
Vera Aguilera Puerto
2021-11-11 17:39:25 +01:00
committed by GitHub
parent 75fc9089c3
commit 52d669e032
13 changed files with 103 additions and 14 deletions

View File

@@ -80,6 +80,7 @@ namespace Robust.Server
[Dependency] private readonly IWatchdogApi _watchdogApi = default!;
[Dependency] private readonly IScriptHost _scriptHost = default!;
[Dependency] private readonly IMetricsManager _metricsManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustMappedStringSerializer _stringSerializer = default!;
[Dependency] private readonly ILocalizationManagerInternal _loc = default!;
[Dependency] private readonly INetConfigurationManager _netCfgMan = default!;
@@ -104,11 +105,12 @@ namespace Robust.Server
/// <inheritdoc />
public string ServerName => _config.GetCVar(CVars.GameHostName);
public bool ContentStart { get; set; }
/// <inheritdoc />
public void Restart()
{
Logger.InfoS("srv", "Restarting Server...");
// FIXME: This explodes very violently.
Cleanup();
Start(Options, _logHandlerFactory);
}
@@ -510,8 +512,6 @@ namespace Robust.Server
FinishMainLoop();
}
public bool ContentStart { get; set; }
public void OverrideMainLoop(IGameLoop gameLoop)
{
_mainLoop = gameLoop;
@@ -541,17 +541,18 @@ namespace Robust.Server
}
// called right before main loop returns, do all saving/cleanup in here
private void Cleanup()
public void Cleanup()
{
_modLoader.Shutdown();
IoCManager.Resolve<INetConfigurationManager>().FlushMessages();
_playerManager.Shutdown();
// shut down networking, kicking all players.
_network.Shutdown($"Server shutting down: {_shutdownReason}");
// shutdown entities
IoCManager.Resolve<IEntityLookup>().Shutdown();
_entityManager.Shutdown();
_entityManager.Cleanup();
if (_config.GetCVar(CVars.LogRuntimeLog))
{
@@ -572,6 +573,8 @@ namespace Robust.Server
{
WindowsTickPeriod.TimeEndPeriod((uint) _config.GetCVar(CVars.SysWinTickPeriod));
}
_config.Shutdown();
}
private void Input(FrameEventArgs args)

View File

@@ -28,6 +28,8 @@ namespace Robust.Server.Player
/// <param name="maxPlayers">Maximum number of players that can connect to this server at one time.</param>
void Initialize(int maxPlayers);
void Shutdown();
bool TryGetSessionByUsername(string username, [NotNullWhen(true)] out IPlayerSession? session);
/// <summary>

View File

@@ -115,6 +115,15 @@ namespace Robust.Server.Player
_network.Disconnect += EndSession;
}
public void Shutdown()
{
KeyMap = default!;
_network.Connecting -= OnConnecting;
_network.Connected -= NewSession;
_network.Disconnect -= EndSession;
}
public bool TryGetSessionByUsername(string username, [NotNullWhen(true)] out IPlayerSession? session)
{
if (!_userIdMap.TryGetValue(username, out var userId))

View File

@@ -31,6 +31,12 @@ namespace Robust.Shared.Configuration
_isServer = isServer;
}
public virtual void Shutdown()
{
_configVars.Clear();
_configFile = null;
}
/// <inheritdoc />
public void LoadFromFile(string configFile)
{

View File

@@ -10,6 +10,8 @@ namespace Robust.Shared.Configuration
void Initialize(bool isServer);
void Shutdown();
/// <summary>
/// Sets up the ConfigurationManager and loads a TOML configuration file.
/// </summary>

View File

@@ -73,6 +73,16 @@ namespace Robust.Shared.Configuration
public event EventHandler? ReceivedInitialNwVars;
private bool _receivedInitialNwVars;
public override void Shutdown()
{
base.Shutdown();
FlushMessages();
_replicatedCVars.Clear();
ReceivedInitialNwVars = null;
_receivedInitialNwVars = false;
}
/// <inheritdoc />
public void SetupNetworking()
{

View File

@@ -70,7 +70,6 @@ namespace Robust.Shared.GameObjects
_componentFactory.ComponentAdded -= OnComponentAdded;
_componentFactory.ComponentReferenceAdded -= OnComponentReferenceAdded;
_netComponents.Clear();
_entTraitDict.Clear();
_entCompIndex.Clear();
_deleteSet.Clear();
FillComponentDict();
@@ -709,6 +708,7 @@ namespace Robust.Shared.GameObjects
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillComponentDict()
{
_entTraitDict.Clear();
foreach (var refType in _componentFactory.GetAllRefTypes())
{
_entTraitDict.Add(refType, new Dictionary<EntityUid, Component>());

View File

@@ -98,6 +98,19 @@ namespace Robust.Shared.GameObjects
Started = false;
}
public void Cleanup()
{
QueuedDeletions.Clear();
QueuedDeletionsSet.Clear();
Entities.Clear();
_eventBus.Dispose();
_eventBus = null!;
ClearComponents();
Initialized = false;
Started = false;
}
public virtual void TickUpdate(float frameTime, Histogram? histogram)
{
using (histogram?.WithLabels("EntitySystems").NewTimer())

View File

@@ -209,6 +209,11 @@ namespace Robust.Shared.GameObjects
_entityManager.EventBus.UnsubscribeEvents(system);
}
Clear();
}
public void Clear()
{
_systemTypes.Clear();
_updateOrder = Array.Empty<UpdateReg>();
_frameUpdateOrder = Array.Empty<IEntitySystem>();

View File

@@ -22,6 +22,11 @@ namespace Robust.Shared.GameObjects
void Initialize();
void Startup();
void Shutdown();
/// <summary>
/// Drops every entity, component and entity system.
/// </summary>
void Cleanup();
void TickUpdate(float frameTime, Histogram? histogram=null);
/// <summary>

View File

@@ -59,6 +59,8 @@ namespace Robust.Shared.GameObjects
/// <seealso cref="IEntitySystem.Shutdown"/>
void Shutdown();
void Clear();
/// <summary>
/// Update all systems.
/// </summary>

View File

@@ -249,7 +249,7 @@ namespace Robust.Shared.Network
_config.OnValueChanged(CVars.NetVerbose, NetVerboseChanged);
if (isServer)
{
_config.OnValueChanged(CVars.AuthMode, i => Auth = (AuthMode) i, invokeImmediately: true);
_config.OnValueChanged(CVars.AuthMode, OnAuthModeChanged, invokeImmediately: true);
}
#if DEBUG
_config.OnValueChanged(CVars.NetFakeLoss, _fakeLossChanged);
@@ -260,11 +260,7 @@ namespace Robust.Shared.Network
_strings.Initialize(() => { Logger.InfoS("net", "Message string table loaded."); },
UpdateNetMessageFunctions);
_serializer.ClientHandshakeComplete += () =>
{
Logger.InfoS("net", "Client completed serializer handshake.");
OnConnected(ServerChannelImpl!);
};
_serializer.ClientHandshakeComplete += OnSerializerOnClientHandshakeComplete;
_initialized = true;
@@ -274,6 +270,17 @@ namespace Robust.Shared.Network
}
}
private void OnAuthModeChanged(int mode)
{
Auth = (AuthMode)mode;
}
private void OnSerializerOnClientHandshakeComplete()
{
Logger.InfoS("net", "Client completed serializer handshake.");
OnConnected(ServerChannelImpl!);
}
private void SynchronizeNetTime()
{
// Synchronize Lidgren NetTime with our RealTime.
@@ -383,12 +390,36 @@ namespace Robust.Shared.Network
// Clear cached message functions.
Array.Clear(_netMsgFunctions, 0, _netMsgFunctions.Length);
_blankNetMsgFunctions.Clear();
// Clear string table.
// This has to be done AFTER clearing _netMsgFunctions so that it re-initializes NetMsg 0.
_strings.Reset();
_messages.Clear();
_config.UnsubValueChanged(CVars.NetVerbose, NetVerboseChanged);
if (IsServer)
{
_config.UnsubValueChanged(CVars.AuthMode, OnAuthModeChanged);
}
#if DEBUG
_config.UnsubValueChanged(CVars.NetFakeLoss, _fakeLossChanged);
_config.UnsubValueChanged(CVars.NetFakeLagMin, _fakeLagMinChanged);
_config.UnsubValueChanged(CVars.NetFakeLagRand, _fakeLagRandomChanged);
_config.UnsubValueChanged(CVars.NetFakeDuplicates, FakeDuplicatesChanged);
#endif
_serializer.ClientHandshakeComplete -= OnSerializerOnClientHandshakeComplete;
_cancelConnectTokenSource?.Cancel();
ClientConnectState = ClientConnectionState.NotConnecting;
ConnectFailed = null;
Connected = null;
Disconnect = null;
_connectingEvent.Clear();
_initialized = false;
}
public void ProcessPackets()

View File

@@ -64,6 +64,7 @@ namespace Robust.Shared.Network
_network.RegisterNetMessage<MsgStringTableEntries>(ReceiveEntries, NetMessageAccept.Client | NetMessageAccept.Handshake);
Reset();
_initialized = true;
}
private void ReceiveEntries(MsgStringTableEntries message)