ModAPI: null Reference CTD - Dedicated server entity gets out of sync with client

xPhoenixXx shared this bug 21 days ago
Investigating

Since the last update I have found an issue where the player entitys are getting out of sync with the dedicated server in modAPI throwing null references and crashing the player to the desktop. The Issue only seems to impact dedicated server<->client gameplay, offline or self hosted gameplay does not seem to suffer from it in any major way.


Specifically in my case if i am using a .draw() related thing like billboard

Example:

Vector3D start = player.Character.GetPosition() + forward * dist + right * Math.Sin(angleH) * dist * 0.1 + up * Math.Sin(angleV) * dist * 0.1;

Vector3D dir = forward * rand.NextDouble() * 5; // Short streak direction (0-5m)

MyTransparentGeometry.AddLineBillboard(material, streakColor, start, dir, (float)dir.Length(), thickness, BlendTypeEnum.Standard);


OR


In the missionHUD system

Example:

MyAPIGateway.Utilities.GetObjectiveLine().Objectives.Clear();

MyAPIGateway.Utilities.GetObjectiveLine().Title = "Initialising";

MyAPIGateway.Utilities.GetObjectiveLine().Objectives.Add("Scanning..");

MyAPIGateway.Utilities.GetObjectiveLine().Show();


The player [entity(MyAPIGateway.Session.Player;)] reference seems to get out of date if a player dies or respawns. After respawning, the client side and server side entities seem to become out of sync in the client side, server side and/or local client instances. causing a Nullreference type Crash to desktop error on the next call to AddlLineBillboard or GetObjectiveLine() after the player finishes respawning.

I've attached one such error to this report in debug.txt


The issue seems to be a timing issue, because if you put in a really extreme timer delay the moment a player is detected as dead effectively halting any player entity calls/checks for a few seconds after the player respawns, the crash to desktop mostly does not occur anymore.


An example of such a hacky workaround fix in my UpdateAfterSimulation() which seems to mostly work is below:


//Client side kill switch if player is not spawned in yet halt all execution for 15 seconds

//after they respawn.

var player = MyAPIGateway.Session.Player;

if (player == null || player.Character == null || player.Character.IsDead)

{

killswitch = true;

base.UpdateAfterSimulation();

return;

} else if (killswitch)

{

if (counter >= 1500)

{

counter = 0;

killswitch = false;

}

counter++;

}


if (!killswitch) {

//all the rest of your mod's UpdateAfterSimulation() / UpdateBeforeSimulation() code needs to go in here

}

Replies (4)

photo
1

Other thoughts here:

Most of this is based on 'guilt by association' testing.

In my lobby mod: https://github.com/jpcsupplies/Lobby/

The crash to desktop went away if I apply 'killswitch' to code that draws objects in the game world (that references MyAPIGateway.Session.Player; when plotting the location to render the lines in relation to)


In my other economy mod:

https://github.com/jpcsupplies/Economy_mod


The crash went away if I disabled the hud in economy, (which also references MyAPIGateway.Session.Player; in some incidental checks if a player has finished loading into the world and deciding to whom the hud show, and internally the game client side needs to know its own entity before updating said hud)


This bug seems particularly odd, as the "killswitch" itself triggering suggests that the MyAPIGateway.Session.Player; has already cleared/updated (to reflect null or isdead) so it seems particularly unusual that after the player finishes respawning at a med bay, the MyAPIGateway.Session.Player; check is somehow contributing to a null reference crash to desktop suggesting MyAPIGateway.Session.Player; has either not updated yet even after the player has respawned (still null for a few ticks) or has somehow populated with the old entity (null reference as that entity no longer exists) somehow (cached record?)

The null record seems unlikely as my mods already check for null anyway, so there must be /something non-null/ in the entity record after respawn, but it does not match the entity actually being used for the player.


It could be somethine else entirely but immediately after the player loads in world next to a med bay, the above seem to contribute to a null reference crash.


Also other server admins and modders have been reporting unexplainable client crash to desktops, in one case they suspected it related to NPC entity ids not updating consistently too, possibly as a conseqence of sim speed or network latency when playing on a dedicated server.


Whatever it is, it's a strange one. The fix here literally seems to be "go slower"

photo
1

To recap the issue with the items requested by Milka:

(yes the steam versions already implement my workaround)

Here is a paste from my answer in discord:


Yes I implemented my suggested fix already on the linked mods, and yes they are both my mods.


The pre-fixed versions should be available on github if you use a version

from about a month before the date of the report. So this commit or any before it:

<https://github.com/jpcsupplies/Economy_mod/tree/250d4cfda9d90776e809886af2a6ed1b691ad1a4/Economy/Data/Scripts/Economy.scripts>; main files changed to implement fix later were EconConfig/HudManager.cs and EconomyScript.cs


To make the bug happen when playing on a dedicated server with the script type /hud on then press backspace to respawn at a medbay. Game will crash to desktop once the player loads in front of the med bay.

============================


In the other scenario for lobby mod this version or older will have the issue still:

<https://github.com/jpcsupplies/Lobby/tree/d4b9202b0b7276752d3a761dda7cc84f10997ce7/Data/Scripts/Lobby.scripts>;


The fixes in later versions with workaround implemented are:

Lobby.cs, LobbyPhysics.cs and LobbyServer.cs


To make the issue occur in Lobby you will need to play on a dedicated server with the Lobby mod running, then configure a navigation hazard that will be drawn into the world, such as a blackhole, instructions how to do this are in here: <https://github.com/jpcsupplies/Lobby/blob/master/README.txt>; under the section "Adding Navigation Hazards to your game:" then setup a medbay within visual range of the blackhole, then press backspace to respawn.

photo
1

Note the issue only occurs playing on dedicated servers, which is why it took so long for me to notice them, as I usually test my mods offline or in local coop mode first; and the issue will not occur there.

photo
1

REPOST WITH COPIES OF MAP SAVES ADDED


To recap the issue with the items requested by Mika:


Yes I implemented my suggested fix already on the linked mods, and yes they are both my mods.


The pre-fixed versions should be available on github if you use a version

from about a month before the date of the report. So this commit or any before it:

<https://github.com/jpcsupplies/Economy_mod/tree/250d4cfda9d90776e809886af2a6ed1b691ad1a4/Economy/Data/Scripts/Economy.scripts>; main files changed to implement fix later were EconConfig/HudManager.cs and EconomyScript.cs


To make the bug happen when playing on a dedicated server with the script type /hud on then press backspace to respawn at a medbay. Game will crash to desktop once the player loads in front of the med bay.

============================


In the other scenario for lobby mod this version or older will have the issue still:

<https://github.com/jpcsupplies/Lobby/tree/d4b9202b0b7276752d3a761dda7cc84f10997ce7/Data/Scripts/Lobby.scripts>;


The fixes in later versions with workaround implemented are:

Lobby.cs, LobbyPhysics.cs and LobbyServer.cs


To make the issue occur in Lobby you will need to play on a dedicated server with the Lobby mod running, then configure a navigation hazard that will be drawn into the world, such as a blackhole, instructions how to do this are in here: <https://github.com/jpcsupplies/Lobby/blob/master/README.txt>; under the section "Adding Navigation Hazards to your game:" then setup a medbay within visual range of the blackhole, then press backspace to respawn.

Leave a Comment
 
Attach a file