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:
tomchristie: Looks great! Let’s make sure we’ve also got a new release of Starlette that handles *both* cases before we merge this in and release.

Patch:
diff --git a/uvicorn/lifespan.py b/uvicorn/lifespan.py
index aa53860..e419beb 100644
--- a/uvicorn/lifespan.py
+++ b/uvicorn/lifespan.py
@@ -7,12 +7,12 @@ STATE_TRANSITION_ERROR = 'Got invalid state transition on lifespan protocol.'
 
 
 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 @@ class Lifespan:
     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 @@ class Lifespan:
         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
index 5119352..2066a0c 100644
--- a/uvicorn/main.py
+++ b/uvicorn/main.py
@@ -410,7 +410,7 @@ class Server:
 
         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()
 
diff --git a/uvicorn/middleware/wsgi.py b/uvicorn/middleware/wsgi.py
index 82e45ec..901a260 100644
--- a/uvicorn/middleware/wsgi.py
+++ b/uvicorn/middleware/wsgi.py
@@ -55,6 +55,7 @@ class WSGIMiddleware:
         self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=workers)
 
     def __call__(self, scope):
+        assert scope["type"] == "http"
         return WSGIResponder(self.app, self.executor, scope)
 
 


Test patch:
diff --git a/tests/test_lifespan.py b/tests/test_lifespan.py
index 8efb414..642d71e 100644
--- a/tests/test_lifespan.py
+++ b/tests/test_lifespan.py
@@ -14,7 +14,7 @@ class LifespanContext:
         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 @@ def test_lifespan():
             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):