Skip to content

Change ThreadLock to ThreadRLock to resolve rare deadlock #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
from .._models import Origin, Proxy, Request, Response
from .._synchronization import AsyncEvent, AsyncShieldCancellation, AsyncThreadLock
from .._synchronization import AsyncEvent, AsyncShieldCancellation, AsyncThreadRLock
from .connection import AsyncHTTPConnection
from .interfaces import AsyncConnectionInterface, AsyncRequestInterface

Expand Down Expand Up @@ -123,7 +123,7 @@ def __init__(
# We only mutate the state of the connection pool within an 'optional_thread_lock'
# context. This holds a threading lock unless we're running in async mode,
# in which case it is a no-op.
self._optional_thread_lock = AsyncThreadLock()
self._optional_thread_lock = AsyncThreadRLock()

def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
if self._proxy is not None:
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .._backends.base import SOCKET_OPTION, NetworkBackend
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
from .._models import Origin, Proxy, Request, Response
from .._synchronization import Event, ShieldCancellation, ThreadLock
from .._synchronization import Event, ShieldCancellation, ThreadRLock
from .connection import HTTPConnection
from .interfaces import ConnectionInterface, RequestInterface

Expand Down Expand Up @@ -123,7 +123,7 @@ def __init__(
# We only mutate the state of the connection pool within an 'optional_thread_lock'
# context. This holds a threading lock unless we're running in async mode,
# in which case it is a no-op.
self._optional_thread_lock = ThreadLock()
self._optional_thread_lock = ThreadRLock()

def create_connection(self, origin: Origin) -> ConnectionInterface:
if self._proxy is not None:
Expand Down
20 changes: 11 additions & 9 deletions httpcore/_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ async def __aexit__(
self._anyio_lock.release()


class AsyncThreadLock:
class AsyncThreadRLock:
"""
This is a threading-only lock for no-I/O contexts.
The lock type is a reentrant lock.

In the sync case `ThreadLock` provides thread locking.
In the async case `AsyncThreadLock` is a no-op.
In the sync case `ThreadRLock` provides thread locking.
In the async case `AsyncThreadRLock` is a no-op.
"""

def __enter__(self) -> AsyncThreadLock:
def __enter__(self) -> AsyncThreadRLock:
return self

def __exit__(
Expand Down Expand Up @@ -253,18 +254,19 @@ def __exit__(
self._lock.release()


class ThreadLock:
class ThreadRLock:
"""
This is a threading-only lock for no-I/O contexts.
The lock type is a reentrant lock.

In the sync case `ThreadLock` provides thread locking.
In the async case `AsyncThreadLock` is a no-op.
In the sync case `ThreadRLock` provides thread locking.
In the async case `AsyncThreadRLock` is a no-op.
"""

def __init__(self) -> None:
self._lock = threading.Lock()
self._lock = threading.RLock()

def __enter__(self) -> ThreadLock:
def __enter__(self) -> ThreadRLock:
self._lock.acquire()
return self

Expand Down