Skip to content

[10.x] Add POSIX compliant cleanup to artisan serve - #49943

Merged
taylorotwell merged 5 commits into
laravel:10.xfrom
Tofandel:patch-1
Feb 2, 2024
Merged

taylorotwell merged 5 commits into
laravel:10.xfrom
Tofandel:patch-1

Conversation

@Tofandel

@Tofandel Tofandel commented Feb 1, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #49941

Description

When running php artisan serve it creates a subprocess for the server, but it seems to simply forward all it's input to the subprocess which is only listening for Ctrl + C on the keyboard to close itself which in turn exits the parent, this is is not POSIX compliant

As such when we kill the php artisan serve command it doesn't do any cleanup itself and the subprocess keeps running

Steps To Reproduce

  1. Run php artisan serve
  2. Open a second terminal
  3. Run ps aux and get the pid of php artisan serve
    image
  4. Run kill -SIGTINT $the_pid which should be equivalent to a Ctrl + C signal
  5. Check ps aux and notice that the main process is gone but the subprocess is still there
    image

This is a big problem when spawning php artisan serve from within another program, because programs should only use signals to kill their offsprings

With this PR if pcntl is enabled (it's optional but recommended in composer) any termination signal will trigger the cleanup of the subprocess

@Tofandel Tofandel changed the title Add POSIX compliant cleanup to php artisan serve [10.x] Add POSIX compliant cleanup to artisan serve Feb 1, 2024
@driesvints

Copy link
Copy Markdown
Member

@Tofandel could you please add a full description to your PR and not just link to an issue? (See the template when you open PR's). Thanks!

@rojtjo

rojtjo commented Feb 1, 2024

Copy link
Copy Markdown
Contributor

You might be able to use the built in signal abstractions: https://laravel.com/docs/10.x/artisan#signal-handling

@Tofandel

Tofandel commented Feb 1, 2024 •

Copy link
Copy Markdown
Contributor Author

@rojtjo Indeed it's much better, forgot about it

Tofandel

This comment was marked as outdated.

@driesvints

Copy link
Copy Markdown
Member

Heya. We got a report that this doesn't works on Windows: #50022 so just skipping that there for now: #50023

@Tofandel

Tofandel commented Feb 9, 2024

Copy link
Copy Markdown
Contributor Author

It would be the case if pcntl wasn't activated in any env I think

I got a false sense of security using the wrapper, that it would only run with pcntl enabled, but I forgot that the constants would be evaluated even if the callback doesn't run

Signals::whenAvailable(function () use ($signals, $callback) {

A quick fix would be to wrap the ->trap in Signals::whenAvailable(fn () => $this->trap())

A future improvement for trap then might be to provide the signals in a callback as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Artisan serve doesn't kill it's subprocess if killed with a signal instead of Ctrl + C

4 participants