Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: serialize FaceTime call teardown and poll ticks (#11780)

[._.]/ Adam Eivycfb9c4e31771
diff --git a/server/services/voice/callSession.js b/server/services/voice/callSession.js
index 2661a48dd8..c61fb58815 100644
--- a/server/services/voice/callSession.js
+++ b/server/services/voice/callSession.js
@@ -263,6 +263,8 @@ export async function pollCall() {
return publicState();
}
+ // The probe can outlast the interval; the call may have ended meanwhile.
+ if (!publicState().active) return publicState();
if (observed?.state === 'ended' || observed?.state === 'idle') return endCall('remote-hangup', { disconnectObserved: true });
if (observed?.state === 'connected' && session.state === 'dialing') setState('listening');
@@ -274,10 +276,16 @@ export async function pollCall() {
const startPolling = () => {
stopPolling();
+ let inFlight = false;
timer = setInterval(() => {
+ // A probe can take far longer than the interval; skip rather than overlap.
+ if (inFlight) return;
+ inFlight = true;
// Runs outside the request lifecycle: an unhandled rejection here would
// take the process down rather than reach error middleware.
- pollCall().catch((error) => console.error(`❌ voice call: poll failed: ${error.message}`));
+ pollCall()
+ .catch((error) => console.error(`❌ voice call: poll failed: ${error.message}`))
+ .finally(() => { inFlight = false; });
}, PROBE_INTERVAL_MS);
timer.unref?.();
};
@@ -304,8 +312,13 @@ const stopIncomingWatcher = () => {
const startIncomingWatcher = () => {
stopIncomingWatcher();
+ let inFlight = false;
incomingTimer = setInterval(() => {
- pollIncoming().catch((error) => console.error(`❌ voice call: incoming poll failed: ${error.message}`));
+ if (inFlight) return;
+ inFlight = true;
+ pollIncoming()
+ .catch((error) => console.error(`❌ voice call: incoming poll failed: ${error.message}`))
+ .finally(() => { inFlight = false; });
}, PROBE_INTERVAL_MS);
incomingTimer.unref?.();
};
@@ -505,8 +518,18 @@ const MIND_TRANSCRIPT_MAX_CHARS = 6_000;
* The journal append is best-effort: a failed write must not leave the session
* stuck `connected`, because the state is what gates placing the next call.
*/
-export async function endCall(reason = 'ended', { disconnectObserved = false } = {}) {
- if (session.state === 'idle') return publicState();
+export function endCall(reason = 'ended', options = {}) {
+ // Overlapping triggers (slow probes, a user hangup racing a poll rule) join
+ // the teardown already running instead of repeating hangup/journal/handoff.
+ if (endingCall) return endingCall;
+ if (session.state === 'idle') return Promise.resolve(publicState());
+ endingCall = runEndCall(reason, options).finally(() => { endingCall = null; });
+ return endingCall;
+}
+
+let endingCall = null;
+
+async function runEndCall(reason, { disconnectObserved = false }) {
stopPolling();
const transcript = session.transcript;
const origin = session.origin;
diff --git a/server/services/voice/callSession.test.js b/server/services/voice/callSession.test.js
index 5fd5e1dfa1..9c066de8f7 100644
--- a/server/services/voice/callSession.test.js
+++ b/server/services/voice/callSession.test.js
@@ -594,3 +594,49 @@ describe('FaceTime incoming-call watcher', () => {
expect(probe).not.toHaveBeenCalled();
});
});
+
+describe('FaceTime call teardown re-entry', () => {
+ it('ends once when two overlapping polls both observe a remote hangup', async () => {
+ attachHost(socket());
+ await startCall({ origin: 'mind' });
+ recordTurn('caller', 'Bye');
+ probe.mockResolvedValue({ state: 'ended' });
+ await Promise.all([pollCall(), pollCall()]);
+ expect(appendJournal).toHaveBeenCalledTimes(1);
+ expect(enqueueMindMessage).toHaveBeenCalledTimes(1);
+ });
+
+ it('joins a teardown already held on hangup instead of pressing hangup again', async () => {
+ attachHost(socket());
+ await startCall({ origin: 'mind' });
+ recordTurn('caller', 'Bye');
+ let release;
+ hangup.mockImplementation(() => new Promise(resolve => { release = () => resolve({ state: 'ended' }); }));
+ const first = endCall('user-hangup');
+ const second = endCall('max-duration');
+ await vi.waitFor(() => expect(hangup).toHaveBeenCalledTimes(1));
+ release();
+ await Promise.all([first, second]);
+ expect(hangup).toHaveBeenCalledTimes(1);
+ expect(appendJournal).toHaveBeenCalledTimes(1);
+ expect(enqueueMindMessage).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not start a second probe while the previous tick is still pending', async () => {
+ vi.useFakeTimers();
+ try {
+ attachHost(socket());
+ let release;
+ probe.mockImplementation(() => new Promise(resolve => { release = () => resolve({ state: 'connected' }); }));
+ await startCall();
+ probe.mockClear();
+ await vi.advanceTimersByTimeAsync(PROBE_INTERVAL_MS * 3);
+ expect(probe).toHaveBeenCalledTimes(1);
+ release();
+ await vi.advanceTimersByTimeAsync(PROBE_INTERVAL_MS);
+ expect(probe).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});