[C#] TriggerClientEvent crashes resources intermittently

This is in relation to this post in the development forums: [C#] Unknown error using 'chatMessage'

This bug is very intermittent, but when triggering player client events from a C# server script, the resource crashes with a RemotingException. At first, it seemed like it was related to the chatMessage event…but I tried creating a Lua script to capture the chat command, and then triggering a custom server event in my C# script. The error persisted.

The call that it’s failing on is Player.TriggerEvent, and it happens on BaseScript.TriggerClientEvent as well.

@BlockBa5her also has had difficulties with this error, and neither of us have found a fix. I’ve also spoken to @Fiskey111, who says he gets it at random times.

The best I can gather is that it occurs when you clear the cache and restart the server for the first time. If you restart the resource 2 or 3 times, the script will work perfectly. Or even if you get the exception, you can restart it once and it will work afterwards.

I’ve confirmed this to be the case in builds 351 and 389…we’re in the process of upgrading to 426 to see if it happens on the newer build as well. This does not appear to be happening with any of our Lua scripts, only the C# ones.

If anyone could take a look, it would be appreciated.

This is a code snippet from one of the affected scripts…it happens whether the OnChatCommand function is attached to the chatMessage event, or a custom one.

The exception details are below the code snippet. Let me know if you need any further details.

using CitizenFX.Core;
using CitizenFX.Core.Native;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerScript
{
    public class Main : BaseScript
    {
        public Main()
        {
            EventHandlers["myResource:OnServerChatCommand"] += new Action<int, string, string>(OnChatCommand);
        }

        private void OnChatCommand(int source, string n, string message)
        {
            string[] args = message.Split(' ');

            if (args[0] == "/eng")
            {
                try
                {
                    PlayerList pl = new PlayerList();
                    Player p = pl[source];

                    if (p != null)
                    {
                        p.TriggerEvent("myResource:myEvent");
                        Function.Call(Hash.CANCEL_EVENT);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Write(ex.ToString());
                }
            }
        }
    }
}
System.Runtime.Remoting.RemotingException: Server for uri '374dca40_09ca_4b7f_a886_a5f2de357ed5/df2c9b5c_5.rem' not found
Server stack trace:
at System.Runtime.Remoting.Proxies.RealProxy.GetAppDomainTarget () [0x00031] in <fb0d2884a2b44d8db375bbed7cc70740>:0
at (wrapper xdomain-dispatch) CitizenFX.Core.MonoScriptRuntime+WrapScriptHost:InvokeNative (object,byte[]&,byte[]&)
Exception rethrown at [0]:
at (wrapper xdomain-invoke) CitizenFX.Core.MonoScriptRuntime+WrapScriptHost:InvokeNative (CitizenFX.Core.fxScriptContext&)
at CitizenFX.Core.ScriptContext.InvokeInternal (System.UInt64 nativeIdentifier, CitizenFX.Core.IScriptHost scriptHost) [0x0000c] in /src/code/client/clrcore/ScriptContext.cs:168
at CitizenFX.Core.ScriptContext.Invoke (System.UInt64 nativeIdentifier, CitizenFX.Core.IScriptHost scriptHost) [0x00000] in /src/code/client/clrcore/ScriptContext.cs:161
at CitizenFX.Core.Native.Function.InvokeInternal (CitizenFX.Core.Native.Hash nativeHash, System.Type returnType, CitizenFX.Core.Native.InputArgument[] args) [0x00026] in /src/code/client/clrcore/Native.cs:28
at CitizenFX.Core.Native.Function.Call (CitizenFX.Core.Native.Hash hash, CitizenFX.Core.Native.InputArgument[] arguments) [0x00000] in /src/code/client/clrcore/Native.cs:16
at CitizenFX.Core.Player.TriggerEvent (System.String eventName, System.Object[] args) [0x0000f] in /src/code/client/clrcore/Server/ServerWrappers.cs:47
2 Likes

would it be possible to get a self-contained repro that does not involve having to join as a client multiple times?

also is this happening on linux or windows?

http://zaidox.com/net-remote-object-lifetime.html

This seems to be a common .NET gotcha. @iodine, can you look into this?

1 Like

I’ll see what I can do…it’s random as hell, so unfortunately I’ve had difficulties even getting this far in troubleshooting. But I’ll look into it further and see if I can get a repro scenario where it happens 100% of the time.

This is happening on Linux…my local copy of FX Server is on my Windows machine. I don’t remember seeing it happen on my local PC, but I can’t say that for sure. We normally test on the Linux boxes we have set up.

Thanks for looking into this, guys. It would be awesome if we can find the root cause of this.

Let me know if you need any more information.

This issue should be resolved in this commit:

You’ll probably have to wait until the build succeeds, the fix will be in version 427 or higher of the server.

2 Likes

Awesome! Thanks so much!

1 Like