Change ThreadLock to ThreadRLock to resolve rare deadlock by Zenulous · Pull Request #1003 · encode/httpcore

I wouldn't make this change without a clear understanding of why a re-entrant lock would be required here.

We are seeing this issue sporadically as well. Specifically, it seems to occur when an exception is thrown in the middle of http response streaming, and an immediate retry after that. We haven't been able to reproduce it reliably based on these factors alone, unfortunately, but I managed to grab a few thread dumps, and it clearly shows an attempt to acquire a lock from within _assign_requests_to_connections which already holds that lock.

  File "/opt/venv/lib/python3.12/site-packages/openai/resources/chat/completions/completions.py", line 182, in parse
    return self._post(
  File "/opt/venv/lib/python3.12/site-packages/openai/_base_client.py", line 1259, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/opt/venv/lib/python3.12/site-packages/openai/_base_client.py", line 982, in request
    response = self._client.send(
  File "/opt/venv/lib/python3.12/site-packages/httpx/_client.py", line 914, in send
    response = self._send_handling_auth(
  File "/opt/venv/lib/python3.12/site-packages/httpx/_client.py", line 942, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/opt/venv/lib/python3.12/site-packages/httpx/_client.py", line 979, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/opt/venv/lib/python3.12/site-packages/httpx/_client.py", line 1014, in _send_single_request
    response = transport.handle_request(request)
  File "/opt/venv/lib/python3.12/site-packages/httpx/_transports/default.py", line 250, in handle_request
    resp = self._pool.handle_request(req)
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 228, in handle_request
    closing = self._assign_requests_to_connections()
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 294, in _assign_requests_to_connections
    and len([connection.is_idle() for connection in self._connections])
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py", line 192, in is_idle
    def is_idle(self) -> bool:
  File "/opt/venv/lib/python3.12/site-packages/httpx/_models.py", line 900, in iter_bytes
    yield chunk
  File "/opt/venv/lib/python3.12/site-packages/httpx/_models.py", line 954, in iter_raw
    yield chunk
  File "/opt/venv/lib/python3.12/site-packages/httpx/_client.py", line 154, in __iter__
    yield chunk
  File "/opt/venv/lib/python3.12/site-packages/httpx/_transports/default.py", line 128, in __iter__
    yield part
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 406, in __iter__
    self.close()
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 416, in close
    with self._pool._optional_thread_lock:
  File "/opt/venv/lib/python3.12/site-packages/httpcore/_synchronization.py", line 268, in __enter__
    self._lock.acquire()