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

Suppress side-effects of signal propagation #2317

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions uvicorn/_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,10 @@ def subprocess_started(
# Logging needs to be setup again for each child.
config.configure_logging()

# Now we can call into `Server.run(sockets=sockets)`
target(sockets=sockets)
try:
# Now we can call into `Server.run(sockets=sockets)`
target(sockets=sockets)
except KeyboardInterrupt: # pragma: no cover
# supress the exception to avoid a traceback from subprocess.Popen
# the parent already expects us to end, so no vital information is lost
pass
22 changes: 12 additions & 10 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,18 @@ def run(
logger.warning("You must pass the application as an import string to enable 'reload' or " "'workers'.")
sys.exit(1)

if config.should_reload:
sock = config.bind_socket()
ChangeReload(config, target=server.run, sockets=[sock]).run()
elif config.workers > 1:
sock = config.bind_socket()
Multiprocess(config, target=server.run, sockets=[sock]).run()
else:
server.run()
if config.uds and os.path.exists(config.uds):
os.remove(config.uds) # pragma: py-win32
try:
if config.should_reload:
sock = config.bind_socket()
ChangeReload(config, target=server.run, sockets=[sock]).run()
elif config.workers > 1:
sock = config.bind_socket()
Multiprocess(config, target=server.run, sockets=[sock]).run()
else:
server.run()
finally:
if config.uds and os.path.exists(config.uds):
os.remove(config.uds) # pragma: py-win32
Comment on lines +579 to +580
Copy link
Member

@Kludex Kludex May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to Server.shutdown instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems sensible. I'll give it a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Sorry for the delay on follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've experimented with this but see no way to make it work well. The socket can be owned by either main:run (for reload and workers) or Server (otherwise). When moving cleanup to Server.shutdown I would have to duplicate it inside the reload and worker code as well.

So main:run seems adequate for cleanup, since it owns the Server as well.


if not server.started and not config.should_reload and config.workers == 1:
sys.exit(STARTUP_FAILURE)
Expand Down
Loading