Skip to content

Document when PluginManager.unloadPlugin returns false #291 - #686

Open
AzazelSensei wants to merge 2 commits into
pf4j:masterfrom
AzazelSensei:docs-291-unload-plugin
Open

AzazelSensei wants to merge 2 commits into
pf4j:masterfrom
AzazelSensei:docs-291-unload-plugin

Conversation

@AzazelSensei

Copy link
Copy Markdown
Contributor

unloadPlugin's javadoc only said it returns true if the plugin was unloaded.

I documented the current AbstractPluginManager behavior:

  • false when the id is unknown or already unloaded (not an error)
  • false when stop leaves the plugin STARTED; it stays loaded
  • Plugin.stop throwing PluginRuntimeException still unloads and returns true; the cause is on PluginWrapper.getFailedException()
  • PluginRuntimeException is thrown if the plugin classloader cannot be closed

Fixes #291

@decebals

decebals commented Sep 3, 2026

Copy link
Copy Markdown
Member

Thanks for picking this one up, it has been open since 2019 and the behaviour you describe is real. I checked it against AbstractPluginManager.unloadPlugin. Four changes and this can go in.

1. Split the text the way we did on #681. The point there was not only to keep implementation class names off the interface, it was that PluginManager states what every implementation must honour and the behaviour of one implementation goes on the AbstractPluginManager override.

Keep on the interface: the plugin and its dependents are unloaded, dependents first; true when the plugin was unloaded; false when there is no such loaded plugin, or when it could not be stopped and therefore stays loaded.

Move to the override: that a PluginRuntimeException from Plugin.stop() does not prevent the unload, that the plugin is removed anyway with state FAILED and the cause on PluginWrapper.getFailedException(), and that the class loader is closed at the end. A javadoc body consisting only of {@inheritDoc} is the same as no comment, so that is the place for it.

2. Drop "treat it as a no-op". Dependents are unloaded before the plugins.containsKey(pluginId) check and before the stop, so false can come back after the dependents are already gone. Please say that instead.

3. Move the getFailedException() pointer. In the branch that returns false because the plugin is still STARTED, nothing sets the failed exception. doStopPlugin sets it only when stop() throws, and then the state is FAILED and the method returns true. So that pointer belongs to the successful unload, in the override. Worth adding there that the shipped stop path never leaves the plugin STARTED, since doStopPlugin returns STOPPED or FAILED and any other exception is caught and the plugin removed anyway; the branch is there for a subclass that overrides the stop.

4. Two small ones. Widen the @throws: resolveDependencies() runs after the removal and can throw CyclicDependencyException or DependenciesNotFoundException, both PluginRuntimeException. And in the changelog, entries under a heading go in ascending issue order, so [#291] before [#293]; the link definition at the bottom is already in the right place.

@AzazelSensei

Copy link
Copy Markdown
Contributor Author

Thanks. Moved the details about failed stops and the false case to the AbstractPluginManager override and kept only the contract on the interface. The throws clause now points at the resolver exceptions, and the changelog entry is above #293.

@decebals

Copy link
Copy Markdown
Member

Looks good, thanks. Two last things and it's ready to merge:

  1. 3.16.0 went out in the meantime, so the changelog entry now lands under 3.16.0 when merged. Please rebase on master and put it under [Unreleased], #### Added.

  2. My mistake in the review: after the unload the state is UNLOADED, not FAILED. FAILED is only the old state in the event that gets fired. Something like "the plugin is unloaded anyway and the cause is available from getFailedException()" is enough.

@AzazelSensei
AzazelSensei force-pushed the docs-291-unload-plugin branch from 12ff432 to c47251a Compare November 23, 2026 07:01
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.

Document when unloadPlugin can fail

2 participants