Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): flush every packet to the out-of-process muxer

Richie McIlroyf01a4e6b1ef6
diff --git a/crates/recording/src/output_pipeline/oop_muxer.rs b/crates/recording/src/output_pipeline/oop_muxer.rs
index 1cacf4fe2..909c6321f 100644
--- a/crates/recording/src/output_pipeline/oop_muxer.rs
+++ b/crates/recording/src/output_pipeline/oop_muxer.rs
@@ -365,7 +365,9 @@ impl MuxerSubprocess {
data,
};
- match write_packet_frame(stdin, &packet) {
+ // Flush every packet: anything left in the buffer is lost if the
+ // recorder dies, and a dead child only shows up as a write error.
+ match write_packet_frame(stdin, &packet).and_then(|()| Ok(stdin.flush()?)) {
Ok(()) => {
self.packets_written.fetch_add(1, Ordering::Relaxed);
Ok(())
@@ -871,6 +873,61 @@ mod tests {
(directory, subprocess)
}
+ fn script_subprocess(script: &str) -> (tempfile::TempDir, MuxerSubprocess, PathBuf) {
+ let directory = tempfile::tempdir().unwrap();
+ let binary = directory.path().join("muxer");
+ std::fs::write(&binary, format!("#!/bin/sh\n{script}\n")).unwrap();
+ std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
+ let config = MuxerSubprocessConfig {
+ output_directory: directory.path().join("video"),
+ init_segment_name: "init.mp4".into(),
+ media_segment_pattern: "segment_$Number%03d$.m4s".into(),
+ video_init: None,
+ audio_init: None,
+ };
+ let received = directory.path().join("received");
+ let subprocess = MuxerSubprocess::spawn(binary, config, None).unwrap();
+ (directory, subprocess, received)
+ }
+
+ #[test]
+ fn every_packet_reaches_the_child_before_the_next_one_is_written() {
+ let (directory, mut subprocess, received) =
+ script_subprocess("exec cat > \"$(dirname \"$0\")/received\"");
+ let size = |path: &Path| std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
+ let deadline = Instant::now() + Duration::from_secs(5);
+ while size(&received) == 0 && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ let after_start = size(&received);
+ assert!(after_start > 0, "init and start frames never arrived");
+ subprocess
+ .write_video_packet(0, 0, 1, true, &[7; 1000])
+ .unwrap();
+ let deadline = Instant::now() + Duration::from_secs(5);
+ while size(&received) < after_start + 1000 && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ assert!(
+ size(&received) >= after_start + 1000,
+ "a written packet stayed in the recorder's buffer"
+ );
+ drop(subprocess);
+ drop(directory);
+ }
+
+ #[test]
+ fn a_dead_child_is_reported_on_the_next_packet() {
+ let (_directory, mut subprocess, _) = script_subprocess("sleep 0.3; exit 3");
+ let child = subprocess.child.as_mut().unwrap();
+ let status = child.wait().unwrap();
+ assert_eq!(status.code(), Some(3));
+ assert!(matches!(
+ subprocess.write_video_packet(0, 0, 1, true, &[7; 1000]),
+ Err(MuxerSubprocessError::Crashed(_))
+ ));
+ }
+
#[test]
fn disk_full_stays_terminal_and_finish_confirms_shutdown() {
let (_directory, mut subprocess) = exited_subprocess(EXIT_DISK_FULL, 2);
diff --git a/crates/recording/tests/oop_muxer.rs b/crates/recording/tests/oop_muxer.rs
index c705d4077..c9ecbfd4f 100644
--- a/crates/recording/tests/oop_muxer.rs
+++ b/crates/recording/tests/oop_muxer.rs
@@ -232,10 +232,10 @@ fn finish_preserves_packets_when_a_slow_muxer_takes_more_than_five_seconds() {
let config = minimal_video_config(&directory.path().join("video"), Vec::new());
let mut subprocess = MuxerSubprocess::spawn(binary, config, None).unwrap();
let payload = vec![42; 128 * 1024];
+ let started = Instant::now();
subprocess
.write_video_packet(0, 0, 1, true, &payload)
.unwrap();
- let started = Instant::now();
let report = subprocess.finish().unwrap();
assert_eq!(report.exit_code, Some(0));
assert!(started.elapsed() >= Duration::from_secs(5));