Skip to content

Commit

Permalink
Raise an error on thread-sensitive deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
shughes-uk authored May 31, 2021
1 parent cd15242 commit 13d0b82
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ class SyncToAsync:
else:
thread_sensitive_context: None = None

# Contextvar that is used to detect if the single thread executor
# would be awaited on while already being used in the same context
if sys.version_info >= (3, 7):
deadlock_context: "contextvars.ContextVar[bool]" = contextvars.ContextVar(
"deadlock_context"
)
else:
deadlock_context: None = None

# Maintaining a weak reference to the context ensures that thread pools are
# erased once the context goes out of scope. This terminates the thread pool.
context_to_thread_executor: "weakref.WeakKeyDictionary[object, ThreadPoolExecutor]" = (
Expand Down Expand Up @@ -396,9 +405,15 @@ async def __call__(self, *args, **kwargs):
# Create new thread executor in current context
executor = ThreadPoolExecutor(max_workers=1)
self.context_to_thread_executor[thread_sensitive_context] = executor
elif self.deadlock_context and self.deadlock_context.get(False):
raise RuntimeError(
"Single thread executor already being used, would deadlock"
)
else:
# Otherwise, we run it in a fixed single thread
executor = self.single_thread_executor
if self.deadlock_context:
self.deadlock_context.set(True)
else:
# Use the passed in executor, or the loop's default if it is None
executor = self._executor
Expand Down Expand Up @@ -429,6 +444,8 @@ async def __call__(self, *args, **kwargs):

if contextvars is not None:
_restore_context(context)
if self.deadlock_context:
self.deadlock_context.set(False)

return ret

Expand Down
25 changes: 25 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import functools
import multiprocessing
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -669,3 +670,27 @@ def sync_func():
thread_sensitive=True,
executor=custom_executor,
)


@pytest.mark.skipif(sys.version_info < (3, 7), reason="Issue persists with 3.6")
def test_sync_to_async_deadlock_raises():
def db_write():
pass

async def io_task():
await sync_to_async(db_write)()

async def do_io_tasks():
t = asyncio.create_task(io_task())
await t
# await asyncio.gather(io_task()) # Also deadlocks
# await io_task() # Works

def view():
async_to_sync(do_io_tasks)()

async def server_entry():
await sync_to_async(view)()

with pytest.raises(RuntimeError):
asyncio.run(server_entry())

0 comments on commit 13d0b82

Please sign in to comment.