nsf / termbox-go

Pure Go termbox implementation
http://godoc.org/github.com/nsf/termbox-go
MIT License
4.66k stars 372 forks source link

Fix nonblocking read on BSD systems #241

Closed ixtenu closed 2 years ago

ixtenu commented 2 years ago

On FreeBSD and OpenBSD, the syscall.Read call in the anonymous goroutine in Init blocks. This causes problems during Close: the send to the quit channel blocks, because the anonymous goroutine is blocked on syscall.Read, and things remain blocked until syscall.Read returns.

Init sets the in file descriptor to nonblocking with fcntl, but—on FreeBSD and OpenBSD—the in fd and the out fd are the same, and every out.Fd call clears nonblocking flag. This problem can be fixed by saving the out.Fd in a variable prior to setting the in fd to nonblocking, and using that variable afterward instead of out.Fd.

https://github.com/nsf/termbox-go/issues/167 is related.

This fixes a bug in godit on FreeBSD. Without this change, C-x C-c hangs, leaving the editor on the screen: to clear the hang, you have to type arbitrary characters so that the syscall.Read returns and then the anonymous goroutine picks up on the quit signal. With this change, C-x C-c works correctly on FreeBSD.

nsf commented 2 years ago

The patch looks innocent enough, I'll just merge it, thanks.