Memory leak reopening a camera whose source left (nokhwa reuses the stuck thread's v4l fd, which still owns the loopback capture token, so the driver reports continuous ranges that nokhwa expands into billions of formats)
Browse files at this commitdabd7316807245f6129c623d2962e55fad45da0fParent: 98764b6- fix
- memory leak reopening a camera whose source left (nokhwa reuses the stuck thread's v4l fd, which still owns the loopback capture token, so the driver reports continuous ranges that nokhwa expands into billions of formats)
- an index stays held from open until its capture thread returns
1 changed file +28 -16
crates/client/src/share/camera.rsmodified+28 -16
| @@ -50,7 +50,7 @@ pub struct Camera { | ||
| 50 | 50 | alive: Arc<AtomicU32>, |
| 51 | 51 | opened: Instant, |
| 52 | 52 | stop: Arc<AtomicBool>, |
| 53 | - thread: Option<(JoinHandle<()>, Receiver<()>, CameraIndex)>, | |
| 53 | + thread: Option<(JoinHandle<()>, Receiver<()>)>, | |
| 54 | 54 | _fake: Option<fake::Generator>, |
| 55 | 55 | } |
| 56 | 56 | |
| @@ -64,20 +64,30 @@ fn held() -> std::sync::MutexGuard<'static, Vec<CameraIndex>> { | ||
| 64 | 64 | impl Drop for Camera { |
| 65 | 65 | fn drop(&mut self) { |
| 66 | 66 | self.stop.store(true, Ordering::Relaxed); |
| 67 | - let Some((thread, done, index)) = self.thread.take() else { | |
| 68 | - return; | |
| 69 | - }; | |
| 70 | - if done.recv_timeout(JOIN_TIMEOUT) != Err(RecvTimeoutError::Timeout) { | |
| 67 | + if let Some((thread, done)) = self.thread.take() | |
| 68 | + && done.recv_timeout(JOIN_TIMEOUT) != Err(RecvTimeoutError::Timeout) | |
| 69 | + { | |
| 71 | 70 | let _ = thread.join(); |
| 72 | - return; | |
| 73 | 71 | } |
| 74 | - tracing::warn!(?index, "camera thread stuck, device held until it returns"); | |
| 75 | - held().push(index.clone()); | |
| 76 | - std::thread::spawn(move || { | |
| 77 | - let _ = done.recv(); | |
| 78 | - let _ = thread.join(); | |
| 79 | - held().retain(|busy| *busy != index); | |
| 80 | - }); | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +struct Hold(CameraIndex); | |
| 76 | + | |
| 77 | +impl Hold { | |
| 78 | + fn take(index: CameraIndex) -> Option<Self> { | |
| 79 | + let mut held = held(); | |
| 80 | + if held.contains(&index) { | |
| 81 | + return None; | |
| 82 | + } | |
| 83 | + held.push(index.clone()); | |
| 84 | + Some(Self(index)) | |
| 85 | + } | |
| 86 | +} | |
| 87 | + | |
| 88 | +impl Drop for Hold { | |
| 89 | + fn drop(&mut self) { | |
| 90 | + held().retain(|busy| *busy != self.0); | |
| 81 | 91 | } |
| 82 | 92 | } |
| 83 | 93 | |
| @@ -137,7 +147,7 @@ fn index(device: Option<&str>) -> Option<CameraIndex> { | ||
| 137 | 147 | }) |
| 138 | 148 | .unwrap_or(CameraIndex::Index(0)); |
| 139 | 149 | if held().contains(&index) { |
| 140 | - tracing::warn!(?index, "camera still held by a stuck thread"); | |
| 150 | + tracing::warn!(?index, "camera still held by our capture thread"); | |
| 141 | 151 | return None; |
| 142 | 152 | } |
| 143 | 153 | let count = format_count(&index); |
| @@ -228,6 +238,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> { | ||
| 228 | 238 | return Err(w().camera.needs_media_pack.to_string()); |
| 229 | 239 | } |
| 230 | 240 | let index = index(device.as_deref()).ok_or_else(|| w().camera.no_response.to_string())?; |
| 241 | + let hold = Hold::take(index.clone()).ok_or_else(|| w().camera.no_response.to_string())?; | |
| 231 | 242 | let (negotiated_tx, negotiated_rx) = sync_channel::<Result<Negotiated, String>>(1); |
| 232 | 243 | let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE); |
| 233 | 244 | let stop = Arc::new(AtomicBool::new(false)); |
| @@ -236,11 +247,12 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> { | ||
| 236 | 247 | let opened = Instant::now(); |
| 237 | 248 | let (done_tx, done) = sync_channel::<()>(0); |
| 238 | 249 | let thread = { |
| 239 | - let (stop, fps, alive, index) = (stop.clone(), fps.clone(), alive.clone(), index.clone()); | |
| 250 | + let (stop, fps, alive) = (stop.clone(), fps.clone(), alive.clone()); | |
| 240 | 251 | std::thread::Builder::new() |
| 241 | 252 | .name("camera-capture".to_string()) |
| 242 | 253 | .spawn(move || { |
| 243 | 254 | let _done = done_tx; |
| 255 | + let _hold = hold; | |
| 244 | 256 | let clock = Clock { |
| 245 | 257 | opened, |
| 246 | 258 | alive: &alive, |
| @@ -274,7 +286,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> { | ||
| 274 | 286 | alive, |
| 275 | 287 | opened, |
| 276 | 288 | stop, |
| 277 | - thread: Some((thread, done, index)), | |
| 289 | + thread: Some((thread, done)), | |
| 278 | 290 | _fake: None, |
| 279 | 291 | }) |
| 280 | 292 | } |