uvicorn: RuntimeError in uvicorn for some requests

uvicorn==0.2.5 is throwing errors for some requests.

ERROR: Exception in ASGI application
Traceback (most recent call last):
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 196, in run_asgi
    result = await asgi(self.receive, self.send)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 190, in __call__
    await self.handle(body)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 110, in __call__
    return await asyncio.wait_for(future, timeout=None)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
    return (yield from fut)
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 125, in thread_handler
    return self.func(*args, **kwargs)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 229, in handle
    self.send(response_message)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
    return call_result.result()
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 308, in send
    protocol.transport.write(body)
  File "uvloop/handles/stream.pyx", line 636, in uvloop.loop.UVStream.write
  File "uvloop/handles/handle.pyx", line 165, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x1a48ad8>; the handler is closed

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 35 (15 by maintainers)

Commits related to this issue

Most upvoted comments

For the commenters above - if the following applies to you:

  • You are using FastAPI<0.87.0
  • You have path operations defined using def instead of async def
  • You have defined custom middleware

…then you may be susceptible to https://github.com/encode/starlette/issues/919, which can cause these errors in uvicorn:

  • LocalProtocolError: Can't send data when our state is ERROR
  • LocalProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE

This issue was resolved in starlette==0.21.0, which was added in FastAPI==0.87.0. Bumping to FastAPI>=0.87.0 should fix your application.

Apologies for responding to an old issue, this page is the top search result for the above errors.

@Kludex Happy to investigate this issue if it still needs attention!

FYI I am getting this error with uvicorn 0.11.5 and h11 0.9.0, on a Django channels project. I’m running uvicorn with just the basic host/port options. The error happens only sometimes and has not had any impact on my app’s functionality.

ERROR:    Exception in callback H11Protocol.timeout_keep_alive_handler()
handle: <TimerHandle when=112443.109 H11Protocol.timeout_keep_alive_handler()>
Traceback (most recent call last):
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 249, in _fire_event_triggered_transitions
    new_state = EVENT_TRIGGERED_TRANSITIONS[role][state][event_type]
KeyError: <class 'h11._events.ConnectionClosed'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "c:\otree\ve38\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 339, in timeout_keep_alive_handler
    self.conn.send(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 469, in send
    data_list = self.send_with_data_passthrough(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 494, in send_with_data_passthrough
    self._process_event(self.our_role, event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 242, in _process_event
    self._cstate.process_event(role, type(event), server_switch_event)
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 238, in process_event
    self._fire_event_triggered_transitions(role, event_type)
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 251, in _fire_event_triggered_transitions
    raise LocalProtocolError(
h11._util.LocalProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE
ERROR:    Exception in callback H11Protocol.timeout_keep_alive_handler()
handle: <TimerHandle when=112443.109 H11Protocol.timeout_keep_alive_handler()>
Traceback (most recent call last):
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 249, in _fire_event_triggered_transitions
    new_state = EVENT_TRIGGERED_TRANSITIONS[role][state][event_type]
KeyError: <class 'h11._events.ConnectionClosed'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "c:\otree\ve38\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 339, in timeout_keep_alive_handler
    self.conn.send(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 469, in send
    data_list = self.send_with_data_passthrough(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 494, in send_with_data_passthrough
    self._process_event(self.our_role, event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 242, in _process_event
    self._cstate.process_event(role, type(event), server_switch_event)
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 238, in process_event
    self._fire_event_triggered_transitions(role, event_type)
  File "c:\otree\ve38\lib\site-packages\h11\_state.py", line 251, in _fire_event_triggered_transitions
    raise LocalProtocolError(
h11._util.LocalProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE
INFO:     127.0.0.1:52787 - "GET /p/oypfo5xq/misc_3p/PickWinner/2/ HTTP/1.1" 302 Found
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "c:\otree\ve38\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 384, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "c:\otree\ve38\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "c:\otree\ve38\lib\site-packages\uvicorn\middleware\asgi2.py", line 7, in __call__
    await instance(receive, send)
  File "c:\otree\ve38\lib\site-packages\channels\http.py", line 192, in __call__
    await self.handle(body_stream)
  File "c:\otree\ve38\lib\site-packages\asgiref\sync.py", line 269, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 455, in wait_for
    return await fut
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "c:\otree\ve38\lib\site-packages\asgiref\sync.py", line 314, in thread_handler
    return func(*args, **kwargs)
  File "c:\otree\ve38\lib\site-packages\channels\http.py", line 250, in handle
    self.send(response_message)
  File "c:\otree\ve38\lib\site-packages\asgiref\sync.py", line 120, in __call__
    return call_result.result()
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 432, in result
    return self.__get_result()
  File "C:\Users\wicke\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 388, in __get_result
    raise self._exception
  File "c:\otree\ve38\lib\site-packages\asgiref\sync.py", line 180, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "c:\otree\ve38\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 461, in send
    output = self.conn.send(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 469, in send
    data_list = self.send_with_data_passthrough(event)
  File "c:\otree\ve38\lib\site-packages\h11\_connection.py", line 484, in send_with_data_passthrough
    raise LocalProtocolError("Can't send data when our state is ERROR")
h11._util.LocalProtocolError: Can't send data when our state is ERROR

Happy to provide extra info.

@r1b I am still getting this error when trying to test def endpoint with ~50 users concurrency. Do you have any idea why? FastAPI==0.92.0

Update: After removing middleware I don’t see this issue. Does anyone has ideas why it breaks the server?

@euri10 the error is gone in the latest master, thank you! 👍

(I am still getting this issue: https://github.com/encode/uvicorn/issues/757 but will continue the discussion over there.)

This issue was resolved years ago, and there’s too much information here.

If interested, please open a discussion with a minimal reproducible example.

Fixed by #832 it seems !😎

Released as 0.2.6