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

read_sync fails under LLDB debugging in Linux #253

Closed
joshhansen opened this issue Sep 26, 2019 · 4 comments · Fixed by crossterm-rs/crossterm-input#9
Closed

read_sync fails under LLDB debugging in Linux #253

joshhansen opened this issue Sep 26, 2019 · 4 comments · Fixed by crossterm-rs/crossterm-input#9

Comments

@joshhansen
Copy link

In addition to the current woes of read_sync (e.g. #195) it seems we have an additional problem. When debugging using LLDB in Linux, read_sync encounters an error that crashes the program. Here's the code:

extern crate crossterm;

use std::{
    io::{Write,stdout},
};

use crossterm::{
    InputEvent,
    KeyEvent,
    RawScreen,
    input,
};

fn main() {
    let _raw = RawScreen::into_raw_mode().unwrap();
    let input = input();
    for x in input.read_sync() {
        if let InputEvent::Keyboard(KeyEvent::Ctrl('c') )= x {
            return;
        }
        print!("\rRead: {:?}", x);
        stdout().flush().unwrap();
    }
}

When run directly, it reads input events and prints them out, exiting if CTRL-C is pressed.

However, when running in VSCode's CodeLLDB debugger extension, I get a "No such device or address error". And when running under lldb on the command line, I get a SIGTTIN that stops the thread, halting the program.

The program runs fine under gdb.

In practice, this is crashing the input thread in my program whenever I attempt to debug using the usual tools. It could be that this is some LLDB error, or I am misusing LLDB somehow, but the behavior is very surprising so I thought I'd report it here.

@vadimcn
Copy link

vadimcn commented Oct 3, 2019

I would hypothesize that this happens because crossterm tries to obtain TTY handle by directly opening /dev/tty. Before LLDB launches the debuggee process, it, for various reasons, detaches from the controlling terminal, so opening /dev/tty will fail. However, in most cases, debuggee's stdin will still be connected to a TTY.
Maybe crossterm could first check if stdin is a TTY?

@TimonPost
Copy link
Member

TimonPost commented Oct 15, 2019

Thanks for reporting. Input layer is going through a couple of rewrites, the first is here: https://github.com/crossterm-rs/crossterm-input/pulls, we will have to see if the issue is resolved with this PR. Otherwise, it will be fixed in a other one.

@joshhansen
Copy link
Author

@TimonPost Let me know when that refactor lands and I'll check the status of this issue

@zrzka
Copy link
Contributor

zrzka commented Oct 17, 2019

@joshhansen I tested your code with the PR (branch zrzka/unix-async-events). I can confirm that:

  • zrzka/unix-async-events fixes the issue with SIGTTIN
  • master is broken and exhibits SIGTTIN issue

All tests were made with lldb & gdb and with the CodeLLDB extension.

Test code

I just replaced Ctrl-C with Esc, because Ctrl-C is captured by lldb to interrupt the program.

examples/lldb.rs:

use std::io::{stdout, Write};

use crossterm_input::{input, InputEvent, KeyEvent, RawScreen};

fn main() {
    let _raw = RawScreen::into_raw_mode().unwrap();
    let input = input();
    for x in input.read_sync() {
        match x {
            InputEvent::Keyboard(KeyEvent::Esc) => break,
            x => print!("\rRead: {:?}\n", x),
        };

        stdout().flush().unwrap();
    }
}

Platforms

This process exited with status 0/normally is on these screenshots, because I hit the Esc key.

macOS

Screenshot 2019-10-17 at 11 28 09

Ubuntu & lldb

Screenshot 2019-10-17 at 11 39 05

Ubuntu & gdb

Screenshot 2019-10-17 at 11 40 48

Ubuntu & VSCode & CodeLLDB

Screenshot 2019-10-17 at 12 01 33

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.

4 participants