From 7a585c271269b63655a23798fabaf82134eab47c Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:07:14 +0100 Subject: [PATCH 1/5] Add POSIX compliant cleanup to php artisan serve Fixes #49941 --- src/Illuminate/Foundation/Console/ServeCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 194c80fa5127..e7f82fc45ed8 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -89,6 +89,18 @@ public function handle() $process = $this->startProcess($hasEnvironment); + if (function_exists('pcntl_signal')) { + $cleanup = function ($signo) use ($process) { + if ($process->isRunning()) { + $process->stop(10, $signo); + } + exit(); + }; + pcntl_signal(SIGINT, $cleanup); + pcntl_signal(SIGTERM, $cleanup); + pcntl_signal(SIGHUP, $cleanup); + } + while ($process->isRunning()) { if ($hasEnvironment) { clearstatcache(false, $environmentFile); From 693ced0017ccc239f6ccc6f84d59ee4852416f2e Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:09:07 +0100 Subject: [PATCH 2/5] Style CI --- src/Illuminate/Foundation/Console/ServeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index e7f82fc45ed8..600166a36e85 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -94,13 +94,13 @@ public function handle() if ($process->isRunning()) { $process->stop(10, $signo); } - exit(); + exit; }; pcntl_signal(SIGINT, $cleanup); pcntl_signal(SIGTERM, $cleanup); pcntl_signal(SIGHUP, $cleanup); } - + while ($process->isRunning()) { if ($hasEnvironment) { clearstatcache(false, $environmentFile); From dddc9d982cf500cc057e06d3abae526aa91e4763 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:28:36 +0100 Subject: [PATCH 3/5] Move cleanup to a better location --- .../Foundation/Console/ServeCommand.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 600166a36e85..275d84651f49 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -89,18 +89,6 @@ public function handle() $process = $this->startProcess($hasEnvironment); - if (function_exists('pcntl_signal')) { - $cleanup = function ($signo) use ($process) { - if ($process->isRunning()) { - $process->stop(10, $signo); - } - exit; - }; - pcntl_signal(SIGINT, $cleanup); - pcntl_signal(SIGTERM, $cleanup); - pcntl_signal(SIGHUP, $cleanup); - } - while ($process->isRunning()) { if ($hasEnvironment) { clearstatcache(false, $environmentFile); @@ -152,6 +140,18 @@ protected function startProcess($hasEnvironment) return in_array($key, static::$passthroughVariables) ? [$key => $value] : [$key => false]; })->all()); + if (function_exists('pcntl_signal')) { + $cleanup = function ($signo) use ($process) { + if ($process->isRunning()) { + $process->stop(10, $signo); + } + exit; + }; + pcntl_signal(SIGINT, $cleanup); + pcntl_signal(SIGTERM, $cleanup); + pcntl_signal(SIGHUP, $cleanup); + } + $process->start($this->handleProcessOutput()); return $process; From e1ab475f9687d2e5a380e839a3c516b84cf5581b Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:49:31 +0100 Subject: [PATCH 4/5] Use built in server abstraction --- .../Foundation/Console/ServeCommand.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 275d84651f49..6c59765cbea8 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -140,17 +140,12 @@ protected function startProcess($hasEnvironment) return in_array($key, static::$passthroughVariables) ? [$key => $value] : [$key => false]; })->all()); - if (function_exists('pcntl_signal')) { - $cleanup = function ($signo) use ($process) { - if ($process->isRunning()) { - $process->stop(10, $signo); - } - exit; - }; - pcntl_signal(SIGINT, $cleanup); - pcntl_signal(SIGTERM, $cleanup); - pcntl_signal(SIGHUP, $cleanup); - } + $this->trap([SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signo) use ($process) { + if ($process->isRunning()) { + $process->stop(10, $signo); + } + exit; + }); $process->start($this->handleProcessOutput()); From 01052fe842c2d926ac4367f226fe5540b0e9143b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 2 Feb 2024 10:48:28 -0600 Subject: [PATCH 5/5] Update ServeCommand.php --- src/Illuminate/Foundation/Console/ServeCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 6c59765cbea8..416c8a2801ae 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -140,10 +140,11 @@ protected function startProcess($hasEnvironment) return in_array($key, static::$passthroughVariables) ? [$key => $value] : [$key => false]; })->all()); - $this->trap([SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signo) use ($process) { + $this->trap([SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signal) use ($process) { if ($process->isRunning()) { - $process->stop(10, $signo); + $process->stop(10, $signal); } + exit; });