forge
Explore
Log in

Don't you dare go Hollow

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 commit

dabd7316807245f6129c623d2962e55fad45da0fAhmet Kaan GümüşParent: 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
Show full message
🐛 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
crates/client/src/share/camera.rsmodified+28 -16
@@ -50,7 +50,7 @@ pub struct Camera {
5050 alive: Arc<AtomicU32>,
5151 opened: Instant,
5252 stop: Arc<AtomicBool>,
53- thread: Option<(JoinHandle<()>, Receiver<()>, CameraIndex)>,
53+ thread: Option<(JoinHandle<()>, Receiver<()>)>,
5454 _fake: Option<fake::Generator>,
5555 }
5656
@@ -64,20 +64,30 @@ fn held() -> std::sync::MutexGuard<'static, Vec<CameraIndex>> {
6464 impl Drop for Camera {
6565 fn drop(&mut self) {
6666 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+ {
7170 let _ = thread.join();
72- return;
7371 }
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);
8191 }
8292 }
8393
@@ -137,7 +147,7 @@ fn index(device: Option<&str>) -> Option<CameraIndex> {
137147 })
138148 .unwrap_or(CameraIndex::Index(0));
139149 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");
141151 return None;
142152 }
143153 let count = format_count(&index);
@@ -228,6 +238,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
228238 return Err(w().camera.needs_media_pack.to_string());
229239 }
230240 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())?;
231242 let (negotiated_tx, negotiated_rx) = sync_channel::<Result<Negotiated, String>>(1);
232243 let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE);
233244 let stop = Arc::new(AtomicBool::new(false));
@@ -236,11 +247,12 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
236247 let opened = Instant::now();
237248 let (done_tx, done) = sync_channel::<()>(0);
238249 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());
240251 std::thread::Builder::new()
241252 .name("camera-capture".to_string())
242253 .spawn(move || {
243254 let _done = done_tx;
255+ let _hold = hold;
244256 let clock = Clock {
245257 opened,
246258 alive: &alive,
@@ -274,7 +286,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
274286 alive,
275287 opened,
276288 stop,
277- thread: Some((thread, done, index)),
289+ thread: Some((thread, done)),
278290 _fake: None,
279291 })
280292 }