Skip to content

std: Don't fail the task when a Future is dropped #14941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/libstd/sync/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl<A:Send> Future<A> {
let (tx, rx) = channel();

spawn(proc() {
tx.send(blk());
// Don't fail if the other end has hung up
let _ = tx.send_opt(blk());
});

Future::from_receiver(rx)
Expand All @@ -144,6 +145,7 @@ mod test {
use prelude::*;
use sync::Future;
use task;
use comm::{channel, Sender};

#[test]
fn test_from_value() {
Expand Down Expand Up @@ -206,4 +208,28 @@ mod test {
assert_eq!(actual, expected);
});
}

#[test]
fn test_dropped_future_doesnt_fail() {
struct Bomb(Sender<bool>);

local_data_key!(LOCAL: Bomb)

impl Drop for Bomb {
fn drop(&mut self) {
let Bomb(ref tx) = *self;
tx.send(task::failing());
}
}

// Spawn a future, but drop it immediately. When we receive the result
// later on, we should never view the task as having failed.
let (tx, rx) = channel();
drop(Future::spawn(proc() {
LOCAL.replace(Some(Bomb(tx)));
}));

// Make sure the future didn't fail the task.
assert!(!rx.recv());
}
}