fix(desktop): stop the sidecar with SIGTERM first, and free a stale WhatsApp profile (UF-046)
On macOS and Linux the shell stopped the sidecar with Child::kill(), which is SIGKILL, so the sidecar never ran its shutdown: the processes it started (MCP servers, a managed Ollama, the WhatsApp browser) kept running. After an app restart the orphaned WhatsApp browser still held the session profile, and the channel failed with "The browser is already running". The shell now sends SIGTERM, waits up to 5 seconds for the sidecar to exit, then sends SIGKILL; Windows keeps its kill-on-close job. For crashes, where no shutdown runs, the WhatsApp channel stops a browser left over from an earlier run before starting its own. It only stops a process whose command line uses this profile and whose parent is gone; a browser another running Waggle server owns is left alone, and the channel says so instead of retrying.
This commit is contained in:
@@ -27,6 +27,7 @@ import type {
|
||||
} from './types.js';
|
||||
import { chunkText } from './types.js';
|
||||
import { resolveWhatsAppBrowser, WhatsAppBrowserError } from './whatsapp-browser.js';
|
||||
import { releaseOrphanedBrowser, type ProfileLockState } from './whatsapp-profile-lock.js';
|
||||
|
||||
/** WhatsApp accepts ~65k; 4000 keeps replies phone-readable. */
|
||||
export const WHATSAPP_MAX_TEXT = 4000;
|
||||
@@ -103,6 +104,8 @@ export interface WhatsAppAdapterOptions {
|
||||
browserResolver?: WaBrowserResolver;
|
||||
/** Test seam. */
|
||||
clientFactory?: WaClientFactory;
|
||||
/** Test seam: frees the session profile from a browser left by an earlier run. */
|
||||
releaseStaleBrowser?: (profileDir: string) => Promise<ProfileLockState>;
|
||||
backoffCapMs?: number;
|
||||
}
|
||||
|
||||
@@ -114,6 +117,7 @@ export class WhatsAppAdapter implements ChannelAdapter {
|
||||
private readonly log: WhatsAppAdapterOptions['log'];
|
||||
private readonly browserResolver: WaBrowserResolver;
|
||||
private readonly clientFactory: WaClientFactory;
|
||||
private readonly releaseStaleBrowser: (profileDir: string) => Promise<ProfileLockState>;
|
||||
private readonly backoffCap: number;
|
||||
|
||||
private running = false;
|
||||
@@ -136,6 +140,7 @@ export class WhatsAppAdapter implements ChannelAdapter {
|
||||
await resolveWhatsAppBrowser({ dataDir: opts.dataDir, onProgress })
|
||||
).executablePath);
|
||||
this.clientFactory = opts.clientFactory ?? defaultClientFactory;
|
||||
this.releaseStaleBrowser = opts.releaseStaleBrowser ?? (profileDir => releaseOrphanedBrowser(profileDir));
|
||||
this.backoffCap = opts.backoffCapMs ?? BACKOFF_CAP_MS;
|
||||
this.backoff = Math.min(BACKOFF_START_MS, this.backoffCap);
|
||||
}
|
||||
@@ -208,6 +213,21 @@ export class WhatsAppAdapter implements ChannelAdapter {
|
||||
let client: WaClientLike;
|
||||
try {
|
||||
this.detail = 'Starting the browser';
|
||||
// LocalAuth keeps the profile in <authDir>/session.
|
||||
const lock = await this.releaseStaleBrowser(path.join(this.authDir, 'session'));
|
||||
if (!current()) return;
|
||||
if (lock.kind === 'in-use') {
|
||||
// Another server's live session: taking it over would only make the two
|
||||
// stop each other's WhatsApp, so wait for the user instead.
|
||||
this.handleDrop(
|
||||
`another Waggle server on this computer is running WhatsApp with this session (browser pid ${lock.pid}); stop WhatsApp there, then start it here`,
|
||||
false,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (lock.kind === 'released') {
|
||||
this.log.warn(`[whatsapp] stopped a browser left over from an earlier run (pid ${lock.pid})`);
|
||||
}
|
||||
client = await this.clientFactory({ authDir: this.authDir, executablePath });
|
||||
} catch (e: unknown) {
|
||||
if (current()) this.handleDrop(e instanceof Error ? e.message : String(e), true);
|
||||
|
||||
Reference in New Issue
Block a user