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(update): arm restart-polling fallback on socket disconnect, not just the 'restart' step

Adam Eivyb7cc3f8f5643
diff --git a/client/src/components/apps/tabs/UpdateTab.jsx b/client/src/components/apps/tabs/UpdateTab.jsx
index 2dd13b8701..0d5836c244 100644
--- a/client/src/components/apps/tabs/UpdateTab.jsx
+++ b/client/src/components/apps/tabs/UpdateTab.jsx
@@ -43,6 +43,9 @@ export default function UpdateTab() {
const attemptsRef = useRef(0);
const targetVersionRef = useRef(null);
const preUpdateVersionRef = useRef(null);
+ // Mirrors `updating` for the socket 'disconnect' listener below, which is
+ // registered once on mount and would otherwise close over a stale `false`.
+ const updatingRef = useRef(false);
// Tracks whether the health endpoint went down during a restart poll. A
// reconcile (issue #1779) often lands the SAME version (new commits, no
// release bump), so version-change detection alone can't confirm completion —
@@ -65,8 +68,21 @@ export default function UpdateTab() {
fetchStatus();
}, [fetchStatus]);
+ useEffect(() => {
+ updatingRef.current = updating;
+ }, [updating]);
+
// Socket event listeners for update progress
useEffect(() => {
+ // Shared by the 'restart' step, 'portos:update:complete', and the
+ // 'disconnect' fallback below — all three mean "the server is (or is
+ // about to be) restarting, stop trusting the socket and start polling."
+ const armRestartPolling = () => {
+ setUpdating(false);
+ setPolling(true);
+ toast.loading('PortOS is restarting...', { id: 'portos-update-restart', duration: Infinity });
+ };
+
const handleStep = ({ step, status: stepStatus, message }) => {
setSteps(prev => {
const existing = prev.findIndex(s => s.step === step);
@@ -81,22 +97,20 @@ export default function UpdateTab() {
// When the server signals it's restarting, begin health polling immediately.
// The PM2 restart may kill the server before portos:update:complete fires.
if ((step === 'restarting' || step === 'restart') && stepStatus !== 'error' && targetVersionRef.current) {
- setUpdating(false);
- setPolling(true);
- toast.loading('PortOS is restarting...', { id: 'portos-update-restart', duration: Infinity });
+ armRestartPolling();
}
};
const handleComplete = ({ success, newVersion, versionKnown }) => {
- setUpdating(false);
- if (success) {
- // Use server-reported actual version when available; fall back to target
- if (versionKnown && newVersion) {
- targetVersionRef.current = newVersion;
- }
- setPolling(true);
- toast.loading('PortOS is restarting...', { id: 'portos-update-restart', duration: Infinity });
+ if (!success) {
+ setUpdating(false);
+ return;
}
+ // Use server-reported actual version when available; fall back to target
+ if (versionKnown && newVersion) {
+ targetVersionRef.current = newVersion;
+ }
+ armRestartPolling();
};
const handleError = ({ message }) => {
@@ -106,14 +120,30 @@ export default function UpdateTab() {
setUpdateError(message);
};
+ // `pm2 delete ecosystem.config.cjs` (the update's own "pm2-stop" step) kills
+ // this server process — and its socket — well before update.sh reaches its
+ // 'restart' step. That step event, and 'portos:update:complete', are then
+ // never emitted, and the UI hangs on "Reconciling..."/"Stopping apps"
+ // forever even though update.sh finishes fine in the background. The
+ // socket disconnecting IS the server dying, so treat it as the same
+ // signal 'restart' would have been — it's a strictly more reliable proxy
+ // for "the update just tore down this process" than waiting on a step
+ // event from the process being torn down.
+ const handleDisconnect = () => {
+ if (!updatingRef.current) return;
+ armRestartPolling();
+ };
+
socket.on('portos:update:step', handleStep);
socket.on('portos:update:complete', handleComplete);
socket.on('portos:update:error', handleError);
+ socket.on('disconnect', handleDisconnect);
return () => {
socket.off('portos:update:step', handleStep);
socket.off('portos:update:complete', handleComplete);
socket.off('portos:update:error', handleError);
+ socket.off('disconnect', handleDisconnect);
};
}, []);
diff --git a/client/src/components/apps/tabs/UpdateTab.test.jsx b/client/src/components/apps/tabs/UpdateTab.test.jsx
new file mode 100644
index 0000000000..e00c91b91a
--- /dev/null
+++ b/client/src/components/apps/tabs/UpdateTab.test.jsx
@@ -0,0 +1,82 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+
+// ── Mock socket — capture registered handlers so the test can fire 'disconnect' ──
+const handlers = new Map();
+vi.mock('../../../services/socket', () => ({
+ default: {
+ on: (event, fn) => { handlers.set(event, fn); },
+ off: (event, fn) => { if (handlers.get(event) === fn) handlers.delete(event); },
+ },
+}));
+
+// ── Mock toast ────────────────────────────────────────────────────────────────
+const mockToast = vi.hoisted(() => Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn(), loading: vi.fn(), dismiss: vi.fn() }));
+vi.mock('../../ui/Toast', () => ({ default: mockToast }));
+
+// ── Mock API ──────────────────────────────────────────────────────────────────
+const mockGetUpdateStatus = vi.fn();
+const mockCheckHealth = vi.fn();
+const mockExecutePortosUpdate = vi.fn();
+vi.mock('../../../services/api', () => ({
+ getUpdateStatus: (...a) => mockGetUpdateStatus(...a),
+ checkHealth: (...a) => mockCheckHealth(...a),
+ executePortosUpdate: (...a) => mockExecutePortosUpdate(...a),
+ checkForUpdate: vi.fn(),
+ ignoreUpdateVersion: vi.fn(),
+ clearIgnoredVersions: vi.fn(),
+ syncPortosFork: vi.fn(),
+}));
+
+const UpdateTab = (await import('./UpdateTab')).default;
+
+const OUT_OF_SYNC_STATUS = {
+ currentVersion: '2.24.0',
+ installState: { outOfSync: true, runningStaleCode: true },
+};
+
+describe('UpdateTab reconcile flow', () => {
+ beforeEach(() => {
+ handlers.clear();
+ mockGetUpdateStatus.mockReset().mockResolvedValue(OUT_OF_SYNC_STATUS);
+ mockCheckHealth.mockReset().mockResolvedValue({ version: '2.24.0', uptime: 120 });
+ mockExecutePortosUpdate.mockReset().mockResolvedValue({ tag: 'v2.24.0' });
+ mockToast.mockClear();
+ mockToast.loading.mockClear();
+ });
+
+ it('arms the restart-polling fallback on socket disconnect, not just on the "restart" step', async () => {
+ render(<UpdateTab />);
+
+ const button = await screen.findByRole('button', { name: 'Reconcile Now' });
+ fireEvent.click(button);
+
+ // runUpdate flips to "Reconciling..." once the update starts.
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Reconciling...' })).toBeTruthy());
+ expect(mockExecutePortosUpdate).toHaveBeenCalledWith({ reconcile: true });
+
+ // The server process dies mid-update (pm2-stop kills it) before it ever
+ // emits a 'restart' step or 'portos:update:complete' — only the socket
+ // disconnecting tells the client the process is gone.
+ expect(handlers.has('disconnect')).toBe(true);
+ fireEvent.click(button); // no-op safety: button should already reflect updating state
+ handlers.get('disconnect')();
+
+ // The fallback must arm from 'disconnect' alone — the UI should not stay
+ // stuck showing "Reconciling..." forever waiting for a step event that
+ // will never arrive from the now-dead process.
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Restarting...' })).toBeTruthy());
+ });
+
+ it('ignores a disconnect that happens while no update is in progress', async () => {
+ render(<UpdateTab />);
+ await screen.findByRole('button', { name: 'Reconcile Now' });
+
+ expect(handlers.has('disconnect')).toBe(true);
+ handlers.get('disconnect')();
+
+ // No update was running, so disconnect must not fake-arm the restart flow.
+ expect(screen.getByRole('button', { name: 'Reconcile Now' })).toBeTruthy();
+ expect(mockToast.loading).not.toHaveBeenCalled();
+ });
+});