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

How to cancel a Read #44

Open
AnomalRoil opened this issue Dec 8, 2021 · 2 comments
Open

How to cancel a Read #44

AnomalRoil opened this issue Dec 8, 2021 · 2 comments

Comments

@AnomalRoil
Copy link

I'm using the following example:

tty, err := tty.Open()
if err != nil {
	log.Fatal(err)
}
defer tty.Close()

for {
	r, err := tty.ReadRune()
	if err != nil {
		log.Fatal(err)
	}
	// handle key event
}

which is running in a Go routine, but sadly there are cases where I don't need to wait for user input anymore (e.g. a timer elapsed) and so would love to be able to cancel the ReadRune call, since it seems to be blocking.
If I cannot cancel the Read, I have a dangling, leaked Go routine running in the background, which is annoying on a system that is running for a long time.

I've tried doing:

tty.Input().Close()

to close the underlying os.File, hoping it would cancel the Read, but it doesn't.

I also started looking into https://pkg.go.dev/os#File.SetReadDeadline but since it seems that the os.Open("/dev/tty") is not recognized as a type on which we can set a Deadline as it errors out with "file type does not support deadline".

So here am I: how can I cancel a ReadRune in a Go routine while it is waiting for user input ?

@ZenLiuCN
Copy link

ZenLiuCN commented Feb 4, 2023

go func(ch chan rune){ 
for{
	r, err := tty.ReadRune()
	if err != nil {
		return
	}
ch <-r
}}(ch)
//handle
for c:=range ch{
//handle
}
//timeout 
tty.Close()
	

@fess932
Copy link
Contributor

fess932 commented Jul 24, 2024

same? #50

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

3 participants