FastAPI server stuck on Windows
When developing FastAPI based web services, I sometimes run into a weird issue on Windows: I kill the uvicorn process, but the port it used remains in use and I get responses in the browser, even though the server appears to not be running.
Today I learned what's the problem: uvicorn spawns some child processes and if they don't get cleaned up properly, Windows won't clean up after parent process either (which is the one holding the port open).
First, we have to see the PID of the process that is listening on the socket and then we have to look for any processes that are it's children:
> (Get-NetTCPConnection -LocalPort 8000).OwningProcess
38876
> wmic process where ("ParentProcessId=38876") get Caption,ProcessId
Caption ProcessId
python.exe 1824
You can then use your favorite process killing tool: Task Manager, Resource Monitor or taskkill /F /PID 1824