Failed tasks in Rust

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

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

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

      

We'll call this task 1. In task 1, I create 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 task 1 start_new_socket

,, fails due to EOF

or something else on the thread, does the recording task that it started?

+3


source to share


1 answer


I experimented 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 !");
}

      

:



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

      

Apparently not, the sub-task does not fail if the main task is.

+1


source







All Articles