Fix deadlocks by making CurrentThreadExecutor
reentrant and reusing it
#493
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When there are multiple
CurrentThreadExecutor
s on the same stack, it seems there’s no good way to decide in advance which one new jobs should be routed to. If we send a job to an executor too low in the stack, something in a higher executor’s queue might deadlock waiting for it to finish (#348?). If we send a job to an executor too high in the stack, that job might deadlock waiting for something in a lower executor’s queue to finish (#492), or the executor might have already terminated and been marked broken (#491).So instead, make a single
CurrentThreadExecutor
for the loop, and reuse it on the same stack as many times as necessary, thereby allowing any pending job to run no matter where in the stack we currently are.It is still possible for a job higher in the stack to deadlock waiting for a job that’s currently executing lower in the stack, but in that case, there’s truly no solution as far as I can see (the user needs to migrate more of their sync code to async, I guess).
async_to_sync
→sync_to_async
→async_to_sync
→create_task
→sync_to_async
#492