Oneshot channel deadlock on async runtime #21916
Draft
+2,963
−1,013
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.
Description
Using tokio's oneshot channel on our async runtime can lead to an intermittent deadlock. Tested on macOS 14.6.1 / Apple M3 Pro.
As an alternative, we can currently use the
connector
instead.Steps to reproduce
make build-release
dbg!
in this branch:crates/polars-stream/src/nodes/io_sources/multi_file_reader/reader_pipelines/generic.rs:505:17
crates/polars-stream/src/nodes/io_sources/parquet_reader/mod.rs:166:13
async_executor::spawn
crates/polars-stream/src/nodes/io_sources/multi_file_reader/reader_pipelines/generic.rs:509:17
The script runs an infinite loop, however the output should eventually stop and look something like this:
It should show that for the latest scan source (3 in the above case), only 2 lines of
dbg!
are printed - the one from after a value is sent, and the one from before the receiver is awaited. But somehow theawait
on the receiver is not completing even though a value is sent.This branch also uses an updated
TracedAwait
, if we setPOLARS_AWAIT_TIMEOUT_REPOLL=1
, the output from the script should resume after printingrepolling in 2 seconds
. It is as though we have missed a task wakeup notification.Notes
make build-release
)POLARS_MAX_THREADS=1
Waker oddness
Tokio's oneshot channel has a mechanism by which the receiver avoids cloning the context waker if it determines that it already has a waker from a previous poll that wakes the same task using
will_wake
. If we patch this to instead always replace the waker, the deadlock will not occur - tokio-rs/tokio@master...nameexhaustion:tokio:tokio-1.43-will-wake.