-
Notifications
You must be signed in to change notification settings - Fork 1.5k
perf(recording): reduce recording memory pressure #1831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,7 +24,7 @@ use std::{ | |||||||||||||||||||||||||||||||||||||||||||
| use tracing::*; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_MP4_MUXER_BUFFER_SIZE: usize = 60; | ||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT: usize = 240; | ||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT: usize = 96; | ||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_MP4_AUDIO_FINISH_TIMEOUT: Duration = Duration::from_secs(2); | ||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_MP4_AUDIO_FINISH_TIMEOUT_INSTANT: Duration = Duration::from_secs(8); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -51,9 +51,17 @@ fn get_available_disk_space_mb(path: &std::path::Path) -> Option<u64> { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn get_mp4_muxer_buffer_size(instant_mode: bool) -> usize { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::var("CAP_MP4_MUXER_BUFFER_SIZE") | ||||||||||||||||||||||||||||||||||||||||||||
| .ok() | ||||||||||||||||||||||||||||||||||||||||||||
| .and_then(|s| s.parse().ok()) | ||||||||||||||||||||||||||||||||||||||||||||
| let instant_override = instant_mode | ||||||||||||||||||||||||||||||||||||||||||||
| .then(|| std::env::var("CAP_MP4_MUXER_BUFFER_SIZE_INSTANT").ok()) | ||||||||||||||||||||||||||||||||||||||||||||
| .flatten() | ||||||||||||||||||||||||||||||||||||||||||||
| .and_then(|s| s.parse().ok()); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| instant_override | ||||||||||||||||||||||||||||||||||||||||||||
| .or_else(|| { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::var("CAP_MP4_MUXER_BUFFER_SIZE") | ||||||||||||||||||||||||||||||||||||||||||||
| .ok() | ||||||||||||||||||||||||||||||||||||||||||||
| .and_then(|s| s.parse().ok()) | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| .unwrap_or(if instant_mode { | ||||||||||||||||||||||||||||||||||||||||||||
| DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1197,6 +1205,37 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||
| mod mp4_muxer_buffer_size { | ||||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn with_muxer_env<T>( | ||||||||||||||||||||||||||||||||||||||||||||
| global: Option<&str>, | ||||||||||||||||||||||||||||||||||||||||||||
| instant: Option<&str>, | ||||||||||||||||||||||||||||||||||||||||||||
| test: impl FnOnce() -> T, | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> T { | ||||||||||||||||||||||||||||||||||||||||||||
| let _guard = ENV_LOCK.lock().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| match global { | ||||||||||||||||||||||||||||||||||||||||||||
| Some(value) => std::env::set_var("CAP_MP4_MUXER_BUFFER_SIZE", value), | ||||||||||||||||||||||||||||||||||||||||||||
| None => std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE"), | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| match instant { | ||||||||||||||||||||||||||||||||||||||||||||
| Some(value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::set_var("CAP_MP4_MUXER_BUFFER_SIZE_INSTANT", value) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| None => std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE_INSTANT"), | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| let result = test(); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE"); | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE_INSTANT"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1229
to
+1237
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: crates/recording/src/output_pipeline/macos.rs
Line: 1229-1237
Comment:
Env vars set inside `with_muxer_env` are not cleaned up if `test()` panics. After the panic is caught by the test harness, `CAP_MP4_MUXER_BUFFER_SIZE` / `CAP_MP4_MUXER_BUFFER_SIZE_INSTANT` remain set, which can pollute later tests. Wrapping `test()` in `catch_unwind` and resuming after cleanup ensures the vars are always removed.
```suggestion
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(test));
unsafe {
std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE");
std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE_INSTANT");
}
match result {
Ok(v) => v,
Err(e) => std::panic::resume_unwind(e),
}
}
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn instant_mode_buffer_is_larger_than_normal() { | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1210,8 +1249,8 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn instant_mode_default_is_240() { | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT, 240); | ||||||||||||||||||||||||||||||||||||||||||||
| fn instant_mode_default_is_96() { | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT, 96); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1221,30 +1260,42 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn env_override_takes_precedence() { | ||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::set_var("CAP_MP4_MUXER_BUFFER_SIZE", "500"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| with_muxer_env(Some("500"), None, || { | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn instant_env_override_takes_precedence_over_global_override() { | ||||||||||||||||||||||||||||||||||||||||||||
| with_muxer_env(Some("500"), Some("120"), || { | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, 120); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn invalid_instant_override_falls_back_to_global_override() { | ||||||||||||||||||||||||||||||||||||||||||||
| with_muxer_env(Some("500"), Some("not_a_number"), || { | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, 500); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn invalid_env_falls_back_to_defaults() { | ||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::set_var("CAP_MP4_MUXER_BUFFER_SIZE", "not_a_number"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||
| std::env::remove_var("CAP_MP4_MUXER_BUFFER_SIZE"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, DEFAULT_MP4_MUXER_BUFFER_SIZE); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT); | ||||||||||||||||||||||||||||||||||||||||||||
| with_muxer_env(Some("not_a_number"), None, || { | ||||||||||||||||||||||||||||||||||||||||||||
| let normal = get_mp4_muxer_buffer_size(false); | ||||||||||||||||||||||||||||||||||||||||||||
| let instant = get_mp4_muxer_buffer_size(true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(normal, DEFAULT_MP4_MUXER_BUFFER_SIZE); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(instant, DEFAULT_MP4_MUXER_BUFFER_SIZE_INSTANT); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any test panics inside
with_muxer_env(e.g. anassert_eq!fails), the thread unwinds while holding_guard. Rust marks theMutexas poisoned when a thread panics while holding a lock. Every subsequent call toENV_LOCK.lock().unwrap()will then panic with aPoisonError, causing all remaining tests in the module to fail regardless of their own correctness. Use.unwrap_or_else(|e| e.into_inner())to tolerate a poisoned mutex.Prompt To Fix With AI