Skip to content

Commit

Permalink
Ignore SIGPIPE signal by default
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Dec 9, 2020
1 parent d70d45a commit 9647678
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void Server::worker_signal_init(void) {
SwooleG.use_signalfd = SwooleG.enable_signalfd;

swSignal_set(SIGHUP, nullptr);
swSignal_set(SIGPIPE, nullptr);
swSignal_set(SIGPIPE, SIG_IGN);
swSignal_set(SIGUSR1, nullptr);
swSignal_set(SIGUSR2, nullptr);
// swSignal_set(SIGINT, Server::worker_signal_handler);
Expand Down
58 changes: 58 additions & 0 deletions tests/swoole_process/ignore_sigpipe.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
swoole_process: ignore SIGPIPE
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Server;
use Swoole\Constant;

$GLOBALS['data'] = base64_encode(random_bytes(128));

$pm = new SwooleTest\ProcessManager;
$pm->setWaitTimeout(5);

$pm->parentFunc = function ($pid) use ($pm) {
echo "DONE\n";
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$serv = new Swoole\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
$serv->set(array(
"worker_num" => 1,
'hook_flags' => SWOOLE_HOOK_ALL,
'log_level' => SWOOLE_LOG_WARNING,
));
$serv->on("WorkerStart", function (Server $serv) use ($pm) {
$cli = new Co\Client(SWOOLE_SOCK_TCP);
if ($cli->connect('127.0.0.1', $pm->getFreePort(), 1) == false) {
echo "ERROR\n";
return;
}
while (1) {
usleep(10000);
if ($cli->send($GLOBALS['data']) == false) {
Assert::eq($cli->errCode, SOCKET_EPIPE);
break;
}
}
$pm->wakeup();
});

$serv->on(Constant::EVENT_CONNECT, function (Server $serv, $fd, $rid) {
$serv->close($fd);
});
$serv->on(Constant::EVENT_RECEIVE, function (Server $serv, $fd, $rid, $data) {

});
$serv->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE
64 changes: 64 additions & 0 deletions tests/swoole_process/ignore_sigpipe_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
swoole_process: close
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Server;
use Swoole\Constant;
use Swoole\Timer;
use Swoole\Process;
use Swoole\Atomic;

$GLOBALS['data'] = base64_encode(random_bytes(128));
$GLOBALS['atomic'] = new Atomic(0);

$pm = new SwooleTest\ProcessManager;
$pm->setWaitTimeout(5);

$pm->parentFunc = function ($pid) use ($pm) {
Co\run(function () use ($pm) {
$cli = new Co\Client(SWOOLE_SOCK_TCP);
if ($cli->connect('127.0.0.1', $pm->getFreePort(), 1) == false) {
echo "ERROR\n";
return;
}
while (1) {
usleep(10000);
if ($cli->send($GLOBALS['data']) == false) {
Assert::eq($cli->errCode, SOCKET_EPIPE);
break;
}
}
});
echo "DONE\n";
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$serv = new Swoole\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
$serv->set(array(
"worker_num" => 1,
'hook_flags' => SWOOLE_HOOK_ALL,
'log_level' => SWOOLE_LOG_WARNING,
));
$serv->on("WorkerStart", function (Server $serv) use ($pm) {
$pm->wakeup();
});

$serv->on(Constant::EVENT_CONNECT, function (Server $serv, $fd, $rid) {
$serv->close($fd);
});
$serv->on(Constant::EVENT_RECEIVE, function (Server $serv, $fd, $rid, $data) {

});
$serv->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE

0 comments on commit 9647678

Please sign in to comment.