Repository: encode/uvicorn

Issue:
Lifespan `cleanup` should become `shutdown`
Naming in ASGI spec recently tweaked here. Good one of a contributor to jump on.
We’re first want to update Starlette to deal with *either* of the two names.


Discussion:

Resolving patch:
diff --git a/uvicorn/lifespan.py b/uvicorn/lifespan.py
--- a/uvicorn/lifespan.py
+++ b/uvicorn/lifespan.py
@@ -7,12 +7,12 @@
 
 
 class Lifespan:
-    def __init__(self, app, logger=None, startup_timeout=10, cleanup_timeout=10):
+    def __init__(self, app, logger=None, startup_timeout=10, shutdown_timeout=10):
         self.logger = logger or logging.getLogger("uvicorn")
         self.startup_timeout = startup_timeout
-        self.cleanup_timeout = cleanup_timeout
+        self.shutdown_timeout = shutdown_timeout
         self.startup_event = asyncio.Event()
-        self.cleanup_event = asyncio.Event()
+        self.shutdown_event = asyncio.Event()
         self.receive_queue = asyncio.Queue()
         try:
             self.asgi = app({'type': 'lifespan'})
@@ -39,12 +39,12 @@ async def run(self):
     async def send(self, message):
         if message['type'] == 'lifespan.startup.complete':
             assert not self.startup_event.is_set(), STATE_TRANSITION_ERROR
-            assert not self.cleanup_event.is_set(), STATE_TRANSITION_ERROR
+            assert not self.shutdown_event.is_set(), STATE_TRANSITION_ERROR
             self.startup_event.set()
-        elif message['type'] == 'lifespan.cleanup.complete':
+        elif message['type'] == 'lifespan.shutdown.complete':
             assert self.startup_event.is_set(), STATE_TRANSITION_ERROR
-            assert not self.cleanup_event.is_set(), STATE_TRANSITION_ERROR
-            self.cleanup_event.set()
+            assert not self.shutdown_event.is_set(), STATE_TRANSITION_ERROR
+            self.shutdown_event.set()
         else:
             error = 'Got invalid message type on lifespan protocol "%s"'
             raise RuntimeError(error % message['type'])
@@ -57,6 +57,6 @@ async def wait_startup(self):
         await self.receive_queue.put({'type': 'lifespan.startup'})
         await asyncio.wait_for(self.startup_event.wait(), timeout=self.startup_timeout)
 
-    async def wait_cleanup(self):
-        await self.receive_queue.put({'type': 'lifespan.cleanup'})
-        await asyncio.wait_for(self.cleanup_event.wait(), timeout=self.cleanup_timeout)
+    async def wait_shutdown(self):
+        await self.receive_queue.put({'type': 'lifespan.shutdown'})
+        await asyncio.wait_for(self.shutdown_event.wait(), timeout=self.shutdown_timeout)
diff --git a/uvicorn/main.py b/uvicorn/main.py
--- a/uvicorn/main.py
+++ b/uvicorn/main.py
@@ -410,7 +410,7 @@ async def tick(self):
 
         if self.lifespan.is_enabled:
             self.logger.info("Waiting for application cleanup.")
-            await self.lifespan.wait_cleanup()
+            await self.lifespan.wait_shutdown()
 
         self.loop.stop()
 


Test patch:
diff --git a/tests/test_lifespan.py b/tests/test_lifespan.py
--- a/tests/test_lifespan.py
+++ b/tests/test_lifespan.py
@@ -14,7 +14,7 @@ async def __aenter__(self):
         return self.app
 
     async def __aexit__(self, exc_type, exc, tb):
-        await self.lifespan.wait_cleanup()
+        await self.lifespan.wait_shutdown()
 
 
 def test_lifespan_enabled():
@@ -40,9 +40,9 @@ async def lifespan(receive, send):
             startup_complete = True
             await send({'type': 'lifespan.startup.complete'})
             message = await receive()
-            assert message['type'] == 'lifespan.cleanup'
+            assert message['type'] == 'lifespan.shutdown'
             cleanup_complete = True
-            await send({'type': 'lifespan.cleanup.complete'})
+            await send({'type': 'lifespan.shutdown.complete'})
         return lifespan
 
     async def test(app):