Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/bin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ joins mid-recording is kept, though only the finished file indexes it.
fragmented, in place and the same way: it indexes every fragment written in full,
late renditions included, and cuts a partial one at the end.

The recorder holds an exclusive file lock on its output while it records, and the
OS drops the lock with a killed recorder. `repair` refuses a file it can't lock,
since a recorder still has it open and repairing it would corrupt it. A filesystem
without file locks (some network mounts) is refused by both: the recorder won't start there.
On Windows the lock also blocks other programs from reading the file until the
recording finishes, so copy or preview it afterwards.

```bash
moq ... export mp4 --output recording.mp4
moq repair mp4 recording.mp4
Expand Down
22 changes: 0 additions & 22 deletions quest/m1/mp4-repair-lock.md

This file was deleted.

9 changes: 9 additions & 0 deletions rs/moq-cli/src/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ impl Mp4 {
.write(true)
.open(&path)
.with_context(|| format!("failed to open {}", path.display()))?;
// Held for the repair's life: an `export mp4` recording holds it until it closes the file,
// so a file we can't lock is still being written, and repairing it would corrupt it.
// A filesystem without locks refuses too, since the recorder refuses to start there.
file.try_lock().with_context(|| {
format!(
"failed to lock {}, so it may still be open by a recorder",
path.display()
)
})?;
let (file, finish) = tokio::task::spawn_blocking(move || {
let mut file = file;
let finish = moq_mux::container::mp4::repair(&mut file);
Expand Down
17 changes: 15 additions & 2 deletions rs/moq-cli/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,22 @@ impl Subscribe {
let mut mp4 = moq_mux::container::mp4::Export::new(self.source, stream)
.with_delay(Delay::default().with_max(self.args.max_delay))
.with_fragment_duration(self.args.fragment_duration);
let mut file = tokio::fs::File::create_new(&path)
let file = tokio::fs::File::create_new(&path)
.await
.with_context(|| format!("failed to create {}", path.display()))?;
.with_context(|| format!("failed to create {}", path.display()))?
.into_std()
.await;
// Held until the file closes, which `moq repair mp4` reads as a recorder still writing.
// The OS drops it with a killed recorder, so that recording stays repairable. A file that
// can't be locked could never be repaired safely, so it isn't recorded.
// On Windows the lock is mandatory and also blocks other readers; a shared lock wouldn't
// help, since it denies writes to the handle holding it too.
if let Err(err) = file.try_lock() {
Comment thread
kixelated marked this conversation as resolved.
Resolved
drop(file);
tokio::fs::remove_file(&path).await?;
return Err(err).with_context(|| format!("failed to lock {}, so it could not be repaired", path.display()));
}
let mut file = tokio::fs::File::from_std(file);

// A failed export still finishes what it recorded; a failed write leaves the file as is.
let mut stopped = false;
Expand Down
62 changes: 53 additions & 9 deletions rs/moq-cli/tests/integration/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,48 @@ async fn a_passthrough_export_ends_with_its_redundant_importer() {
feeding.abort();
}

/// `repair mp4` refuses a file something else holds the lock on, as a recorder does while it
/// writes, and leaves it untouched.
#[tokio::test]
async fn repair_refuses_a_locked_recording() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("recording.mp4");
std::fs::write(&path, b"not scanned: the lock refuses first").unwrap();
let recorder = std::fs::File::open(&path).unwrap();
recorder.try_lock().unwrap();

let output = moq_repair(&path).await;
assert!(!output.status.success(), "repaired a file a recorder holds");
assert!(
String::from_utf8_lossy(&output.stderr).contains("may still be open by a recorder"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(std::fs::read(&path).unwrap(), b"not scanned: the lock refuses first");

// Once the recorder lets go, the file is scanned instead.
drop(recorder);
let output = moq_repair(&path).await;
assert!(
!String::from_utf8_lossy(&output.stderr).contains("may still be open by a recorder"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}

/// Run `moq repair mp4` on the file.
async fn moq_repair(path: &std::path::Path) -> std::process::Output {
let output = tokio::process::Command::new(env!("CARGO_BIN_EXE_moq"))
.env_remove("KIO_DENY_HOST_TIMERS")
.args(["repair", "mp4"])
.arg(path)
.output();
kio::clock::timeout(TIMEOUT, output)
.await
.expect("repair never exited")
.unwrap()
}

/// `repair mp4` finishes a recording whose `export mp4` was killed as a regular MP4, and leaves
/// it alone once finished.
#[tokio::test]
Expand All @@ -1137,6 +1179,16 @@ async fn a_killed_mp4_recording_is_repaired() {
})
.await
.expect("the recording never grew");
// The recorder holds its lock while it writes, so repairing now would corrupt the file.
let mid = std::fs::read(&path).unwrap();
let refused = moq_repair(&path).await;
assert!(!refused.status.success(), "repaired a recording still being written");
assert!(String::from_utf8_lossy(&refused.stderr).contains("may still be open by a recorder"));
assert!(
std::fs::read(&path).unwrap().starts_with(&mid),
"a refused repair rewrote the file"
);

recording.kill().await.unwrap();
let killed = std::fs::read(&path).unwrap();
assert_eq!(
Expand All @@ -1146,15 +1198,7 @@ async fn a_killed_mp4_recording_is_repaired() {
);

let repair = || async {
let output = tokio::process::Command::new(env!("CARGO_BIN_EXE_moq"))
.env_remove("KIO_DENY_HOST_TIMERS")
.args(["repair", "mp4"])
.arg(&path)
.output();
let output = kio::clock::timeout(TIMEOUT, output)
.await
.expect("repair never exited")
.unwrap();
let output = moq_repair(&path).await;
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
std::fs::read(&path).unwrap()
};
Expand Down