Skip to content

Threads not properly killed on windows #684

Description

@racinmat

🐛 Bug

On windows, killing

To Reproduce

I opened Lightning Studio but it redirected me to https://lightning.ai/templates. Below I include fully python code needed for replication.

Code sample

Code sample:

import os
import sys
import threading
import time

from litserve import LitAPI, LitServer

class _MinimalAPI(LitAPI):
    def setup(self, device: str) -> None:
        self.model = lambda x: x * 2

    def decode_request(self, request: dict) -> float:
        return float(request["input"])

    def predict(self, x: float) -> float:
        return self.model(x)

    def encode_response(self, output: float) -> dict:
        return {"output": output}

if __name__ == "__main__":
    print(f"[MAIN] PID={os.getpid()}  platform={sys.platform}")
    print("[MAIN] Starting LitServer...")
    server = LitServer(
        _MinimalAPI(),
        accelerator="cpu",
        devices=1,
        workers_per_device=1,
    )
    def _prompt():
        time.sleep(4)  # wait for uvicorn + inference workers to be ready
        print("[MAIN] Server fully running — click Stop in PyCharm to reproduce the bug.")

    threading.Thread(target=_prompt, daemon=True).start()
    server.run(port=8765, generate_client_file=False, log_level="info")

output when start debug mode and then click the Stop icon:

C:\Projects\others\LitServe\.venv\Scripts\python.exe -X pycache_prefix=C:\Users\Matej.Racinsky\AppData\Local\JetBrains\PyCharm2026.1\cpython-cache C:/Users/Matej.Racinsky/AppData/Local/Programs/PyCharm/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 60226 --file C:\Projects\others\LitServe\litserve_win_shutdown2.py 
import sys; print('Python %s on %s' % (sys.version, sys.platform))
Connected to pydev debugger (build 261.24374.152)
[MAIN] PID=68424  platform=win32
[MAIN] Starting LitServer...
INFO:     Uvicorn running on http://0.0.0.0:8765 (Press CTRL+C to quit)
C:\Projects\others\LitServe\src\litserve\server.py:1500: UserWarning: Windows does not support forking. Using threads api_server_worker_type will be set to 'thread'
  warnings.warn(
[MAIN] Server fully running — click Stop in PyCharm NOW to reproduce / verify the fix.
Swagger UI is available at http://0.0.0.0:8765/docs
INFO:     Started server process [68424]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
2026-06-01 19:11:40,618 - MainProcess[68424] - litserve.server - INFO - server.py:1532 - KeyboardInterrupt received. Initiating graceful shutdown.
2026-06-01 19:11:40,618 - MainProcess[68424] - litserve.server - INFO - server.py:1190 - Shutting down LitServe...

the process keeps handing and never ends.

output when I run the code in PyChamr regularly and then click the Stop icon.

C:\Projects\others\LitServe\.venv\Scripts\python.exe C:\Projects\others\LitServe\litserve_win_shutdown2.py 
[MAIN] PID=34132  platform=win32
[MAIN] Starting LitServer...
INFO:     Uvicorn running on http://0.0.0.0:8765 (Press CTRL+C to quit)
C:\Projects\others\LitServe\src\litserve\server.py:1500: UserWarning: Windows does not support forking. Using threads api_server_worker_type will be set to 'thread'
  warnings.warn(
[MAIN] Server fully running — click Stop in PyCharm NOW to reproduce / verify the fix.
Swagger UI is available at http://0.0.0.0:8765/docs
INFO:     Started server process [34132]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
2026-06-01 19:17:06,069 - MainProcess[34132] - litserve.server - INFO - server.py:1532 - KeyboardInterrupt received. Initiating graceful shutdown.
2026-06-01 19:17:06,069 - MainProcess[34132] - litserve.server - INFO - server.py:1190 - Shutting down LitServe...
2026-06-01 19:17:06,069 - MainProcess[34132] - litserve.server - ERROR - server.py:1228 - Error during termination of LitServer-0 (PID: 57976): 'Thread' object has no attribute 'terminate'

The process ended, but I saw this error.

Expected behavior

I expect that in both cases, the process will end without any error.

Environment

If you published a Studio with your bug report, we can automatically get this information. Otherwise, please describe:

  • PyTorch/Jax/Tensorflow Version (e.g., 1.0): None needed
  • OS (e.g., Linux): Windows 11
  • How you installed PyTorch (conda, pip, source): not used
  • Build command you used (if compiling from source): uv sync
  • Python version: 3.10.11
  • CUDA/cuDNN version: none
  • GPU models and configuration: none
  • Any other relevant information: none

Additional context

Full environment: I used main branch of LitServe and reproduced it, but I also replicated it with version 0.2.17.

Here is my pip freeze:

aiofile==3.9.0
aiohappyeyeballs==2.6.2
aiohttp==3.13.5
aiosignal==1.4.0
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
asgi-lifespan==2.1.0
async-timeout==5.0.1
attrs==26.1.0
Authlib==1.7.2
backports.asyncio.runner==1.2.0
backports.tarfile==1.2.0
beartype==0.22.9
cachetools==7.1.4
caio==0.9.25
certifi==2026.5.20
cffi==2.0.0
charset-normalizer==3.4.7
click==8.4.1
colorama==0.4.6
coverage==7.14.1
cryptography==48.0.0
cyclopts==4.16.1
distro==1.9.0
dnspython==2.8.0
docstring_parser==0.18.0
email-validator==2.3.0
exceptiongroup==1.3.1
fastapi==0.136.3
fastmcp==3.3.1
fastmcp-slim==3.3.1
filelock==3.29.0
frozenlist==1.8.0
fsspec==2026.4.0
griffelib==2.0.2
h11==0.16.0
hf-xet==1.5.0
httpcore==1.0.9
httptools==0.8.0
httpx==0.28.1
httpx-sse==0.4.3
huggingface_hub==1.17.0
idna==3.17
importlib_metadata==9.0.0
iniconfig==2.3.0
jaraco.classes==3.4.0
jaraco.context==6.1.2
jaraco.functools==4.5.0
Jinja2==3.1.6
jiter==0.15.0
joserfc==1.6.8
jsonargparse==4.49.0
jsonref==1.1.0
jsonschema==4.26.0
jsonschema-path==0.5.0
jsonschema-specifications==2025.9.1
keyring==25.7.0
librt==0.11.0
lightning==2.6.5
lightning-utilities==0.15.3
-e git+ssh://git@github.com/Lightning-AI/LitServe.git@a69b6354f634f636298daba2637d68075bf5b681#egg=litserve
markdown-it-py==4.2.0
MarkupSafe==3.0.3
mcp==1.27.2
mdurl==0.1.2
more-itertools==11.1.0
mpmath==1.3.0
multidict==6.7.1
mypy==1.20.0
mypy_extensions==1.1.0
networkx==3.4.2
numpy==2.2.6
openai==2.38.0
openapi-pydantic==0.5.1
opentelemetry-api==1.42.1
packaging==26.2
pathable==0.6.0
pathspec==1.1.1
pillow==12.2.0
platformdirs==4.10.0
pluggy==1.6.0
propcache==0.5.2
psutil==7.2.2
py-key-value-aio==0.4.5
pycparser==3.0
pydantic==2.13.4
pydantic-settings==2.14.1
pydantic_core==2.46.4
Pygments==2.20.0
PyJWT==2.13.0
pyperclip==1.11.0
pytest==9.0.3
pytest-asyncio==1.4.0
pytest-cov==7.1.0
pytest-retry==1.7.0
python-dotenv==1.2.2
python-multipart==0.0.30
pytorch-lightning==2.6.5
pywin32==311
pywin32-ctypes==0.2.3
PyYAML==6.0.3
pyzmq==27.1.0
referencing==0.37.0
regex==2026.5.9
requests==2.34.2
rich==15.0.0
rich-rst==2.0.1
rpds-py==0.30.0
safetensors==0.7.0
shellingham==1.5.4
sniffio==1.3.1
sse-starlette==3.4.4
starlette==1.2.1
sympy==1.14.0
tenacity==9.1.4
tokenizers==0.22.2
tomli==2.4.1
torch==2.12.0
torchmetrics==1.9.0
torchvision==0.27.0
tqdm==4.67.3
transformers==5.9.0
typer==0.25.1
typing-inspection==0.4.2
typing_extensions==4.15.0
uncalled-for==0.3.2
urllib3==2.7.0
uvicorn==0.48.0
watchfiles==1.2.0
websockets==16.0
yarl==1.24.2
zipp==4.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions