From 6f01be22d3872e6459dcb9638f0730f0b4d5bea1 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 21 Apr 2026 11:26:18 -0400 Subject: [PATCH] Bug 42430: Fix issue with stale plugin methods after plugin upgrade When a plugin is upgraded in place (by uploading a new version over the old one), the list of methods Koha knows about for that plugin can get out of date. For example, if a plugin used to have a "tool" method and the new version renames it to "admin", the staff interface can still show the old "Run tool" link, and clicking it produces an error saying the plugin has no "tool" method. This happens because the plugin upload runs inside a long-lived Plack worker that has already loaded the old version of the plugin in memory. When Koha scans the plugin to record its methods, Perl has no reason to re-read the file from disk, so the scan sees the old method list and stores that in the plugin_methods table. The plugin code on disk is new, but the record of what methods it has is stale. This patch forces Koha to throw away the in-memory copy of each plugin and reload it from disk before scanning, so the recorded list of methods matches the plugin that is actually installed. Test Plan: 1) Install the Batch Permission Modifier Plugin version 1.3.1 (this version exposes a "tool" method) 2) Go to Tools > Plugins and confirm the plugin row shows "Run tool" in its actions menu 3) Upload Batch Permission Modifier Plugin version 1.4.1 over the top of 1.3.1 (this version exposes "admin" instead of "tool") 4) Reload the Plugins page 5) Note the plugin still shows "Run tool" in its actions menu 6) Click "Run tool" 7) Note the error: the plugin has no "tool" method! 8) Apply this patch 9) Restart all the things! 10) Uninstall the plugin 11) Repeat steps 1-4 12) Note that "Run tool" is gone and "Run admin tool" appears in its place 13) Click "Run admin tool" and confirm it loads without error! Signed-off-by: Kyle M Hall Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass (cherry picked from commit c912369c0a350f1d546ff66f5da289a0bad179cc) Signed-off-by: Baptiste Wojtkowski --- Koha/Plugins.pm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index a686bf8ec0..e4fc6f283a 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -26,6 +26,7 @@ use Module::Load::Conditional qw( can_load ); use Module::Load; use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/; +use Symbol qw( delete_package ); use Try::Tiny; use POSIX qw(getpid); @@ -322,6 +323,17 @@ sub InstallPlugins { } foreach my $plugin_class (@plugin_classes) { + + # Force a fresh load from disk. Without this, a long-lived Plack + # worker that already has the plugin loaded would rescan a stale + # symbol table during an upgrade, so plugin_methods ends up out of + # sync with the installed .pm (e.g. missing newly-added methods or + # retaining methods that were renamed or removed). + ( my $plugin_inc_key = $plugin_class ) =~ s{::}{/}g; + $plugin_inc_key .= '.pm'; + delete $INC{$plugin_inc_key}; + delete_package($plugin_class); + if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) { next unless $plugin_class->isa('Koha::Plugins::Base');