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 {
50 alive: Arc<AtomicU32>,50 alive: Arc<AtomicU32>,
51 opened: Instant,51 opened: Instant,
52 stop: Arc<AtomicBool>,52 stop: Arc<AtomicBool>,
53- thread: Option<(JoinHandle<()>, Receiver<()>, CameraIndex)>,53+ thread: Option<(JoinHandle<()>, Receiver<()>)>,
54 _fake: Option<fake::Generator>,54 _fake: Option<fake::Generator>,
55 }55 }
56 56
@@ -64,20 +64,30 @@ fn held() -> std::sync::MutexGuard<'static, Vec<CameraIndex>> {
64 impl Drop for Camera {64 impl Drop for Camera {
65 fn drop(&mut self) {65 fn drop(&mut self) {
66 self.stop.store(true, Ordering::Relaxed);66 self.stop.store(true, Ordering::Relaxed);
67- let Some((thread, done, index)) = self.thread.take() else {67+ if let Some((thread, done)) = self.thread.take()
68- return;68+ && done.recv_timeout(JOIN_TIMEOUT) != Err(RecvTimeoutError::Timeout)
69- };69+ {
70- if done.recv_timeout(JOIN_TIMEOUT) != Err(RecvTimeoutError::Timeout) {
71 let _ = thread.join();70 let _ = thread.join();
72- return;
73 }71 }
74- tracing::warn!(?index, "camera thread stuck, device held until it returns");72+ }
75- held().push(index.clone());73+}
76- std::thread::spawn(move || {74+
77- let _ = done.recv();75+struct Hold(CameraIndex);
78- let _ = thread.join();76+
79- held().retain(|busy| *busy != index);77+impl Hold {
80- });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 .unwrap_or(CameraIndex::Index(0));148 .unwrap_or(CameraIndex::Index(0));
139 if held().contains(&index) {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 return None;151 return None;
142 }152 }
143 let count = format_count(&index);153 let count = format_count(&index);
@@ -228,6 +238,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
228 return Err(w().camera.needs_media_pack.to_string());238 return Err(w().camera.needs_media_pack.to_string());
229 }239 }
230 let index = index(device.as_deref()).ok_or_else(|| w().camera.no_response.to_string())?;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 let (negotiated_tx, negotiated_rx) = sync_channel::<Result<Negotiated, String>>(1);242 let (negotiated_tx, negotiated_rx) = sync_channel::<Result<Negotiated, String>>(1);
232 let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE);243 let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE);
233 let stop = Arc::new(AtomicBool::new(false));244 let stop = Arc::new(AtomicBool::new(false));
@@ -236,11 +247,12 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
236 let opened = Instant::now();247 let opened = Instant::now();
237 let (done_tx, done) = sync_channel::<()>(0);248 let (done_tx, done) = sync_channel::<()>(0);
238 let thread = {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 std::thread::Builder::new()251 std::thread::Builder::new()
241 .name("camera-capture".to_string())252 .name("camera-capture".to_string())
242 .spawn(move || {253 .spawn(move || {
243 let _done = done_tx;254 let _done = done_tx;
255+ let _hold = hold;
244 let clock = Clock {256 let clock = Clock {
245 opened,257 opened,
246 alive: &alive,258 alive: &alive,
@@ -274,7 +286,7 @@ pub fn open(top: Quality, device: Option<String>) -> Result<Camera, String> {
274 alive,286 alive,
275 opened,287 opened,
276 stop,288 stop,
277- thread: Some((thread, done, index)),289+ thread: Some((thread, done)),
278 _fake: None,290 _fake: None,
279 })291 })
280 }292 }