Skip to content

Commit 9c05c1c

Browse files
committed
Fix a deadlock in channels, again.
This deadlock was caused when the channel was closed at just the right time, so the extra `self.cnt.fetch_add` actually should have preserved the DISCONNECTED state of the channel. by modifying this the channel entered a state such that the port would never succeed in dropping. This also moves the increment of self.steals until after the MAX_STEALS block. The reason for this is that in 'fn recv()' the steals variable is decremented immediately after the try_recv(), which could in theory set steals to -1 if it was previously set to 0 in try_recv(). Closes #12340
1 parent 47c3183 commit 9c05c1c

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Diff for: src/libstd/comm/shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ impl<T: Send> Packet<T> {
305305
// See the discussion in the stream implementation for why we we
306306
// might decrement steals.
307307
Some(data) => {
308-
self.steals += 1;
309308
if self.steals > MAX_STEALS {
310309
match self.cnt.swap(0, atomics::SeqCst) {
311310
DISCONNECTED => {
@@ -314,11 +313,12 @@ impl<T: Send> Packet<T> {
314313
n => {
315314
let m = cmp::min(n, self.steals);
316315
self.steals -= m;
317-
self.cnt.fetch_add(n - m, atomics::SeqCst);
316+
self.bump(n - m);
318317
}
319318
}
320319
assert!(self.steals >= 0);
321320
}
321+
self.steals += 1;
322322
Ok(data)
323323
}
324324

Diff for: src/libstd/comm/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ impl<T: Send> Packet<T> {
213213
// down as much as possible (without going negative), and then
214214
// adding back in whatever we couldn't factor into steals.
215215
Some(data) => {
216-
self.steals += 1;
217216
if self.steals > MAX_STEALS {
218217
match self.cnt.swap(0, atomics::SeqCst) {
219218
DISCONNECTED => {
@@ -222,11 +221,12 @@ impl<T: Send> Packet<T> {
222221
n => {
223222
let m = cmp::min(n, self.steals);
224223
self.steals -= m;
225-
self.cnt.fetch_add(n - m, atomics::SeqCst);
224+
self.bump(n - m);
226225
}
227226
}
228227
assert!(self.steals >= 0);
229228
}
229+
self.steals += 1;
230230
match data {
231231
Data(t) => Ok(t),
232232
GoUp(up) => Err(Upgraded(up)),

0 commit comments

Comments
 (0)