Skip to content

Commit 6b53175

Browse files
committed
Auto merge of #113861 - ibraheemdev:mpsc-tls-bug, r=Mark-Simulacrum
Avoid tls access while iterating through mpsc thread entries Upstream fix: crossbeam-rs/crossbeam#802. Possibly fixes #113726.
2 parents b14fd23 + fb31a1a commit 6b53175

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

Diff for: library/std/src/sync/mpmc/waker.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,32 @@ impl Waker {
6666
/// Attempts to find another thread's entry, select the operation, and wake it up.
6767
#[inline]
6868
pub(crate) fn try_select(&mut self) -> Option<Entry> {
69-
self.selectors
70-
.iter()
71-
.position(|selector| {
72-
// Does the entry belong to a different thread?
73-
selector.cx.thread_id() != current_thread_id()
74-
&& selector // Try selecting this operation.
75-
.cx
76-
.try_select(Selected::Operation(selector.oper))
77-
.is_ok()
78-
&& {
79-
// Provide the packet.
80-
selector.cx.store_packet(selector.packet);
81-
// Wake the thread up.
82-
selector.cx.unpark();
83-
true
84-
}
85-
})
86-
// Remove the entry from the queue to keep it clean and improve
87-
// performance.
88-
.map(|pos| self.selectors.remove(pos))
69+
if self.selectors.is_empty() {
70+
None
71+
} else {
72+
let thread_id = current_thread_id();
73+
74+
self.selectors
75+
.iter()
76+
.position(|selector| {
77+
// Does the entry belong to a different thread?
78+
selector.cx.thread_id() != thread_id
79+
&& selector // Try selecting this operation.
80+
.cx
81+
.try_select(Selected::Operation(selector.oper))
82+
.is_ok()
83+
&& {
84+
// Provide the packet.
85+
selector.cx.store_packet(selector.packet);
86+
// Wake the thread up.
87+
selector.cx.unpark();
88+
true
89+
}
90+
})
91+
// Remove the entry from the queue to keep it clean and improve
92+
// performance.
93+
.map(|pos| self.selectors.remove(pos))
94+
}
8995
}
9096

9197
/// Notifies all operations waiting to be ready.

0 commit comments

Comments
 (0)