uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

Ctrl+C in terminal broken on windows

Open davidefortunatotecnos opened this issue 1 year ago • 36 comments

With last release of Uvicorn 0.22.0 doing CTRL+C in a windows terminal doesn't work anymore. Everything is frozen and it is impossible to stop it. In addition the command --reload is broken too, because it doesn't restart anymore, probably because of the same issue.

Tried on Windows 11 and Powershell 7.3.4.

Downgrading to version 0.21.1 makes it works again.

[!IMPORTANT]

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

davidefortunatotecnos avatar May 08 '23 12:05 davidefortunatotecnos

same question

lzcmian avatar May 09 '23 13:05 lzcmian

when you launch without testing properly that will happen, also same here, in addition to that issue with "--reload" parameter over time it stucks and doesn't update api endpoints

UF-code avatar May 10 '23 06:05 UF-code

Also the issue of the everything being frozen is generally triggered right after watchfiles tries restarting the server/detected a change, also killing the process manually using the port number doesn't fully terminate it and does not allow for the port to be reused without a full system reboot.

AshminJayson avatar Jun 15 '23 21:06 AshminJayson

Crude monkey-patching hack to use SetConsoleCtrlHandler, which actually has its handlers called unlike signal.signal's. Don't add this to anything serious:

import sys

if __name__ == "__main__":
    if sys.platform == "win32":
        # Based on Eryk Sun's code: https://stackoverflow.com/a/43095532
        import ctypes

        from signal import SIGINT, CTRL_C_EVENT

        _console_ctrl_handlers = {} # must be global / kept alive to avoid GC

        kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

        PHANDLER_ROUTINE = ctypes.WINFUNCTYPE(
            ctypes.wintypes.BOOL,
            ctypes.wintypes.DWORD)

        def _errcheck_bool(result, func, args):
            if not result:
                raise ctypes.WinError(ctypes.get_last_error())
            return args

        kernel32.SetConsoleCtrlHandler.errcheck = _errcheck_bool
        kernel32.SetConsoleCtrlHandler.argtypes = (
            PHANDLER_ROUTINE,
            ctypes.wintypes.BOOL)

        from uvicorn.supervisors.multiprocess import Multiprocess
        uvicorn_multiprocess_startup_orig = Multiprocess.startup
        def uvicorn_multiprocess_startup(self, *args, **kwargs):
            ret = uvicorn_multiprocess_startup_orig(self, *args, **kwargs)

            def win_ctrl_handler(dwCtrlType):
                if (dwCtrlType == CTRL_C_EVENT and
                    not self.should_exit.is_set()):
                    kernel32.SetConsoleCtrlHandler(_console_ctrl_handlers[win_ctrl_handler], False)
                    self.signal_handler(SIGINT, None)
                    del _console_ctrl_handlers[win_ctrl_handler]
                    return True
                return False

            if win_ctrl_handler not in _console_ctrl_handlers:
                h = PHANDLER_ROUTINE(win_ctrl_handler)
                kernel32.SetConsoleCtrlHandler(h, True)
                _console_ctrl_handlers[win_ctrl_handler] = h

            return ret
        Multiprocess.startup = uvicorn_multiprocess_startup

    uvicorn.run("app", workers=4)

JinglingB avatar Sep 25 '23 02:09 JinglingB

Also the issue of the everything being frozen is generally triggered right after watchfiles tries restarting the server/detected a change, also killing the process manually using the port number doesn't fully terminate it and does not allow for the port to be reused without a full system reboot.

The current workaround which works for this is to run uvicorn from the main function of the app rather than having to run it directly as a command line command.

This ensures that the port is freed and also terminates the program correctly.


if __name__ == '__main__':
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

AshminJayson avatar Sep 25 '23 03:09 AshminJayson

Does not works for me @AshminJayson . I switched to hypercorn in the meantime.

if __name__ == '__main__':
    # https://github.com/encode/uvicorn/issues/1972
    asyncio.run(serve(app, Config()))

Sample:

PS C:\Users\olafr\test> python .\server.py
[2023-10-28 02:19:19 +0200] [13228] [INFO] Running on http://127.0.0.1:8000 (CTRL + C to quit)
2023-10-28 02:19:19,472 loglevel=INFO   logger=hypercorn.error info() L106  Running on http://127.0.0.1:8000 (CTRL + C to quit)
PS C:\Users\olafr\test>

olafrv avatar Oct 28 '23 00:10 olafrv

@olafrv can you check if the changes that are in this MR works on your side?

https://github.com/encode/uvicorn/pull/2059

humrochagf avatar Oct 28 '23 12:10 humrochagf

@humrochagf , tested, nothing changes, but thanks to your PR I nailed down the problem.

I'm on latests Windows 11 23H2 builtin Poweshell, VSCode 1.83.1, Python 3.12.0 (tags/v3.12.0:0fb18b0).

The signal is indeed received (CTRL+C), since the shutdown starts, both in 0.23.2 and your https://github.com/encode/uvicorn/pull/2059 .

However, the shutdown process hangs forever, giving the impression of not working, you only see: INFO: Shutting down

It does not matter if you pass or not the --reload and/or --reload-delay, so you end up doing: taskkill /f /im "uvicorn.exe"

I found that the server.wait_closed() never returns (or timeouts after minutes):

I guess the socket.close() is never closed until timeout?: https://github.com/encode/uvicorn/blob/732cef6ff7700e7c50e6123276f881d483897f43/uvicorn/server.py#L270

My dirty workaround:

      for server in self.servers:
            # await server.wait_closed()
            pass

I also saw Python has an open asyncio.Server.wait_closed() appears to hang indefinitely in 3.12.0a7 but I didn't check how it is related to the server.py logic in depth.

So you can merge https://github.com/encode/uvicorn/pull/2059 but won't solve the(my) problem.

It is reproducible even with this helloworld.py but it gets more evident on more complex apps:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"Hello": "World"}

olafrv avatar Oct 28 '23 21:10 olafrv

Alright, thanks for taking a look at it, I'll run some tests in python 3.12 to see if I can reproduce it 🙂

humrochagf avatar Oct 28 '23 22:10 humrochagf

There's a discussion on this issue here, along with a proposed fix: https://github.com/encode/uvicorn/discussions/2122

jcheng5 avatar Oct 30 '23 19:10 jcheng5

By the way, I'm having the same problem for a while, on Windows 10, both Python 3.9 and 3.11. Don't remember the version I was using with 3.9, but with 3.11 is 0.24.0.post1.

Downgrading to 0.21.1 seems to improve the reload behavior, but CTRL+C still doesn't work, with or without reload.

By the way, this is somewhat intermittent. It's broken most of the time, but sometimes it just works normally, just often enough to make me doubt my sanity, competence, and the nature of reality itself.

tonnydourado avatar Nov 30 '23 11:11 tonnydourado

Not entirely sure if this is the same issue but on Windows 10 I'm also having issues since a python 3.12 upgrade with the NiceGUI framework(that uses uvicorn under the hood for filewatching/serving).

Any time I edit & save a file while uvicorn is running, it seems to shut down some process and not properly pick up on file changes plus it's impossible to Ctrl+C out of the application unless I close the browser tab.

https://github.com/zauberzeug/nicegui/issues/2138

Python 3.11.x works fine, Python 3.12+ seems to break the filewatcher(unsure if the actual bug is in Uvicorn or in the asyncio/python 3.12/Windows combo). On Mac OS/Linux it works fine with Py 3.12, it's just Windows that's seemingly picky.

frankhuurman avatar Dec 11 '23 14:12 frankhuurman

I've been using this project for on Windows for quite a while and finally decided to spend some time tonight to look into why this annoying issue is happening.

There has been some great discussions over possible fixes to this issue, but it seems like it comes down to the parent process, supervisors.multiprocess.Multiprocess, being unable to receive any signals from pressing Ctrl+C.

Why is it the case? The Server subprocesses is able to receive the signals fine. Seems to be an open bug with CPython threading.Event.wait() on Windows.

@humrochagf seems to be on the right path with #2059 by changing the behavior to use a timeout to unblock the parent process and have it receive all the handled signals. Seems to be buggy still though.

The fix I've done was replacing the self.should_exit.wait() with a simple time.sleep() loop in supervisors.multiprocess.Multiprocess

    def run(self) -> None:
        self.startup()
        while not self.should_exit.is_set():
            time.sleep(.1)
        self.shutdown()

Seems to work with my build:

  • Windows 11
  • Python 3.12
  • uvicorn master branch

I'm sure it'll work for older versions. --reload works for me but still has some annoying CancelledError.

Mason-Chou avatar Jan 01 '24 08:01 Mason-Chou

Hello, now that we have #2183 landing on version 0.30.0 the new process manager seems to solve this issue.

If anyone that was also facing this issue is able to check with this version I guess we are good to close this one 🙂

humrochagf avatar May 29 '24 00:05 humrochagf

Same issue still happening for me on v0.30.1

spikecodes avatar Jun 02 '24 22:06 spikecodes

Hello, I just tried with version 0.30.1 and i confirm that the issue is still happening, it is not fixed

davidefortunatotecnos avatar Jun 03 '24 12:06 davidefortunatotecnos

Is this dependent on the OS version or shell/terminal used, perhaps? That would explain why it works for some people but not for others.

maxfischer2781 avatar Jun 03 '24 12:06 maxfischer2781

@maxfischer2781 i provide some additional information about OS and shell, that are changed a bit since the first post i made due to some Windows updates, but are pretty similar.

Today i tried with these versions: OS: Windows 11 23H2 Build 22631.3672 Powershell 7.4.2

If i downgrade to Uvicorn version 0.21.1 everything works fine, but every version i tried after the 0.21.1 always had the issue

davidefortunatotecnos avatar Jun 03 '24 12:06 davidefortunatotecnos

Encountered the same issue on Win10 with uvicorn 0.27.0.post1. When I then tested it with 0.21.2, 0.22.2 and 0.27.0.post1 again, everything worked okay (tested simple code changes).

It seems this bug is pretty rare and maybe happens only if something specific in code changes? Or even env files, can't be sure.

My command:

ENV_FILE := $(PWD)/.local.env
.PHONY: main
main:
    uvicorn project.app:create_app --factory --reload --port=8100 --env-file=$(ENV_FILE)

PS: using PyCharm 2024.1.2 if important. Also enabled feature new terminal. But since this issues doesn't always happen, I doubt it's related to PyCharm (at least).

Danipulok avatar Jun 04 '24 10:06 Danipulok

The same problem with uvicorn 0.30.1 on Win10.

The frequency of the is alarming. I just reopened IDE and everything work fine.

Nochnikov avatar Jun 19 '24 10:06 Nochnikov

My Linux get a similar problem , when Crtl+C in the envirenment with py:12.4 and uvi:0.30.1, it raise a "KeyboardInterrupt()" ; but when I back to the env with py:12.0 and uvi:0.23.2 , the Ctrl+C shutdown gracefully without any raise.

SomaLily avatar Jul 05 '24 08:07 SomaLily

this still happens with version 0.30.1 with windows 11 powershell 7.4.3

poetry shell uvicorn main:app --reload

when a file is modified everything freezes in this part: WARNING: WatchFiles detected changes in 'main.py'. Reloading...

ctrl + c does not work you need to kill all python processes

YuriFontella avatar Jul 06 '24 19:07 YuriFontella

uvicorn0.30.1 also has this problem. The Windows terminal consistently experiences issues, freezing whether you attempt to stop it with Ctrl+C or there's a file change. So, I switched to wsl and everything is fine now.

Abeautifulsnow avatar Jul 19 '24 06:07 Abeautifulsnow

So there is no way for stopping Uvicorn if its reloaded?

kk7188048 avatar Jul 26 '24 08:07 kk7188048

If someone with windows gives me a step by step on how to install Python and reproduce this on a windows 11 machine, I can debug it. 👍

Preferably with an editor that I can modify uvicorn internals.

Kludex avatar Jul 26 '24 08:07 Kludex

It's not limited to windows 11, windows 10 is affected as well. To replicate, just run with the --reload option. It may work correctly a few times - in my case I can 'CTRL + C' after the first run; any subsequent runs or simply after awhile it will not respond to 'CTRL + C'. To exit you have to have a second terminal open to kill python processes, in order to kill the uvicorn process.

tugofpeas avatar Aug 02 '24 20:08 tugofpeas

Use hypercorn, especially with fastapi. Uvicorn does not supprt all the features and is very unstable.

prochor666 avatar Aug 04 '24 12:08 prochor666

Very frustrating and it seems like this has been an issue for some time. Is there any update on attempts to fix this? The --reload is a fantastic feature, but as it stands windows development is even longer than non --reload as I have to kill the python process after every file save and restart.

Edfrost321 avatar Aug 10 '24 23:08 Edfrost321

Well @Edfrost321... Let me explain it.

It seems this issue is not reproducible in every Windows machine. You can just read this issue to notice that. I'm actually writing this comment from a Windows machine that can't reproduce it.

On the other hand, the ones that can reproduce the issue are only complaining and expecting the maintainer to solve this. And... I'm the only maintainer. I can't reproduce it. How do you expect me to solve it?

Anyone really interested, should open a pull request, or... I'm also open to give my address for people interested in sending me a Windows machine with this issue as well. Maybe you can ask Microsoft or JetBrains (https://github.com/encode/uvicorn/issues/2000).

Kludex avatar Aug 11 '24 07:08 Kludex