Skip to content

Commit a21f53e

Browse files
authored
Update pid when worker process exit (swoole#4240)
1 parent 527e083 commit a21f53e

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed

ext-src/php_swoole_process.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222

2323
void php_swoole_process_clean();
2424
int php_swoole_process_start(swoole::Worker *process, zval *zobject);
25+
swoole::Worker *php_swoole_process_get_worker(zval *zobject);
26+
void php_swoole_process_set_worker(zval *zobject, swoole::Worker *worker);

ext-src/swoole_process.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static sw_inline ProcessObject *php_swoole_process_fetch_object(zend_object *obj
4141
return (ProcessObject *) ((char *) obj - swoole_process_handlers.offset);
4242
}
4343

44-
static sw_inline Worker *php_swoole_process_get_worker(zval *zobject) {
44+
Worker *php_swoole_process_get_worker(zval *zobject) {
4545
return php_swoole_process_fetch_object(Z_OBJ_P(zobject))->worker;
4646
}
4747

@@ -506,7 +506,7 @@ static PHP_METHOD(swoole_process, kill) {
506506
int ret = swoole_kill((int) pid, (int) sig);
507507
if (ret < 0) {
508508
if (!(sig == 0 && errno == ESRCH)) {
509-
php_swoole_sys_error(E_WARNING, "swKill(%d, %d) failed", (int) pid, (int) sig);
509+
php_swoole_sys_error(E_WARNING, "kill(%d, %d) failed", (int) pid, (int) sig);
510510
}
511511
RETURN_FALSE;
512512
}

ext-src/swoole_process_pool.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ struct ProcessPoolObject {
4040
zend_object std;
4141
};
4242

43+
static void pool_signal_handler(int sig);
44+
4345
static sw_inline ProcessPoolObject *php_swoole_process_pool_fetch_object(zend_object *obj) {
4446
return (ProcessPoolObject *) ((char *) obj - swoole_process_pool_handlers.offset);
4547
}
@@ -128,6 +130,7 @@ static PHP_METHOD(swoole_process_pool, write);
128130
static PHP_METHOD(swoole_process_pool, detach);
129131
static PHP_METHOD(swoole_process_pool, getProcess);
130132
static PHP_METHOD(swoole_process_pool, start);
133+
static PHP_METHOD(swoole_process_pool, stop);
131134
static PHP_METHOD(swoole_process_pool, shutdown);
132135
SW_EXTERN_C_END
133136

@@ -177,6 +180,7 @@ static const zend_function_entry swoole_process_pool_methods[] =
177180
PHP_ME(swoole_process_pool, write, arginfo_swoole_process_pool_write, ZEND_ACC_PUBLIC)
178181
PHP_ME(swoole_process_pool, detach, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC)
179182
PHP_ME(swoole_process_pool, start, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC)
183+
PHP_ME(swoole_process_pool, stop, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC)
180184
PHP_ME(swoole_process_pool, shutdown, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC)
181185
PHP_FE_END
182186
};
@@ -205,7 +209,6 @@ static void pool_onWorkerStart(ProcessPool *pool, int worker_id) {
205209
php_swoole_process_clean();
206210
SwooleG.process_id = worker_id;
207211
current_pool = pool;
208-
209212
// main function
210213
if (!pp->onWorkerStart) {
211214
return;
@@ -214,6 +217,9 @@ static void pool_onWorkerStart(ProcessPool *pool, int worker_id) {
214217
if (pp->enable_coroutine && php_swoole_reactor_init() < 0) {
215218
return;
216219
}
220+
if (!pp->enable_coroutine && pp->onMessage) {
221+
swSignal_set(SIGTERM, pool_signal_handler);
222+
}
217223
zval args[2];
218224
args[0] = *zobject;
219225
ZVAL_LONG(&args[1], worker_id);
@@ -259,6 +265,9 @@ static void pool_onWorkerStop(ProcessPool *pool, int worker_id) {
259265
}
260266

261267
static void pool_signal_handler(int sig) {
268+
if (!current_pool) {
269+
return;
270+
}
262271
switch (sig) {
263272
case SIGTERM:
264273
current_pool->running = false;
@@ -546,8 +555,6 @@ static PHP_METHOD(swoole_process_pool, start) {
546555
}
547556
}
548557

549-
extern void php_swoole_process_set_worker(zval *zobject, Worker *worker);
550-
551558
static PHP_METHOD(swoole_process_pool, detach) {
552559
if (current_pool == nullptr) {
553560
RETURN_FALSE;
@@ -608,11 +615,23 @@ static PHP_METHOD(swoole_process_pool, getProcess) {
608615
zend::Process *proc = new zend::Process(zend::PIPE_TYPE_STREAM, pp->enable_coroutine);
609616
worker->ptr2 = proc;
610617
(void) add_index_zval(zworkers, worker_id, zprocess);
618+
} else {
619+
auto _worker = php_swoole_process_get_worker(zprocess);
620+
if (_worker->pid != current_pool->workers[worker_id].pid) {
621+
_worker->pid = current_pool->workers[worker_id].pid;
622+
zend_update_property_long(swoole_process_ce, SW_Z8_OBJ_P(zprocess), ZEND_STRL("pid"), _worker->pid);
623+
}
611624
}
612625

613626
RETURN_ZVAL(zprocess, 1, 0);
614627
}
615628

629+
static PHP_METHOD(swoole_process_pool, stop) {
630+
if (current_pool) {
631+
current_pool->running = false;
632+
}
633+
}
634+
616635
static PHP_METHOD(swoole_process_pool, shutdown) {
617636
zval *retval =
618637
sw_zend_read_property_ex(swoole_process_pool_ce, ZEND_THIS, SW_ZSTR_KNOWN(SW_ZEND_STR_MASTER_PID), 0);

include/swoole_process_pool.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,9 @@ struct ProcessPool;
8787
struct Worker;
8888

8989
struct WorkerGlobal {
90-
/**
91-
* Always run
92-
*/
9390
bool run_always;
9491
bool shutdown;
95-
/**
96-
* pipe_worker
97-
*/
98-
int pipe_used;
99-
10092
uint32_t max_request;
101-
10293
String **output_buffer;
10394
Worker *worker;
10495
time_t exit_time;
@@ -299,9 +290,6 @@ static sw_inline int swoole_waitpid(pid_t __pid, int *__stat_loc, int __options)
299290
}
300291

301292
static sw_inline int swoole_kill(pid_t __pid, int __sig) {
302-
if (__pid <= 0) {
303-
return -1;
304-
}
305293
return kill(__pid, __sig);
306294
}
307295

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
swoole_process_pool: getProcess [3]
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc';
5+
?>
6+
--FILE--
7+
<?php
8+
require __DIR__ . '/../include/bootstrap.php';
9+
10+
use Swoole\Process\Pool;
11+
use Swoole\Process;
12+
13+
const N = 70000;
14+
15+
$pool = new Pool(2, SWOOLE_IPC_UNIXSOCK);
16+
17+
$pool->on('workerStart', function (Swoole\Process\Pool $pool, int $workerId) {
18+
if ($workerId == 0) {
19+
usleep(1000);
20+
$process1 = $pool->getProcess(1);
21+
phpt_var_dump($process1);
22+
$pid1 = $process1->pid;
23+
Process::kill($process1->pid, SIGTERM);
24+
usleep(10000);
25+
$process2 = $pool->getProcess(1);
26+
phpt_var_dump($process2);
27+
$pid2 = $process2->pid;
28+
Assert::notEq($pid1, $pid2);
29+
$pool->shutdown();
30+
}
31+
});
32+
33+
$pool->on("message", function ($pool, $data) {
34+
35+
});
36+
37+
$pool->start();
38+
?>
39+
--EXPECT--

0 commit comments

Comments
 (0)