Repository navigation
Fix #603: 4GB memory guard prevents OOM crash - #617
Conversation
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.
There was a problem hiding this comment.
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.
| 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); | ||
| } | ||
| } | ||
| }); | ||
|
|
There was a problem hiding this comment.
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.
| 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(); | |
| } | |
| }); | |
| } |
| 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); |
There was a problem hiding this comment.
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.
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.