File tree Expand file tree Collapse file tree 2 files changed +15
-11
lines changed Expand file tree Collapse file tree 2 files changed +15
-11
lines changed Original file line number Diff line number Diff line change @@ -59,21 +59,22 @@ public function __construct(int $timeout)
5959 */
6060 public function timeBoxed (callable $ code )
6161 {
62- $ existingHandler = pcntl_signal_get_handler (\SIGALRM );
62+ if (pcntl_alarm ($ this ->timeout ) !== 0 ) {
63+ throw new LockAcquireException ('Existing process alarm is not supported ' );
64+ }
65+
66+ $ origSignalHandler = pcntl_signal_get_handler (\SIGALRM );
6367
64- $ signal = pcntl_signal (\SIGALRM , function (): void {
68+ $ timeout = $ this ->timeout ;
69+ $ signalHandlerFx = static function () use ($ timeout ): void {
6570 throw new DeadlineException (sprintf (
6671 'Timebox hit deadline of %d seconds ' ,
67- $ this -> timeout
72+ $ timeout
6873 ));
69- });
70- if (!$ signal ) {
71- throw new LockAcquireException ('Could not install signal handler ' );
72- }
74+ };
7375
74- $ oldAlarm = pcntl_alarm ($ this ->timeout );
75- if ($ oldAlarm !== 0 ) {
76- throw new LockAcquireException ('Existing alarm was not expected ' );
76+ if (!pcntl_signal (\SIGALRM , $ signalHandlerFx )) {
77+ throw new LockAcquireException ('Failed to install signal handler ' );
7778 }
7879
7980 try {
@@ -83,7 +84,7 @@ public function timeBoxed(callable $code)
8384 try {
8485 pcntl_signal_dispatch ();
8586 } finally {
86- pcntl_signal (\SIGALRM , $ existingHandler );
87+ pcntl_signal (\SIGALRM , $ origSignalHandler );
8788 }
8889 }
8990 }
Original file line number Diff line number Diff line change @@ -48,16 +48,19 @@ public function testShouldNotTimeout(): void
4848 */
4949 public function testShouldFailOnExistingAlarm (): void
5050 {
51+ $ origSignalHandler = pcntl_signal_get_handler (\SIGALRM );
5152 try {
5253 pcntl_alarm (1 );
5354 $ timeout = new PcntlTimeout (1 );
5455
5556 $ this ->expectException (LockAcquireException::class);
57+ $ this ->expectExceptionMessage ('Existing process alarm is not supported ' );
5658 $ timeout ->timeBoxed (static function () {
5759 sleep (1 );
5860 });
5961 } finally {
6062 pcntl_alarm (0 );
63+ self ::assertSame ($ origSignalHandler , pcntl_signal_get_handler (\SIGALRM ));
6164 }
6265 }
6366
You can’t perform that action at this time.
0 commit comments