Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
MIO EventLoop is not running for TcpStream

I am a Rust beginner struggling with a problem of async IO. I've decided to use mio.

I've read some source code + tutorials but there is still some fundamental part that I do not understand. I am setting up a server with netcat -k -l 127.0.0.1 9999. Then I am running simple test with cargo (code below). I was expecting to see panic with "ready" or "tick". But it never happens and test is running forever.

extern crate mio;
use mio::*;
#[allow(unused_imports)]
use mio::tcp::TcpStream;

#[allow(dead_code)]
struct MyHandler;

impl Handler for MyHandler {
    type Timeout = ();
    type Message = ();

    fn ready(&mut self, _event_loop: &mut EventLoop<Self>, _token: Token, _event_set: EventSet) {
        panic!("ready");
    }

    fn tick(&mut self, _event_loop: &mut EventLoop<Self>) {
        panic!("tick");
    }
}

#[test]
fn mio_test1() {
    let addr = "127.0.0.1:9999".parse().unwrap();
    let mut event_loop = EventLoop::<MyHandler>::new().unwrap();
    event_loop.register(&TcpStream::connect(&addr).unwrap(), Token(0), EventSet::readable(), PollOpt::level()).unwrap();
    event_loop.run(&mut MyHandler).unwrap();
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your problem arises from the fact that the socket you open is closed before your event loop has the chance to run.

You current code is roughly equivalent to this:

let addr = "127.0.0.1:9999".parse().unwrap();
let mut event_loop = EventLoop::<MyHandler>::new().unwrap();
{
    let sock = TcpStream::connect(&addr).unwrap();
    event_loop.register(&sock, Token(0), EventSet::readable(), PollOpt::level()).unwrap();
}   // The socket is closed here, before the event loop runs
event_loop.run(&mut MyHandler).unwrap();

So the fix is just to bind the socket to a variable so it will stay open when you call the event loop.

let addr = "127.0.0.1:9999".parse().unwrap();
let mut event_loop = EventLoop::<MyHandler>::new().unwrap();
let sock = TcpStream::connect(&addr).unwrap();
event_loop.register(&sock, Token(0), EventSet::readable(), PollOpt::level()).unwrap();
event_loop.run(&mut MyHandler).unwrap();

Your code then behaves as you expect, and panics as soon as there is something to read on the socket.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!