Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: WhatsApp gateway dying silently — pipe stdin + add watchdog auto-restart

Louis Beaumontcf2dcd5f86dd
diff --git a/crates/screenpipe-connect/src/whatsapp/mod.rs b/crates/screenpipe-connect/src/whatsapp/mod.rs
index 6708c36ed..3bcc74c16 100644
--- a/crates/screenpipe-connect/src/whatsapp/mod.rs
+++ b/crates/screenpipe-connect/src/whatsapp/mod.rs
@@ -12,6 +12,7 @@ use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::process::Stdio;
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
@@ -61,6 +62,11 @@ pub struct WhatsAppGateway {
status: Arc<Mutex<WhatsAppStatus>>,
child: Arc<Mutex<Option<Child>>>,
http_port: Arc<Mutex<Option<u16>>>,
+ /// Incremented each time start_pairing is called. The watchdog checks
+ /// this to know if it's been superseded by a newer start_pairing call.
+ generation: Arc<std::sync::atomic::AtomicU64>,
+ /// Whether auto-restart is enabled (set to true after successful pairing).
+ auto_restart: Arc<AtomicBool>,
}
impl WhatsAppGateway {
@@ -70,6 +76,8 @@ impl WhatsAppGateway {
status: Arc::new(Mutex::new(WhatsAppStatus::Disconnected)),
child: Arc::new(Mutex::new(None)),
http_port: Arc::new(Mutex::new(None)),
+ generation: Arc::new(std::sync::atomic::AtomicU64::new(0)),
+ auto_restart: Arc::new(AtomicBool::new(false)),
}
}
@@ -86,18 +94,34 @@ impl WhatsAppGateway {
/// Uses node (not bun) because Baileys requires a full ws implementation
/// and bun's built-in WebSocket is missing 'upgrade'/'unexpected-response' events.
pub async fn start_pairing(&self, bun_path: &str) -> Result<()> {
- // Kill existing process if any
- self.stop().await;
+ // Bump generation so any existing watchdog exits on its next check
+ let gen = self.generation.fetch_add(1, Ordering::SeqCst) + 1;
+
+ // Stop any existing process
+ self.stop_process().await;
+
+ self.ensure_deps(bun_path).await?;
+ self.spawn_gateway().await?;
+
+ // Enable auto-restart now that we've successfully started
+ self.auto_restart.store(true, Ordering::SeqCst);
+
+ // Spawn watchdog that restarts the gateway if it dies unexpectedly.
+ // It will exit if generation changes (meaning start_pairing was called again).
+ self.spawn_watchdog(gen);
+
+ Ok(())
+ }
+ /// Install baileys + pino if needed, write gateway script to disk.
+ async fn ensure_deps(&self, bun_path: &str) -> Result<()> {
let session_dir = self.screenpipe_dir.join("whatsapp-session");
std::fs::create_dir_all(&session_dir)?;
- // Write gateway script to disk
let script_path = self.screenpipe_dir.join("whatsapp-gateway.mjs");
std::fs::write(&script_path, GATEWAY_JS)
.context("failed to write whatsapp gateway script")?;
- // Install baileys + pino if needed
let node_modules = self
.screenpipe_dir
.join("node_modules")
@@ -116,19 +140,29 @@ impl WhatsAppGateway {
anyhow::bail!("failed to install @whiskeysockets/baileys");
}
}
+ Ok(())
+ }
+
+ /// Spawn the node gateway process, wire up stdout/stderr readers.
+ async fn spawn_gateway(&self) -> Result<()> {
+ let session_dir = self.screenpipe_dir.join("whatsapp-session");
+ let script_path = self.screenpipe_dir.join("whatsapp-gateway.mjs");
- // Spawn the gateway using node (not bun) because Baileys needs
- // full ws WebSocket support (upgrade/unexpected-response events).
let node_path = which_node().unwrap_or_else(|| "node".to_string());
let mut child = Command::new(&node_path)
.arg(script_path.to_str().unwrap())
.env("WHATSAPP_SESSION_DIR", session_dir.to_str().unwrap())
+ .stdin(Stdio::piped()) // keep stdin open so gateway doesn't self-terminate
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.context("failed to spawn whatsapp gateway")?;
+ // Take stdin handle and hold it — dropping it would close the pipe and kill the gateway.
+ // We store it alongside the child so it lives as long as the child does.
+ let _stdin = child.stdin.take().expect("stdin piped");
+
let stdout = child.stdout.take().expect("stdout piped");
let stderr = child.stderr.take().expect("stderr piped");
@@ -138,10 +172,12 @@ impl WhatsAppGateway {
}
*self.status.lock().await = WhatsAppStatus::WaitingForQr;
+ *self.http_port.lock().await = None;
// Read stdout events
let status = self.status.clone();
let http_port = self.http_port.clone();
+ let auto_restart = self.auto_restart.clone();
tokio::spawn(async move {
let reader = BufReader::new(stdout);
let mut lines = reader.lines();
@@ -158,6 +194,8 @@ impl WhatsAppGateway {
Ok(GatewayEvent::Disconnected { reason }) => {
warn!("whatsapp: disconnected — {}", reason);
if reason == "logged_out" {
+ // User logged out — don't auto-restart
+ auto_restart.store(false, Ordering::SeqCst);
*status.lock().await = WhatsAppStatus::Disconnected;
}
}
@@ -177,6 +215,18 @@ impl WhatsAppGateway {
}
}
}
+ // stdout closed = process died
+ info!("whatsapp: gateway stdout closed (process exited)");
+ });
+
+ // Hold stdin handle in a background task so it stays open for the lifetime of the process
+ tokio::spawn(async move {
+ // Just hold the handle. When stop_process() kills the child, this task
+ // will end because the pipe will break.
+ let _keep_alive = _stdin;
+ // Block forever — stdin handle stays open, keeping the gateway alive.
+ // When stop_process() kills the child, this task becomes inert.
+ loop { tokio::time::sleep(std::time::Duration::from_secs(3600)).await; }
});
// Log stderr
@@ -191,12 +241,208 @@ impl WhatsAppGateway {
Ok(())
}
- /// Stop the gateway process.
- pub async fn stop(&self) {
+ /// Watchdog: monitors the child process and restarts it if it dies unexpectedly.
+ /// Exits if generation changes (a newer start_pairing superseded this one).
+ fn spawn_watchdog(&self, my_generation: u64) {
+ let child = self.child.clone();
+ let status = self.status.clone();
+ let http_port = self.http_port.clone();
+ let auto_restart = self.auto_restart.clone();
+ let generation = self.generation.clone();
+ let screenpipe_dir = self.screenpipe_dir.clone();
+
+ tokio::spawn(async move {
+ loop {
+ tokio::time::sleep(std::time::Duration::from_secs(10)).await;
+
+ // Exit if we've been superseded by a newer start_pairing call
+ if generation.load(Ordering::SeqCst) != my_generation {
+ info!("whatsapp: watchdog exiting (superseded by newer generation)");
+ return;
+ }
+
+ // Check if process is still alive
+ let is_dead = {
+ let mut lock = child.lock().await;
+ if let Some(ref mut c) = *lock {
+ // try_wait returns Ok(Some(status)) if exited, Ok(None) if still running
+ match c.try_wait() {
+ Ok(Some(exit_status)) => {
+ info!("whatsapp: gateway exited with {}", exit_status);
+ true
+ }
+ Ok(None) => false, // still running
+ Err(e) => {
+ warn!("whatsapp: failed to check child status: {}", e);
+ true
+ }
+ }
+ } else {
+ true // no child at all
+ }
+ };
+
+ if !is_dead {
+ continue;
+ }
+
+ // Process is dead — should we restart?
+ if !auto_restart.load(Ordering::SeqCst) {
+ info!("whatsapp: gateway exited and auto-restart is disabled, watchdog stopping");
+ *status.lock().await = WhatsAppStatus::Disconnected;
+ *http_port.lock().await = None;
+ return;
+ }
+
+ // Only restart if session files exist (user hasn't logged out)
+ let has_session = screenpipe_dir
+ .join("whatsapp-session")
+ .join("creds.json")
+ .exists();
+ if !has_session {
+ info!("whatsapp: no session on disk, watchdog stopping");
+ auto_restart.store(false, Ordering::SeqCst);
+ *status.lock().await = WhatsAppStatus::Disconnected;
+ *http_port.lock().await = None;
+ return;
+ }
+
+ // Clear dead child
+ {
+ let mut lock = child.lock().await;
+ *lock = None;
+ }
+ *http_port.lock().await = None;
+
+ // Restart with backoff
+ warn!("whatsapp: gateway died unexpectedly, restarting in 5s...");
+ *status.lock().await = WhatsAppStatus::Error {
+ message: "gateway crashed, restarting...".to_string(),
+ };
+
+ tokio::time::sleep(std::time::Duration::from_secs(5)).await;
+
+ // Re-check generation after backoff — may have been superseded
+ if generation.load(Ordering::SeqCst) != my_generation {
+ info!("whatsapp: watchdog exiting after backoff (superseded)");
+ return;
+ }
+
+ // Re-spawn
+ let session_dir = screenpipe_dir.join("whatsapp-session");
+ let script_path = screenpipe_dir.join("whatsapp-gateway.mjs");
+ let node_path = which_node().unwrap_or_else(|| "node".to_string());
+
+ match Command::new(&node_path)
+ .arg(script_path.to_str().unwrap())
+ .env("WHATSAPP_SESSION_DIR", session_dir.to_str().unwrap())
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .kill_on_drop(true)
+ .spawn()
+ {
+ Ok(mut new_child) => {
+ let stdin_handle = new_child.stdin.take().expect("stdin piped");
+ let stdout = new_child.stdout.take().expect("stdout piped");
+ let stderr = new_child.stderr.take().expect("stderr piped");
+
+ {
+ let mut lock = child.lock().await;
+ *lock = Some(new_child);
+ }
+ *status.lock().await = WhatsAppStatus::WaitingForQr;
+
+ // Wire up stdout reader
+ let s = status.clone();
+ let hp = http_port.clone();
+ let ar = auto_restart.clone();
+ tokio::spawn(async move {
+ let reader = BufReader::new(stdout);
+ let mut lines = reader.lines();
+ while let Ok(Some(line)) = lines.next_line().await {
+ match serde_json::from_str::<GatewayEvent>(&line) {
+ Ok(GatewayEvent::Qr { data }) => {
+ info!("whatsapp: qr code received");
+ *s.lock().await = WhatsAppStatus::QrReady { qr: data };
+ }
+ Ok(GatewayEvent::Connected { name, phone }) => {
+ info!("whatsapp: connected as {} ({})", name, phone);
+ *s.lock().await =
+ WhatsAppStatus::Connected { name, phone };
+ }
+ Ok(GatewayEvent::Disconnected { reason }) => {
+ warn!("whatsapp: disconnected — {}", reason);
+ if reason == "logged_out" {
+ ar.store(false, Ordering::SeqCst);
+ *s.lock().await = WhatsAppStatus::Disconnected;
+ }
+ }
+ Ok(GatewayEvent::Error { message }) => {
+ error!("whatsapp gateway error: {}", message);
+ *s.lock().await = WhatsAppStatus::Error { message };
+ }
+ Ok(GatewayEvent::Http { port }) => {
+ info!("whatsapp: gateway HTTP on port {}", port);
+ *hp.lock().await = Some(port);
+ }
+ Ok(GatewayEvent::SendResult { .. }) => {}
+ Err(e) => {
+ warn!(
+ "whatsapp gateway unparseable line: {} ({})",
+ line, e
+ );
+ }
+ }
+ }
+ });
+
+ // Hold stdin
+ tokio::spawn(async move {
+ let _keep = stdin_handle;
+ // Block forever — stdin handle stays open, keeping the gateway alive.
+ // When stop_process() kills the child, this task becomes inert.
+ loop { tokio::time::sleep(std::time::Duration::from_secs(3600)).await; }
+ });
+
+ // Log stderr
+ tokio::spawn(async move {
+ let reader = BufReader::new(stderr);
+ let mut lines = reader.lines();
+ while let Ok(Some(line)) = lines.next_line().await {
+ warn!("whatsapp gateway stderr: {}", line);
+ }
+ });
+
+ info!("whatsapp: gateway restarted successfully");
+ }
+ Err(e) => {
+ error!("whatsapp: failed to restart gateway: {}", e);
+ *status.lock().await = WhatsAppStatus::Error {
+ message: format!("restart failed: {}", e),
+ };
+ // Will retry on next watchdog cycle
+ }
+ }
+ }
+ });
+ }
+
+ /// Kill the child process without disabling auto-restart or signaling the watchdog.
+ async fn stop_process(&self) {
let mut lock = self.child.lock().await;
if let Some(mut child) = lock.take() {
let _ = child.kill().await;
}
+ *self.http_port.lock().await = None;
+ }
+
+ /// Stop the gateway process, disable auto-restart, and stop the watchdog.
+ pub async fn stop(&self) {
+ self.auto_restart.store(false, Ordering::SeqCst);
+ // Bump generation so watchdog exits on its next check
+ self.generation.fetch_add(1, Ordering::SeqCst);
+ self.stop_process().await;
*self.status.lock().await = WhatsAppStatus::Disconnected;
}