Skip to content

Commit 0d5cffc

Browse files
committed
test case for signal snder zombie process
1 parent 82b31c4 commit 0d5cffc

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

PhpAmqpLib/Connection/Heartbeat/SIGHeartbeatSender.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class SIGHeartbeatSender extends AbstractSignalHeartbeatSender
2020
private $signal;
2121

2222
/**
23-
* @var int|null the PID (process ID) of the child process sending regular signals to manage heartbeats
23+
* @var int the PID (process ID) of the child process sending regular signals to manage heartbeats
2424
*/
2525
private $childPid;
2626

@@ -58,7 +58,7 @@ public function unregister(): void
5858
posix_kill($this->childPid, SIGKILL);
5959
pcntl_waitpid($this->childPid, $status);
6060
}
61-
$this->childPid = null;
61+
$this->childPid = 0;
6262
}
6363

6464
private function registerListener(int $interval): void
@@ -84,7 +84,7 @@ private function periodicAlarm(int $interval): void
8484
$slept = sleep($interval);
8585
if ($slept !== 0) {
8686
// interupted by signal from parent, exit immediately
87-
exit;
87+
die;
8888
}
8989
posix_kill($parent, $this->signal);
9090
}

tests/Functional/Connection/Heartbeat/SIGHeartbeatSenderTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function heartbeat_should_interrupt_non_blocking_action()
9393

9494
/**
9595
* @test
96+
* @runInSeparateProcess
9697
*/
9798
public function alarm_sig_should_be_registered_when_conn_is_writing()
9899
{
@@ -101,7 +102,7 @@ public function alarm_sig_should_be_registered_when_conn_is_writing()
101102
->disableOriginalConstructor()
102103
->getMockForAbstractClass();
103104

104-
$connection->expects($this->exactly(3))->method('isConnected')->willReturn(true);
105+
$connection->expects($this->atLeast(2))->method('isConnected')->willReturn(true);
105106
$connection->expects($this->once())->method('getHeartbeat')->willReturn($this->heartbeatTimeout);
106107
$connection->expects($this->exactly(2))
107108
->method('isWriting')
@@ -120,4 +121,26 @@ public function alarm_sig_should_be_registered_when_conn_is_writing()
120121

121122
$sender->unregister();
122123
}
124+
125+
/**
126+
* @test
127+
* @runInSeparateProcess
128+
* @outputBuffering disabled
129+
* @covers \PhpAmqpLib\Connection\Heartbeat\SIGHeartbeatSender::unregister()
130+
*/
131+
public function child_process_must_be_terminated_after_unregister()
132+
{
133+
$property = new \ReflectionProperty(get_class($this->sender), 'childPid');
134+
$property->setAccessible(true);
135+
136+
$this->sender->register();
137+
$pid = $property->getValue($this->sender);
138+
self::assertGreaterThan(0, $pid);
139+
140+
$this->sender->unregister();
141+
142+
$result = pcntl_waitpid($pid, $status, WNOHANG);
143+
self::assertEquals(-1, $result);
144+
self::assertEquals(0, $status);
145+
}
123146
}

0 commit comments

Comments
 (0)