Child keeps running after its client is killed by SIGTERM, SIGHUP or SIGKILL

Open
#764 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
linux, rails, ruby

Research direction

Start by reading Spring::Application#wait and the existing SIGINT forwarding path. Reproduce the issue with the Linux shell commands in the report, then trace how the client socket and spawned command process are managed. Done means child processes are cleaned up after the described client signals or socket EOF, and spring stop no longer waits on orphaned children.

Written by the indexing model from the issue text.

Description

When a Spring 4.7.0 client process ends by "deadly" signals such as SIGTERM, SIGKILL, SIGHUP, the command process spawned by the Spring application will keep running until it finishes on its own, or forever if it waits on something. That can cause problems when it's consuming resources or holding locks.

Minimal repro (tested on Linux):

bin/rails runner 'sleep 60' & sleep 10; kill -TERM $!  # or -HUP, -KILL
sleep 2; ps -eo pid,args | grep '[r]ails_runner'  # still running

SIGINT works as expected but it would be great if other signals were covered as well. It would be useful, for example, when...

  • timeout(1) times out (SIGTERM)
  • an agent subprocess is ended (often SIGTERM then SIGKILL)
  • a terminal window or tab is closed, or an SSH or docker session is lost (SIGHUP)
  • just using plain kill with default args (SIGTERM)

I'd be happy to prepare a PR for it if that's welcome. My proposed fix is to treat unexpected EOF on the client socket as the client's death in Spring::Application#wait and send TERM to the command's process group - that should cover all cases.

EDIT: Actually it would be more accurate to forward termination signals like TERM, HUP and ALRM the same way INT is forwarded, and use EOF as a catch-all for a crash (SEGV and the like, which would be a bit silly to forward) and most importantly KILL (which can't be forwarded).

EDIT: I've also noticed that with orphaned children, spring stop reports success but leaves the server and the application process (that spawned the orphan) running until all orphans have exited. That would be fixed as a side-effect ✨

Dominant language
Ruby
Stars
2.8k
Forks
343
PR merge metrics
No merged PRs in 30d

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rails/spring

All issues in rails/spring

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.