Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ForkController
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 7
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 start
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
182
 getChildNumber
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 prepareEnvironment
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 forkWorkers
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 initChild
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 handleTermSignal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Maintenance;
22
23use MediaWiki\MediaWikiServices;
24use RedisConnectionPool;
25use RuntimeException;
26
27/**
28 * Manage forking inside CLI maintenance scripts.
29 *
30 * Only handles forking and process control. In the future, this could
31 * be extended to provide IPC and job dispatch.
32 *
33 * This class requires the posix and pcntl extensions.
34 *
35 * @ingroup Maintenance
36 */
37class ForkController {
38    /** @var array|null */
39    protected $children = [];
40
41    /** @var int */
42    protected $childNumber = 0;
43
44    /** @var bool */
45    protected $termReceived = false;
46
47    /** @var int */
48    protected $flags = 0;
49
50    /** @var int */
51    protected $procsToStart = 0;
52
53    protected static $RESTARTABLE_SIGNALS = [];
54
55    /**
56     * Pass this flag to __construct() to cause the class to automatically restart
57     * workers that exit with non-zero exit status or a signal such as SIGSEGV.
58     */
59    private const RESTART_ON_ERROR = 1;
60
61    /**
62     * @param int $numProcs The number of worker processes to fork
63     * @param int $flags
64     */
65    public function __construct( $numProcs, $flags = 0 ) {
66        if ( !wfIsCLI() ) {
67            throw new RuntimeException( "MediaWiki\Maintenance\ForkController cannot be used from the web." );
68        } elseif ( !extension_loaded( 'pcntl' ) ) {
69            throw new RuntimeException(
70                'MediaWiki\Maintenance\ForkController requires pcntl extension to be installed.'
71            );
72        } elseif ( !extension_loaded( 'posix' ) ) {
73            throw new RuntimeException(
74                'MediaWiki\Maintenance\ForkController requires posix extension to be installed.'
75            );
76        }
77        $this->procsToStart = $numProcs;
78        $this->flags = $flags;
79
80        // Define this only after confirming PCNTL support
81        self::$RESTARTABLE_SIGNALS = [
82            SIGFPE, SIGILL, SIGSEGV, SIGBUS,
83            SIGABRT, SIGSYS, SIGPIPE, SIGXCPU, SIGXFSZ,
84        ];
85    }
86
87    /**
88     * Start the child processes.
89     *
90     * This should only be called from the command line. It should be called
91     * as early as possible during execution.
92     *
93     * This will return 'child' in the child processes. In the parent process,
94     * it will run until all the child processes exit or a TERM signal is
95     * received. It will then return 'done'.
96     * @return string
97     */
98    public function start() {
99        // Trap SIGTERM
100        pcntl_signal( SIGTERM, [ $this, 'handleTermSignal' ], false );
101
102        do {
103            // Start child processes
104            if ( $this->procsToStart ) {
105                if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
106                    return 'child';
107                }
108                $this->procsToStart = 0;
109            }
110
111            // Check child status
112            $status = false;
113            $deadPid = pcntl_wait( $status );
114
115            if ( $deadPid > 0 ) {
116                // Respond to child process termination
117                unset( $this->children[$deadPid] );
118                if ( $this->flags & self::RESTART_ON_ERROR ) {
119                    if ( pcntl_wifsignaled( $status ) ) {
120                        // Restart if the signal was abnormal termination
121                        // Don't restart if it was deliberately killed
122                        $signal = pcntl_wtermsig( $status );
123                        if ( in_array( $signal, self::$RESTARTABLE_SIGNALS ) ) {
124                            echo "Worker exited with signal $signal, restarting\n";
125                            $this->procsToStart++;
126                        }
127                    } elseif ( pcntl_wifexited( $status ) ) {
128                        // Restart on non-zero exit status
129                        $exitStatus = pcntl_wexitstatus( $status );
130                        if ( $exitStatus != 0 ) {
131                            echo "Worker exited with status $exitStatus, restarting\n";
132                            $this->procsToStart++;
133                        } else {
134                            echo "Worker exited normally\n";
135                        }
136                    }
137                }
138                // Throttle restarts
139                if ( $this->procsToStart ) {
140                    usleep( 500_000 );
141                }
142            }
143
144            // Run signal handlers
145            if ( function_exists( 'pcntl_signal_dispatch' ) ) {
146                pcntl_signal_dispatch();
147            } else {
148                declare( ticks=1 ) {
149                    // @phan-suppress-next-line PhanPluginDuplicateExpressionAssignment
150                    $status = $status;
151                }
152            }
153            // Respond to TERM signal
154            if ( $this->termReceived ) {
155                foreach ( $this->children as $childPid => $unused ) {
156                    posix_kill( $childPid, SIGTERM );
157                }
158                $this->termReceived = false;
159            }
160        } while ( count( $this->children ) );
161        pcntl_signal( SIGTERM, SIG_DFL );
162        return 'done';
163    }
164
165    /**
166     * Get the number of the child currently running. Note, this
167     * is not the pid, but rather which of the total number of children
168     * we are
169     * @return int
170     */
171    public function getChildNumber() {
172        return $this->childNumber;
173    }
174
175    protected function prepareEnvironment() {
176        // Don't share DB, storage, or memcached connections
177        MediaWikiServices::resetChildProcessServices();
178        MediaWikiServices::getInstance()->getObjectCacheFactory()->clear();
179        RedisConnectionPool::destroySingletons();
180    }
181
182    /**
183     * Fork a number of worker processes.
184     *
185     * @param int $numProcs
186     * @return string
187     */
188    protected function forkWorkers( $numProcs ) {
189        $this->prepareEnvironment();
190
191        // Create the child processes
192        for ( $i = 0; $i < $numProcs; $i++ ) {
193            // Do the fork
194            $pid = pcntl_fork();
195            if ( $pid === -1 ) {
196                echo "Error creating child processes\n";
197                exit( 1 );
198            }
199
200            if ( !$pid ) {
201                $this->initChild();
202                $this->childNumber = $i;
203                return 'child';
204            } else {
205                // This is the parent process
206                $this->children[$pid] = true;
207            }
208        }
209
210        return 'parent';
211    }
212
213    protected function initChild() {
214        $this->children = null;
215        pcntl_signal( SIGTERM, SIG_DFL );
216    }
217
218    protected function handleTermSignal( $signal ) {
219        $this->termReceived = true;
220    }
221}
222
223/** @deprecated class alias since 1.40 */
224class_alias( ForkController::class, 'ForkController' );