Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulling USB out is not detected and eats 100% CPU #14

Closed
krant opened this issue Apr 29, 2024 · 3 comments · Fixed by #19
Closed

Pulling USB out is not detected and eats 100% CPU #14

krant opened this issue Apr 29, 2024 · 3 comments · Fixed by #19

Comments

@krant
Copy link

krant commented Apr 29, 2024

Steps to reproduce

  • Run basic_serial example and enter port name of plugged-in device
  • Wait for console to calm down and pull the USB out
  • Observe example loop is still running, console is clean, 1 CPU core is 100% utilized

Possible reason

This line is getting executed endlessly:

Ok(0) => continue,
If I return an error here the example starts to work, but I'm not sure it will not break something else.

@canardleteer
Copy link
Contributor

canardleteer commented May 13, 2024

Confirming I can reproduce on my end in Linux.

One interesting thing I noticed, is that this happens even with the timeout set here:

rust/src/utils_internal.rs

Lines 115 to 117 in e77d168

let builder = tokio_serial::new(port_name.clone(), baud_rate.unwrap_or(DEFAULT_SERIAL_BAUD))
.flow_control(tokio_serial::FlowControl::Software)
.timeout(Duration::from_millis(10));

I'm not sure it will not break something else.

@krant - Me either. start_read_handler is sufficiently generic enough to make it an unsafe bet.

I didn't have time to look much deeper into the chain of tokio-serial -> mio-serial -> serialport.

I have however, confirmed that changing the line to:

   Ok(0) => { tokio::time::sleep(Duration::from_millis(5)).await; continue},

Will reduce the CPU usage down from 100%, which might be enough to make it tolerable in the meantime, until someone gets a chance to dig deeper.

@krant
Copy link
Author

krant commented May 16, 2024

tokio-serial example handles the pulling correctly:

cargo run --features="rt codec" --example serial_println -- /dev/ttyACM0
[LOG DATA]
## Pull out USB
[PROGRAM EXITS]

@powturns
Copy link
Contributor

I seem to be able to reproduce this using the tcp_stream as well if the socket is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants