Skip to content

Fix #603: 4GB memory guard prevents OOM crash - #617

Merged
joelteply merged 1 commit into
mainfrom
fix/rust-memory-guard
Mar 30, 2026
Merged

joelteply merged 1 commit into
mainfrom
fix/rust-memory-guard

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Rust worker leaks ~6GB/hour. Guard exits at 4GB instead of letting it hit 30GB and crash. Clear error message points to #603. Real leak fix needs profiling.

Rust worker leaks ~6GB/hour. This guard checks RSS every 10s and exits
gracefully at 4GB with a clear error message. Prevents the 30GB OOM
that crashes everything. The real leak fix needs profiling.
Copilot AI review requested due to automatic review settings March 30, 2026 06:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an RSS-based “OOM guard” to the Rust IPC server to proactively exit at a fixed memory threshold, avoiding an eventual OS-level OOM crash while the underlying leak (#603) is investigated.

Changes:

  • Extends the existing periodic memory leak reporter to also enforce a 4GB RSS exit threshold.
  • Emits a fatal diagnostic message pointing to issue #603 before terminating the process.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 974 to 994
let mem_rt = state.rt_handle.clone();
mem_rt.spawn(async {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
const MAX_RSS_MB: u64 = 4096; // 4GB — exit before OOM kills us ungracefully
loop {
interval.tick().await;
dump_memory_report();
let rss = current_rss_mb();
if rss > MAX_RSS_MB {
eprintln!(
"[MEMLEAK] FATAL: RSS {}MB exceeds {}MB limit — exiting gracefully to avoid OOM. \
Restart with: npm start. Fix tracked in #603.",
rss, MAX_RSS_MB
);
// Give time for the message to flush
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
std::process::exit(1);
}
}
});

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_rss_mb() is implemented as a macOS-only RSS probe and returns 0 on non-macOS builds in this file. That means this 4GB OOM guard will never trigger on Linux/Windows (rss stays 0), which undermines the stated goal of preventing OOM crashes. Consider either (a) implementing current_rss_mb() for other targets (e.g., procfs on Linux) or (b) gating the guard with the same cfg(target_os = "macos") and logging that the guard is unsupported on this platform.

Suggested change
let mem_rt = state.rt_handle.clone();
mem_rt.spawn(async {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
const MAX_RSS_MB: u64 = 4096; // 4GB — exit before OOM kills us ungracefully
loop {
interval.tick().await;
dump_memory_report();
let rss = current_rss_mb();
if rss > MAX_RSS_MB {
eprintln!(
"[MEMLEAK] FATAL: RSS {}MB exceeds {}MB limit — exiting gracefully to avoid OOM. \
Restart with: npm start. Fix tracked in #603.",
rss, MAX_RSS_MB
);
// Give time for the message to flush
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
std::process::exit(1);
}
}
});
#[cfg(target_os = "macos")]
{
let mem_rt = state.rt_handle.clone();
mem_rt.spawn(async {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
const MAX_RSS_MB: u64 = 4096; // 4GB — exit before OOM kills us ungracefully
loop {
interval.tick().await;
dump_memory_report();
let rss = current_rss_mb();
if rss > MAX_RSS_MB {
eprintln!(
"[MEMLEAK] FATAL: RSS {}MB exceeds {}MB limit — exiting gracefully to avoid OOM. \
Restart with: npm start. Fix tracked in #603.",
rss, MAX_RSS_MB
);
// Give time for the message to flush
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
std::process::exit(1);
}
}
});
}
#[cfg(not(target_os = "macos"))]
{
let mem_rt = state.rt_handle.clone();
log_info!(
"ipc",
"server",
"RSS-based OOM guard is only supported on macOS; running leak reporter without exit guard on this platform"
);
mem_rt.spawn(async {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
loop {
interval.tick().await;
dump_memory_report();
}
});
}

Copilot uses AI. Check for mistakes.
Comment on lines +983 to +990
eprintln!(
"[MEMLEAK] FATAL: RSS {}MB exceeds {}MB limit — exiting gracefully to avoid OOM. \
Restart with: npm start. Fix tracked in #603.",
rss, MAX_RSS_MB
);
// Give time for the message to flush
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
std::process::exit(1);

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleeping 500ms to "flush" the message is not a reliable way to ensure the fatal message is emitted before std::process::exit(1), especially when stderr is redirected. Prefer explicitly flushing stderr (or using the project's logging facilities) immediately after writing the message, and then exiting.

Copilot uses AI. Check for mistakes.
@joelteply
joelteply merged commit 994197e into main Mar 30, 2026
6 of 8 checks passed
@joelteply
joelteply deleted the fix/rust-memory-guard branch March 30, 2026 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants