From: Dylan Hardison Date: Tue, 2 Jun 2020 01:49:17 +0000 (-0400) Subject: ensure cleanup is always called at the end of dispatch X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a0e705cb1990a32296fdbff7e15de390535d3cc1;p=thirdparty%2Fbugzilla.git ensure cleanup is always called at the end of dispatch --- diff --git a/Bugzilla/App/CGI.pm b/Bugzilla/App/CGI.pm index 924b90630..2c283a8be 100644 --- a/Bugzilla/App/CGI.pm +++ b/Bugzilla/App/CGI.pm @@ -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(); diff --git a/Bugzilla/App/Plugin/Glue.pm b/Bugzilla/App/Plugin/Glue.pm index 4b56629a6..f17ff7f0e 100644 --- a/Bugzilla/App/Plugin/Glue.pm +++ b/Bugzilla/App/Plugin/Glue.pm @@ -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->(); } );