 +        local $headers = {};
 +        local $err;
 +        no warnings;
++        my $child_pid = $$;
 +        local *CORE::GLOBAL::exit = sub {
-+            eval {
-+                $pipe->print($JSON->encode([$out, $headers, $err]));
-+                $pipe->close;
-+                1;
-+            } or warn $@;
-+            CORE::exit($_[0]//0);
++            my $exitcode = shift // 0;
++
++            # NOTE some scripts might fork (e.g.: tools/stage-marc-import.pl)
++            # in this case, the child WILL exit, and it WILL get here
++            # to avoid potential issues, i'll only "pipe" back from the main child
++            if ($child_pid == $$) {
++                eval {
++                    $pipe->print($JSON->encode([$out, $headers, $err]));
++                    $pipe->close;
++                    1;
++                } or warn $@;
++            }
++            CORE::exit($exitcode);
 +        };
 +        use warnings;
 +

+1;
diff --git a/Koha/Middleware/App.pm b/Koha/Middleware/App.pm
new file mode 100644
index 0000000..38585d9
--- /dev/null
+++ b/Koha/Middleware/App.pm
@@ -0,0 +1,165 @@
+use strict;
+use warnings;
+

+}
+
 1;
