Skip to content

Cleanup crashes fix#526

Closed
bssth wants to merge 3 commits into
ikkentim:mainfrom
OpenSamp:master
Closed

Cleanup crashes fix#526
bssth wants to merge 3 commits into
ikkentim:mainfrom
OpenSamp:master

Conversation

@bssth
Copy link
Copy Markdown
Contributor

@bssth bssth commented May 9, 2026

I don't know if this "fix" works for you, but I can't think of any other way to do it. Without this fix, my server shuts down in very ugly way with crash

@ikkentim
Copy link
Copy Markdown
Owner

I kinda wanna try a different approach, it's roughly like the approach they take in the open.mp server repo:

  • in the SampSharp component implement void onFree(IComponent* component) override;;
  • in SampSharpInitParams add a function that allows setting an OnComponentFree handler
  • in ecs host builder loop register an OnComponentFree handler
  • create a ComponentList wrapper for IComponentList in ecs lib
    • has a GetComponent<T>() returning a SafeComponentHandle();
    • the ComponentList keeps cache of UID -> safe handle
    • on component free, update related safe component handle (set _handle to null)

SafeEventHandlerRegistration should hold one of these safe component handles; it can simply check HasValue of the reference before calling RemoveEventHandler at that point.

@ikkentim
Copy link
Copy Markdown
Owner

The proper fix was actually really simple, but I only found out after i implemented a safe component handle mechanism...

         context.UnhandledExceptionHandler = UnhandledExceptionHandler;

+        context.Cleanup += ContextOnCleanup;
+
         LoadSystems();

         // Fire initial event
         OnGameModeInit();
     }

-    protected override void Cleanup()
+    private void ContextOnCleanup(object? sender, EventArgs e)
     {
         OnGameModeExit();

         if (_serviceProvider is IDisposable disposable)
         {
-            //  TODO: This cleanup is called so late - we can't unsubscribe event handlers anymore, but the disposables in registered systems will try to unsubscribe them. This may cause a System.ExecutionEngineException on shutdown.
             disposable.Dispose();
             _serviceProvider = null;
         }
     }

I'll implement the safe handle mechanism anyway since it is good practice.

@bssth
Copy link
Copy Markdown
Contributor Author

bssth commented May 12, 2026

My version was originally designed to be dumb but functional, because it’s no good if it crashes on shutdown.

Seems like this PR is no longer relevant and we can close it.

@ikkentim
Copy link
Copy Markdown
Owner

Indeed, fixed in #552 👍

@ikkentim ikkentim closed this May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants