Merge "CLI: Make sure we don't exit with 0 when an exception is encountered"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 5 Oct 2017 06:27:41 +0000 (06:27 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 5 Oct 2017 06:27:41 +0000 (06:27 +0000)
includes/exception/MWExceptionHandler.php

index a2ec391..9b59191 100644 (file)
@@ -51,7 +51,7 @@ class MWExceptionHandler {
         * Install handlers with PHP.
         */
        public static function installHandler() {
-               set_exception_handler( 'MWExceptionHandler::handleException' );
+               set_exception_handler( 'MWExceptionHandler::handleUncaughtException' );
                set_error_handler( 'MWExceptionHandler::handleError' );
 
                // Reserve 16k of memory so we can report OOM fatals
@@ -111,6 +111,25 @@ class MWExceptionHandler {
                self::logException( $e, self::CAUGHT_BY_HANDLER );
        }
 
+       /**
+        * Callback to use with PHP's set_exception_handler.
+        *
+        * @since 1.31
+        * @param Exception|Throwable $e
+        */
+       public static function handleUncaughtException( $e ) {
+               self::handleException( $e );
+
+               // Make sure we don't claim success on exit for CLI scripts (T177414)
+               if ( PHP_SAPI === 'cli' ) {
+                       register_shutdown_function(
+                               function () {
+                                       exit( 255 );
+                               }
+                       );
+               }
+       }
+
        /**
         * Exception handler which simulates the appropriate catch() handling:
         *