Skip to content

Access Violation in IServerService.SendRconCommand during OnGameModeInit #599

Open

Description

@DevD4v3

Calling IServerService.SendRconCommand("loadfs ...") from OnGameModeInit intermittently crashes the server with an Access Violation (0xC0000005).

Minimal reproduction:

[Event]
public void OnGameModeInit()
{
    Console.WriteLine("BEFORE");

    serverService.SendRconCommand("loadfs EntryMap");

    Console.WriteLine("AFTER");
}

The crash occurs before "AFTER" is printed.

Stack trace:

Fatal error.
0xC0000005
   at SampSharp.OpenMp.Core.Api.IConsoleComponent.<Send>g____PInvoke|3_0(IntPtr, SampSharp.OpenMp.Core.Std.StringView, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
   at SampSharp.OpenMp.Core.Api.IConsoleComponent.Send(System.String, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
   at SampSharp.Entities.SAMP.ServerService.SendRconCommand(System.String)
   at CTF.Host.GameModeInit.OnGameModeInit()
   at DynamicClass.lambda_method194(System.Runtime.CompilerServices.Closure, System.Object, System.Object[], System.IServiceProvider, SampSharp.Entities.IEntityManager)
   at SampSharp.Entities.EventDispatcher+<>c__DisplayClass16_0.<CreateTargetSite>b__0(System.Object, SampSharp.Entities.EventContext)
   at SampSharp.Entities.EventDispatcher.InnerInvoke(SampSharp.Entities.EventContext, Event, System.Object)
   at SampSharp.Entities.EventDispatcher+<>c__DisplayClass17_0.<CreateEventInvoke>b__0(SampSharp.Entities.EventContext)
   at CTF.Host.Middlewares.GlobalExceptionHandler.Invoke(SampSharp.Entities.EventContext)
   at SampSharp.Entities.EventDispatcher+<>c__DisplayClass17_0.<CreateEventInvoke>b__1(System.ReadOnlySpan`1<System.Object>)
   at SampSharp.Entities.EventDispatcher.Invoke(System.String, System.ReadOnlySpan`1<System.Object>)
   at SampSharp.Entities.EcsHost.OnGameModeInit()
   at SampSharp.Entities.EcsHost.Start(SampSharp.OpenMp.Core.IStartupContext)
   at SampSharp.Entities.HostContextBinder.OnContextInitialized(System.Object, System.EventArgs)
   at SampSharp.OpenMp.Core.StartupContext.InitializeUsing(SampSharp.OpenMp.Core.IStartup)
   at SampSharp.Entrypoint.Initialize(SampSharp.OpenMp.Core.SampSharpInitParams)

Notes:

  • The issue is intermittent.
  • Sometimes the command succeeds, sometimes it crashes.
  • The filterscript being loaded does not appear to matter.
  • Delaying the call (e.g. await Task.Delay(5000)) prevents the crash.

Activity

  1. DevD4v3 commented on Jun 8, 2026

    @DevD4v3
    ContributorAuthor

    While debugging, I observed that IConsoleComponent.Send() is invoked with _handle == 0x0000000000000000.
    The generated wrapper exposes HasValue => _handle != default, but Send() does not validate the handle before invoking the native P/Invoke call.

  2. ikkentim commented on Jun 8, 2026

    @ikkentim
    Owner

    Interesting, I may need to look into a later moment for triggering OnGameModeInit instead on component initialization.

  3. ikkentim commented on Jun 8, 2026

    @ikkentim
    Owner

    Based on the open.mp code, we should use Component.onReady for OnGameModeInit to match the pawn behavior. This requires a setOnReady handler + implementation to be added to sampsharp-api -> SampSharpInitParams and .Core to wire it up correctly.

  4. DevD4v3 commented on Jun 14, 2026

    @DevD4v3
    ContributorAuthor

    @ikkentim The crash is still occurring, but now the stack trace has changed:

    Fatal error.
    0xC0000005
       at SampSharp.OpenMp.Core.Api.IConsoleComponent.<Send>g____PInvoke|3_0(IntPtr, SampSharp.OpenMp.Core.Std.StringView, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
       at SampSharp.OpenMp.Core.Api.IConsoleComponent.Send(System.String, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
       at SampSharp.Entities.SAMP.ServerService.SendRconCommand(System.String)
       at CTF.Application.Maps.Systems.SetDefaultMapSystem.OnGameModeInit()
       at DynamicClass.lambda_method193(System.Runtime.CompilerServices.Closure, System.Object, System.Object[], System.IServiceProvider, SampSharp.Entities.IEntityManager)
       at SampSharp.Entities.EventDispatcher+<>c__DisplayClass16_0.<CreateTargetSite>b__0(System.Object, SampSharp.Entities.EventContext)
       at SampSharp.Entities.EventDispatcher.InnerInvoke(SampSharp.Entities.EventContext, Event, System.Object)
       at SampSharp.Entities.EventDispatcher+<>c__DisplayClass17_0.<CreateEventInvoke>b__0(SampSharp.Entities.EventContext)
       at CTF.Host.Middlewares.GlobalExceptionHandler.Invoke(SampSharp.Entities.EventContext)
       at SampSharp.Entities.EventDispatcher+<>c__DisplayClass17_0.<CreateEventInvoke>b__1(System.ReadOnlySpan`1<System.Object>)
       at SampSharp.Entities.EventDispatcher.Invoke(System.String, System.ReadOnlySpan`1<System.Object>)
       at SampSharp.Entities.EcsHost.OnGameModeInit()
       at SampSharp.Entities.EcsHost.Start(SampSharp.OpenMp.Core.IStartupContext)
       at SampSharp.Entities.HostContextBinder.OnContextReady(System.Object, System.EventArgs)
       at SampSharp.OpenMp.Core.StartupContext.OnReady()
    
  5. DevD4v3 commented on Jun 14, 2026

    @DevD4v3
    ContributorAuthor

    I just noticed that the same crash occurs in a timer.

    Fatal error.
    0xC0000005
       at SampSharp.OpenMp.Core.Api.IConsoleComponent.<Send>g____PInvoke|3_0(IntPtr, SampSharp.OpenMp.Core.Std.StringView, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
       at SampSharp.OpenMp.Core.Api.IConsoleComponent.Send(System.String, SampSharp.OpenMp.Core.Api.ConsoleCommandSenderData ByRef)
       at SampSharp.Entities.SAMP.ServerService.SendRconCommand(System.String)
       at CTF.Application.Maps.Services.MapRotationService.OnLoadingMap()
       at CTF.Application.Maps.LoadTime.Decrease()
       at CTF.Application.Maps.Services.MapRotationService.OnTimer(System.IServiceProvider)
       at SampSharp.Entities.TimerSystem+<>c__DisplayClass10_0.<Start>b__0(System.IServiceProvider, SampSharp.Entities.TimerReference)
       at SampSharp.Entities.TimerSystem+<>c__DisplayClass11_0.<Start>b__0()
       at SampSharp.Entities.TimerSystem.Tick()
       at SampSharp.Entities.TickingSystem.OnTick(SampSharp.OpenMp.Core.Std.Chrono.Microseconds, SampSharp.OpenMp.Core.Std.Chrono.TimePoint)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions