libv4l-rs icon indicating copy to clipboard operation
libv4l-rs copied to clipboard

device: Retry `poll()` on interrupts (`EINTR`)

Open vilhelmbergsoe opened this issue 1 year ago • 3 comments

Attempt at handling EINTR in the case of a system call interrupt when polling for events.

Fixed an issue with a little program I wrote asciicam, which would panic with the error message "Error: Interrupted system call (os error 4)" when calling stream.next() after and upgrade from 0.13.0 -> 0.14.0.

I don't have a deep understanding of interruptible syscalls, but apparently the event polling from libv4l-rs is also connected to keyboard input in the terminal somehow?

While looking this up I came across a similar issue with crossterm here. They seem to have fixed it by continuing in the loop over polls, the PR is here.

This solution isn't as elegant as I just do a recursive call to poll and hope there isn't an interrupt next time, but it seems to work in my program.

vilhelmbergsoe avatar Dec 05 '23 19:12 vilhelmbergsoe

I think that makes sense, but a loop seems more clean than recursion IMO.

MarijnS95 avatar Dec 05 '23 20:12 MarijnS95

I think that makes sense, but a loop seems more clean than recursion IMO.

Something like this, seem good to you?

vilhelmbergsoe avatar Dec 05 '23 20:12 vilhelmbergsoe

IMO the best way to handle this is at the call site (i.e. your application). The EINTR error will be propagated all the way up by dequeue(), e.g. here for mmap: https://github.com/raymanfx/libv4l-rs/blob/6a6017bf9c94f6bf576cdf84605c2adfa0ce7b05/src/io/mmap/stream.rs#L160. If we do it anywhere in the library, it takes away the option for all users to interrupt the poll() call.

Perhaps I'm missing another aspect of this issue though; feel free to chime in in that case.

raymanfx avatar Feb 18 '24 14:02 raymanfx