Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTRL^C doesn't work while startup in progress #1301

Open
2 tasks done
levsh opened this issue Dec 29, 2021 · 4 comments
Open
2 tasks done

CTRL^C doesn't work while startup in progress #1301

levsh opened this issue Dec 29, 2021 · 4 comments
Assignees

Comments

@levsh
Copy link

levsh commented Dec 29, 2021

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Hi!

ctrl^c doesn't work until on_startup function completes. This happens because install_signal_handlers call before startup

Steps to reproduce the bug

import asyncio

import uvicorn
from fastapi import FastAPI

async def on_startup():
    for _ in range(10):
        await asyncio.sleep(1)

app = FastAPI(on_startup=[on_startup])

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=5000, log_level="info")

Expected behavior

No response

Actual behavior

No response

Debugging material

No response

Environment

uvicorn - 0.16.0
python - 3.8.6

Additional context

No response

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
@msenol86
Copy link

msenol86 commented Jan 25, 2022

This happens to me too. This is my solution:

def receive_signal(signalNumber, frame):
    print('Received:', signalNumber)
    sys.exit()


@app.on_event("startup")
async def startup_event():
    import signal
    signal.signal(signal.SIGINT, receive_signal)
    # startup tasks

@tomchristie
Copy link
Member

This is probably a reasonable behaviour?

I'd assume that a sensible approach would be that we always allow the startup to run to completion, but then exit (and run shutdown) if we've been interrupted.

@Kludex Kludex self-assigned this May 5, 2022
@Kludex Kludex added this to the Version 0.21.0 milestone Oct 29, 2022
@jmwielandt
Copy link

This is probably a reasonable behaviour?

I'd assume that a sensible approach would be that we always allow the startup to run to completion, but then exit (and run shutdown) if we've been interrupted.

@tomchristie Hi, I'm having this problem. Imagine you had the wrong config and need to stop the program ASAP.
Ctrl-C doesn't work for any endpoint btw, uvicorn is waiting them to finish before ending the process.

Ctrl-C is mainly for development, where you can't discard a mistake or just a test of a while True: pass. With this, devs are forced to use htop or the task manager to end python.

@Kludex Kludex modified the milestones: Version 0.21.0, Version 0.22.0 Mar 22, 2023
@Birch-san
Copy link

This is probably a reasonable behaviour?

@tomchristie

the problem is that the thing the application may be doing on startup, is downloaing a giant file.

I was launching ialacol's uvicorn server, and on startup it began downloading a 4GB LLM checkpoint. the default model wasn't one I wished to use, so I tried to interrupt it in order to change the CLI args.
I tried to stop it by sending a Ctrl C to uvicorn, but nothing happened. the interrupt was buffered and only serviced after the download completed.

@Kludex Kludex removed this from the Version 0.22.0 milestone Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants