]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
ensure cleanup is always called at the end of dispatch
authorDylan Hardison <dylan@hardison.net>
Tue, 2 Jun 2020 01:49:17 +0000 (21:49 -0400)
committerDylan Hardison <dylan@hardison.net>
Sun, 21 Jun 2020 15:08:16 +0000 (11:08 -0400)
Bugzilla/App/CGI.pm
Bugzilla/App/Plugin/Glue.pm

index 924b9063025c54c690ea3858112b03b9716e682c..2c283a8be80d7fd4a58dc15ca911bf8216c1f1dc 100644 (file)
@@ -74,7 +74,7 @@ sub load_one {
     tie *STDOUT, 'Bugzilla::App::Stdout', controller => $c;   ## no critic (tie)
 
     # the finally block calls cleanup.
-    $c->stash->{cleanup_guard}->dismiss;
+    $Bugzilla::App::Plugin::Glue::cleanup_guard->dismiss if $Bugzilla::App::Plugin::Glue::cleanup_guard;
     Bugzilla->usage_mode(USAGE_MODE_BROWSER);
     try {
       Bugzilla->init_page();
index 4b56629a6a0f29d44062eabf3931cbb85669f355..f17ff7f0ec41232423d4d4c157772d48bd09d99a 100644 (file)
@@ -19,6 +19,8 @@ use Mojo::JSON qw(decode_json);
 use Scalar::Util qw(blessed);
 use Scope::Guard;
 
+our $cleanup_guard;
+
 sub register {
   my ($self, $app, $conf) = @_;
 
@@ -36,10 +38,15 @@ sub register {
   }
 
   $app->hook(
-    before_dispatch => sub {
-      my ($c) = @_;
+    around_dispatch => sub {
+      my ($next, $c) = @_;
       Log::Log4perl::MDC->put(request_id => $c->req->request_id);
-      $c->stash->{cleanup_guard} = Scope::Guard->new(\&Bugzilla::cleanup);
+
+      # Below we localize a package scoped variable, and put a scope guard in it
+      # this means the cleanup routine will be called when this around_dispatch
+      # hook returns. We do this to avoid having to handle any exceptions.
+      # Think of this as like a "defer cleanup()" in the Go language.
+      local $cleanup_guard = Scope::Guard->new(\&Bugzilla::cleanup);
 
       # Ensure the request_cache is always cleared prior to every request,
       # regardless of routing or Bugzilla::App wrapping.
@@ -48,6 +55,7 @@ sub register {
       # We also need to clear CGI's globals.
       CGI::initialize_globals();
       Bugzilla->usage_mode(USAGE_MODE_MOJO);
+      $next->();
     }
   );