Skip to content

Commit

Permalink
Suppress side-effects of signal propagation (#2317)
Browse files Browse the repository at this point in the history
* supress tracebacks on termination

* always clean up unix sockets

* OS specific coverage

* not covered on any OS

---------

Co-authored-by: Marcelo Trylesinski <[email protected]>
  • Loading branch information
maxfischer2781 and Kludex authored May 24, 2024
1 parent 14ffba8 commit 22873a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
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

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

0 comments on commit 22873a9

Please sign in to comment.