From 77ee96306f7d42e0d70e45b017a8b95d9d1ba056 Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Fri, 1 May 2026 22:30:29 -0700 Subject: [PATCH] webcam: prevent double Close releasing wrong fd Closes #56. --- webcam.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/webcam.go b/webcam.go index bf72815..6a53d29 100644 --- a/webcam.go +++ b/webcam.go @@ -18,6 +18,7 @@ type Webcam struct { bufcount uint32 buffers [][]byte streaming bool + closed bool pollFds []unix.PollFd } @@ -333,13 +334,19 @@ func (w *Webcam) StopStreaming() error { return stopStreaming(w.fd) } -// Close the device +// Close the device. Calling Close more than once returns nil; the +// underlying file descriptor is only released on the first call so +// the kernel can't hand it to unrelated code in the meantime. func (w *Webcam) Close() error { + if w.closed { + return nil + } if w.streaming { w.StopStreaming() } err := unix.Close(int(w.fd)) + w.closed = true return err }