Brian Oh
Brian Oh

Reputation: 10720

How can I detect an error rather than have this Rust program abort?

When running the following test program (below) using Rust 0.8 on win8, if I run two instances of the program simultaneously, when the first program is waiting for input (line 12), then the second program aborts (line 7) with fatal error message below.

To prevent the fatal error, I presume that I need to use the match statement, but I don't know enough yet about the type system and using match with it, but I have tried.

How can I get the program below to fail gracefully rather than abort, in this situation?

example test program :

001  use std::cell::Cell;
002  use std::rt::io::{Writer, Listener, Acceptor};
003  use std::rt::io::net::tcp::TcpListener;
004  use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
005
006  fn main() {
007      let res_tcp_acceptor = TcpListener::bind(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1),
008                                                          port: 9876}).listen();
009   
010      print("Connected OK. Start Listening? (y/n)  : ");
011
012      let s_input: ~str = std::io::stdin().read_line();  
013
014      if s_input == ~"y" {

fatal error displayed by second instance of program:

task <unnamed> failed at 'assertion failed: `(left == right) && (right == left)`
 (left: `0i32`, right: `-4092i32`)', C:\bot\slave\dist2-win\build\src\libstd\rt\
uv\net.rs:302

Upvotes: 2

Views: 347

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90762

That's a bug which should be reported.

The expected behaviour, as demonstrated by my Linux machine, is the raising of an IoError condition, which may be handled but which if unhandled will lead to task failure: task '<unnamed>' failed at 'Unhandled condition: io_error: rt::io::IoError{kind: OtherIoError, desc: "address already in use", detail: None}', /home/chris/vc/rust/src/libstd/condition.rs:131.

Upvotes: 1

Related Questions