Skip to content

Commit 8f341bb

Browse files
ZhiXiao-LinRoy Lin
andauthored
fix(vm): event-style log drain for run_deferred_main (#24)
Replace the fixed 150ms post-exit sleep with a bounded poll: wait until container.json stops growing for one 40ms interval (1s cap). Faster when the shim's log processor has already drained, safer when it lags. Co-authored-by: Roy Lin <roylin@a3s.box>
1 parent d1fd68b commit 8f341bb

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/runtime/src/vm/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,25 @@ impl VmManager {
530530
};
531531

532532
// Let the shim's log processor finish draining console.log into the json
533-
// file (it flushes as the VM halts) before reading the captured output.
534-
tokio::time::sleep(std::time::Duration::from_millis(150)).await;
533+
// file (it flushes as the VM halts): poll until container.json stops
534+
// growing for one interval (bounded at 1s) instead of a fixed sleep —
535+
// fast when the drain is already done, safe when it lags.
536+
let json_path = self
537+
.home_dir
538+
.join("boxes")
539+
.join(&self.box_id)
540+
.join("logs")
541+
.join("container.json");
542+
let drain_start = std::time::Instant::now();
543+
let mut last_len = u64::MAX;
544+
loop {
545+
let len = std::fs::metadata(&json_path).map(|m| m.len()).unwrap_or(0);
546+
if len == last_len || drain_start.elapsed() >= std::time::Duration::from_secs(1) {
547+
break;
548+
}
549+
last_len = len;
550+
tokio::time::sleep(std::time::Duration::from_millis(40)).await;
551+
}
535552
let (stdout, stderr) = self.read_container_logs();
536553
Ok(a3s_box_core::exec::ExecOutput {
537554
stdout,

0 commit comments

Comments
 (0)