3

I'm looking for some clarification on task failure. As I understand it, if task 1 spawns task 2, task 2 is a child of task 1. If task 1 fails, does it automatically fail and clean up after task 2?

For example, I start a i/o task on a socket like so:

spawn(proc() {
    start_new_socket(socket, socket_receiver)
});

We'll call that task 1. In task 1, I spawn another task:

fn start_new_socket(socket: Socket, receiver: Receiver<Message>) {

    // Write task
    let mut stream_write = socket.stream.clone();
    spawn(proc() {
        loop {
            let msg = receiver.recv();
            msg.send(&mut stream_write).unwrap();
        }
    });

    // Open up a blocking read on this socket
    let mut stream_read = socket.stream.clone();
    loop {        
        let msg = Message::load(&mut stream_read).unwrap();
        match msg.payload {
            Text(ptr) => {
                let json_slice = (*ptr).as_slice();
                println!("Socket: {} recevied: {}", socket.id, json_slice);
                parse_json(json_slice, socket.clone());
            }
            Binary(ptr) => {
                // TODO - Do awesome binary shit
            }
        }
    }    
}

If the task 1, start_new_socket, fails because of EOF or something else in the stream, does the write task it started also fail?

nathansizemore
  • 3,028
  • 7
  • 39
  • 63

1 Answers1

1

I did the experiment with this code :

use std::io::Timer;
use std::time::Duration;

fn main () {

    spawn(proc() {
        let mut timer = Timer::new().unwrap();
        loop {
            println!("I from subtask !");
            timer.sleep(Duration::seconds(1));
        }
    });

    let mut other_timer = Timer::new().unwrap();
    other_timer.sleep(Duration::seconds(5));
    println!("Gonna fail....");
    other_timer.sleep(Duration::seconds(1));
    fail!("Failed !");
}

output is :

I from subtask !
I from subtask !
I from subtask !
I from subtask !
I from subtask !
Gonna fail....
I from subtask !
task '<main>' failed at 'Failed !', failing.rs:18
I from subtask !
I from subtask !
I from subtask !
I from subtask !
I from subtask !
I from subtask !
^C

So apparently not, subtask doesn't fail when main task does.

Levans
  • 14,196
  • 3
  • 49
  • 53
  • 1
    A long time ago, the tasks were indeed hierarchical, with a lot of options to control their behavior on failure. This complex system was replaced by a simpler and more approachable one, where tasks have no relationship one another. – barjak Sep 03 '14 at 18:15