SIGTERM cancels in-flight requests and the server always ends by SIGKILL

Open
#6,787 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with shutdown_event_loop and the signal lambda in gpustack/utils/process.py, then handle_termination_signal and its self-call to terminate_process_tree(os.getpid()). The lifespan shutdown in server/app.py shows what must run before exit. Done means in-flight requests finish during a SIGTERM drain, the process exits without SIGKILL, and tests in tests/utils cover both handlers. The author has a ready fix and an open question about the timeout env var, so confirm with a maintainer before starting.

Written by the indexing model from the issue text.

Description

On SIGTERM or Ctrl+C, shutdown_event_loop in gpustack/utils/process.py cancels every task in the loop, uvicorn's serve() and the request tasks included. Then terminate_process_tree(os.getpid()) sends SIGTERM to its own process, waits 3 s on its own pid and SIGKILLs it. uvicorn catches the same signal and would drain by itself, but the cancel gets there first.

I ran a server built from main (189a43a) with python -m gpustack.main start, PostgreSQL 17 and --gateway-mode disabled. A POST /v2/model-providers/test-model against a provider that answers after 4 s was in flight when SIGTERM arrived. It got HTTP 500 after 1.2 s, an open GET /v2/models?watch=true was cut at once, the app lifespan shutdown (the aiohttp sessions in server/app.py) never ran, and the process was SIGKILLed 3.4 s after SIGTERM. That happens on every stop. A worker registered to that server was SIGKILLed 6.1 s after SIGTERM with a lifespan CancelledError. The two CancelledError tracebacks followed by Killed in the verification log on #3026, and the ones in #5087, look like this.

Two smaller things in the same file. The loop handler's lambda doesn't bind sig, so Ctrl+C is logged as signal 15. And the sync handle_termination_signal that the serving, provisioning and benchmark children install has the same self-kill, so every child stop takes 3 s and ends with -9.

From the history it doesn't look intended: bb225f00 ("fix: graceful exit") ended with sys.exit(0), and faeb0cc7 three days later switched to terminate_process_tree(os.getpid()), reusing the helper for child trees on its own pid.

I have a fix ready. On a signal it sets should_exit on the uvicorn servers and waits for serve() to return before cancelling the other tasks, and it ends with os._exit(128 + sig) instead of signalling itself, so a terminated provisioning child still exits non-zero. With it the same request gets HTTP 200 after 4.0 s, the lifespan shutdown runs and the server exits with 143. Tests in tests/utils cover both handlers and fail on main.

The drain needs a bound, because watch streams never end by themselves. I used uvicorn's timeout_graceful_shutdown from a new GPUSTACK_GRACEFUL_SHUTDOWN_TIMEOUT, 5 s by default. Is an env var the way you'd want that configured, and should I open the PR?

Dominant language
Python
Stars
6.1k
Forks
750
Avg merge
1d 8h
Merged PRs (30d)
109

Getting set up

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 gpustack/gpustack

All issues in gpustack/gpustack

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.