This PR improves event management of the plugin by fixing potential resource leaks and preventing a potential deadlock