Implementation:Sgl project Sglang Engine Shutdown
| Knowledge Sources | |
|---|---|
| Domains | LLM_Serving, Resource_Management |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for shutting down the SGLang Engine and terminating all associated subprocesses.
Description
The Engine.shutdown() method calls kill_process_tree on the current process ID (with include_parent=False) to terminate all child subprocesses — the Scheduler and DetokenizerManager. This is also registered as an atexit handler during initialization and called automatically when using the context manager (with statement).
Usage
Call Engine.shutdown() when you are done with inference to release GPU memory and system resources. Prefer the context manager pattern for automatic cleanup.
Code Reference
Source Location
- Repository: sglang
- File: python/sglang/srt/entrypoints/engine.py
- Lines: L443-445
Signature
def shutdown(self):
"""Shutdown the engine"""
kill_process_tree(os.getpid(), include_parent=False)
Import
# Engine.shutdown() is a method on the Engine instance
engine.shutdown()
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | Engine | Yes | Active Engine instance to shut down |
Outputs
| Name | Type | Description |
|---|---|---|
| (none) | None | All subprocesses terminated, resources freed |
Usage Examples
Explicit Shutdown
import sglang as sgl
engine = sgl.Engine(model_path="meta-llama/Llama-3.1-8B-Instruct")
output = engine.generate("Hello!", {"max_new_tokens": 32})
print(output["text"])
# Explicit shutdown
engine.shutdown()
Context Manager (Recommended)
import sglang as sgl
with sgl.Engine(model_path="meta-llama/Llama-3.1-8B-Instruct") as engine:
output = engine.generate("Hello!", {"max_new_tokens": 32})
print(output["text"])
# Automatically calls engine.shutdown() on exit