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

On Linux if program exits when read_char() is blocking, the terminal will stay in raw mode #172

Open
JasonWei512 opened this issue May 23, 2023 · 3 comments

Comments

@JasonWei512
Copy link

JasonWei512 commented May 23, 2023

Problem

Run the following code on Linux with console v0.15.7:

use std::{thread, time::Duration};

fn main() {
    println!("(This program will exit in 5 seconds.)");
    println!("Press any key, and the program will echo the key you pressed.");

    thread::spawn(|| {
        let terminal = console::Term::stdout();
        loop {
            let key_pressed = terminal.read_char().unwrap();    // ← Blocking
            println!("You pressed: {}", key_pressed);
        }
    });

    thread::sleep(Duration::from_secs(5));
    panic!("After the program exits, the terminal will stay in raw mode.")
}

After the program exits, the terminal will stay in raw mode. Anything I type in shell does not display on screen.

Related issues

@knutaf
Copy link

knutaf commented Sep 16, 2023

This reproes for me using Windows Subsystem for Linux, exactly as described by OP.

knutaf added a commit to knutaf/netcrab that referenced this issue Sep 16, 2023
To work around an issue in the console crate (console-rs/console#172), use stdin-nochar mode on Unix-based systems by default. Users can still opt in to the more interactive mode if they want.
@mitsuhiko
Copy link
Collaborator

Unfortunately the only way to prevent this would be to hook ctrl+c and i'm not sure if that is something that should be done. Potentially there needs to be an API that can be called in your own ctrl+c handler.

@JasonWei512 JasonWei512 changed the title On *nix if program exits when read_char() is blocking, the terminal will stay in raw mode On Linux if program exits when read_char() is blocking, the terminal will stay in raw mode Sep 28, 2023
@fadeevab
Copy link
Contributor

@mitsuhiko The same issue with read_key, and it's reproduced even in dialoguer, there the cursor is not restored after ctrl-c, e.g. if you ctrl-c on the select component.

How to reproduce: dialoguer -> cargo run --example select -> ctrl-c -> no cursor (because term.show_cursor()? is never called).

Seems like crossterm just disables the ctrl-c in the raw mode, returning the ctrl-c key to the application.

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

No branches or pull requests

4 participants