Skip to content

Commit

Permalink
Don't serve requests until startup complete (#498)
Browse files Browse the repository at this point in the history
* Don't serve requests until startup complete

* Send lifespan shutdown on socket bind exception

* Black formatter
  • Loading branch information
zhammer authored and tomchristie committed Jan 20, 2020
1 parent ded37bc commit 4b3117c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ async def serve(self, sockets=None):
)

async def startup(self, sockets=None):
await self.lifespan.startup()
if self.lifespan.should_exit:
self.should_exit = True
return

config = self.config

create_protocol = functools.partial(
Expand Down Expand Up @@ -464,6 +469,7 @@ async def startup(self, sockets=None):
)
except OSError as exc:
logger.error(exc)
await self.lifespan.shutdown()
sys.exit(1)
port = config.port
if port == 0:
Expand All @@ -484,11 +490,7 @@ async def startup(self, sockets=None):
)
self.servers = [server]

await self.lifespan.startup()
if self.lifespan.should_exit:
self.should_exit = True
else:
self.started = True
self.started = True

async def main_loop(self):
counter = 0
Expand Down

0 comments on commit 4b3117c

Please sign in to comment.