title: RuntimeError: No response returned in FastAPI when refresh request
body: <p>I got this error in my application and i didn't know why.
  After many search and debugging just figured out that it happens when i refresh my request before getting response(cancel request and send another request while processing previous request). Because of that my application need more than 2 seconds to respond, i get too many of this type of error.</p>
  <p>So far i know its from my middleware but i don't know why it happens and what should i do.</p>
  <p>Any idea how to fix this issue ?</p>
  <p>This is the error i get:</p>
  <pre><code>ERROR:    Exception in ASGI application
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py&quot;, line 81, in receive
      return self.receive_nowait()
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py&quot;, line 76, in receive_nowait
      raise WouldBlock
  anyio.WouldBlock
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py&quot;, line 35, in call_next
      message = await recv_stream.receive()
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py&quot;, line 101, in receive
      raise EndOfStream
  anyio.EndOfStream
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py&quot;, line 367, in run_asgi
      result = await app(self.scope, self.receive, self.send)
    File &quot;/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 75, in __call__
      return await self.app(scope, receive, send)
    File &quot;/usr/local/lib/python3.9/site-packages/fastapi/applications.py&quot;, line 208, in __call__
      await super().__call__(scope, receive, send)
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/applications.py&quot;, line 112, in __call__
      await self.middleware_stack(scope, receive, send)
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
      raise exc
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
      await self.app(scope, receive, _send)
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py&quot;, line 55, in __call__
      response = await self.dispatch_func(request, call_next)
    File &quot;/gateway/./app/core/middlewares.py&quot;, line 26, in dispatch
      response = await call_next(request)
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py&quot;, line 37, in call_next
      raise RuntimeError(&quot;No response returned.&quot;)
  RuntimeError: No response returned.
  </code></pre>
  <p>and this is my middleware:</p>
  <pre><code>class LoggerMiddleWare(BaseHTTPMiddleware):
  
      def __init__(self, app: ASGIApp):
          super().__init__(app)
          self.logger = logging.getLogger(self.__class__.__name__)
          self.logger.setLevel(logging.INFO)
          file_handler = logging.FileHandler('api.log')
          file_handler.setFormatter(JSONFormatter())
          self.logger.addHandler(file_handler)
          self.logger.addFilter(APIFilter())
  
      async def dispatch(self, request: Request, call_next):
          request.state.services = {}
          response = await call_next(request)
          self.logger.info(None, extra={'request': request, 'response': response})
  
          return response
  </code></pre>
  <p>I'm using <code>fastapi 0.73</code> and <code>starlette 0.17.1</code>.</p>
  <hr />
  <p>To reproduce this issue, we need to add two middlewares.</p>
  <p>A minimal reproducible example can be found here: <a href="https://github.com/encode/starlette/issues/1634#issuecomment-1124806406" rel="noreferrer">https://github.com/encode/starlette/issues/1634#issuecomment-1124806406</a></p>