FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module

Home Page:https://docs.altv.mp/cs/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dead lock when setting player health to 0

solrac200 opened this issue · comments

Description of the problem

In an Async Resource:
If you set the health of a Player to 0, this will trigger the PlayerDead Event. If you have some funtion attached to this event which calls some function that needs to run on the main thread (like IVehicle.Remove) this will result in a dead lock if the main thread needs to do something with that player in the meantime.
image

Reproduction steps

Empty Server with only following code:

    public class Class1 : AsyncResource
    {
        public override void OnStart()
        {
            
        }

        public override void OnStop()
        {
            
        }
    }
    public class Class2 : IScript
    {
        IVehicle veh;
        [ScriptEvent(ScriptEventType.PlayerConnect)]
        public void OnPlayerConnect(IPlayer player, string reason)
        {
            player.Spawn(new Position(0, 0, 72), 0);
            veh = Alt.CreateVehicle(AltV.Net.Enums.VehicleModel.Adder, player.Position, player.Rotation);
            Task.Run(() =>
            {
                Thread.Sleep(20000);
                Alt.Log("Set Health");
                player.Health = 0;
            });
        }

        [ScriptEvent(ScriptEventType.PlayerEnterVehicle)]
        public void OnPlayerEnterVehicle(IVehicle vehicle, IPlayer player, byte seat)
        {
            Thread.Sleep(20000);
            Alt.Log("EnteredVehicle");
            Alt.Log("Armor:"+player.Armor);
        }

        [ScriptEvent(ScriptEventType.PlayerDead)]
        public void OnPlayerDead(IPlayer player, IEntity killer, uint reason)
        {
            Alt.Log("Dead");
            Thread.Sleep(20000);
            Alt.Log("Remove");
            veh.Remove();
        }


    }

Then get into the vehicle in the first 20s after joining the server. The server will output "Remove" but the vehicle never gets removed because there will be a dead lock.

Output from the Logs of the above code will be the follwing:
[13:45:07] Set Health
[13:45:07] Dead
[13:45:19] EnteredVehicle
[13:45:27] Remove

Expected behaviour

No dead lock should occur. The AsyncPlayer should release the lock bevore invoking the PlayerDead event.

Additional context

Tested on release and dev branch

Operating system

Windows 10

Version

14.0-dev4

Reproduction tested

  • I confirm that I have made sure that this issue is also present on the newest dev version

Fixed in 13.41