Skip to content

Refactor plugin lifecycle cleanup in PluginContext#1259

Open
Ayrton09 wants to merge 1 commit intoroflmuffin:mainfrom
Ayrton09:refactor/plugincontext-disposal
Open

Refactor plugin lifecycle cleanup in PluginContext#1259
Ayrton09 wants to merge 1 commit intoroflmuffin:mainfrom
Ayrton09:refactor/plugincontext-disposal

Conversation

@Ayrton09
Copy link
Copy Markdown

@Ayrton09 Ayrton09 commented Apr 1, 2026

Summary

This PR improves plugin lifecycle cleanup in PluginContext by making disposal explicit, idempotent, and centralized.

Why

PluginContext currently owns several long-lived resources during a plugin's lifetime:

  • the plugin instance
  • the plugin DI scope
  • the plugin root ServiceProvider
  • the plugin loader
  • hot-reload related event subscriptions
  • a FileSystemWatcher when hot reload is enabled

At the moment, unload/cleanup behavior is not fully centralized, which makes repeated unload/reload cycles harder to reason about and increases the risk of leaked resources.

This PR keeps the scope intentionally small and only focuses on PluginContext resource ownership and cleanup.

Changes

  • Implement IDisposable on PluginContext
  • Make owned-resource cleanup explicit and idempotent
  • Dispose the plugin file watcher cleanly
  • Unsubscribe Loader.Reloaded before disposing the loader
  • Dispose the root ServiceProvider
  • Replace the NotImplementedException used in self-termination with PluginTerminationException

Non-goals

This PR does not:

  • change PluginManager ownership rules
  • refactor css_plugins unload/reload
  • change bootstrap/host builder behavior
  • redesign contextual dependency resolution

Those changes are better handled in follow-up PRs.

Testing

  • Load a plugin normally
  • Unload a plugin once and verify no exceptions
  • Reload the same plugin multiple times
  • Replace/delete the plugin DLL and confirm hot-reload/unload still behaves correctly
  • Verify no watcher / file lock regressions

Files to review

  • managed/CounterStrikeSharp.API/Core/Plugin/PluginContext.cs

@Ayrton09 Ayrton09 requested a review from roflmuffin as a code owner April 1, 2026 03:58
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.

1 participant