Skip to content

Re-assigning a closed TcpStream and reading causes a segmentation fault #12668

@rapha

Description

@rapha
$ cat segv.rs    
use std::io::{Acceptor, Listener, net};

fn main() {
  let address = net::ip::SocketAddr { ip: net::ip::Ipv4Addr(127, 0, 0, 1), port: 1234 };
  std::task::spawn(proc() {
    let mut socket = std::io::TcpListener::bind(address).listen().accept();
    socket.write([0]).unwrap();
    println!("server: done");
  });
  let socket = std::io::TcpStream::connect(address);
  std::task::spawn(proc() {
    loop {
      let mut socket = socket; // Here is the problem! Fixed if assignment happens outside loop.
      println!("client: about to read");
      socket.read_byte().unwrap();
      println!("client: read successfully");
    }
  });
}
$ rustc segv.rs && ./segv
client: about to read
server: done
client: read successfully
client: about to read
Segmentation fault: 11
$

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions