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(apps): handle PortOS self-restart gracefully with recovery polling

Adam Eivybf5476e6e843
diff --git a/.changelog/v1.21.x.md b/.changelog/v1.21.x.md
index 2ef0421bf9..e1d67cc6e8 100644
--- a/.changelog/v1.21.x.md
+++ b/.changelog/v1.21.x.md
@@ -11,3 +11,7 @@ GSD tab one-click agent spawning and Claude Code launcher.
- **GSD one-click agent spawn** — each setup step now has a "Run" button that creates a CoS agent task to execute the GSD command, eliminating the need to manually open a terminal
- **Open Claude Code button** — new button in the GSD setup guide header launches Claude Code directly in the app's directory for interactive use
- **`POST /api/apps/:id/open-claude` endpoint** — spawns Claude Code as a detached process in the app's repo path
+
+## Fixes
+
+- **PortOS self-restart no longer errors** — restarting PortOS from the dashboard now shows a "Restarting PortOS..." loading toast, polls for server recovery, and auto-reloads the page when ready (previously showed an error because the server died before responding)
diff --git a/client/src/components/AppTile.jsx b/client/src/components/AppTile.jsx
index 39c8d7d59e..b7c1628392 100644
--- a/client/src/components/AppTile.jsx
+++ b/client/src/components/AppTile.jsx
@@ -27,7 +27,13 @@ const AppTile = memo(function AppTile({ app, onUpdate }) {
restart: api.restartApp
}[action];
- await actionFn(app.id);
+ const result = await actionFn(app.id);
+
+ if (result?.selfRestart) {
+ api.handleSelfRestart();
+ return;
+ }
+
setLoading(null);
onUpdate?.();
};
diff --git a/client/src/components/apps/AppDetailView.jsx b/client/src/components/apps/AppDetailView.jsx
index 86083a0909..e0173a576f 100644
--- a/client/src/components/apps/AppDetailView.jsx
+++ b/client/src/components/apps/AppDetailView.jsx
@@ -59,7 +59,11 @@ export default function AppDetailView() {
const handleRestart = async () => {
setActionLoading('restart');
- await api.restartApp(appId).catch(() => null);
+ const result = await api.restartApp(appId).catch(() => null);
+ if (result?.selfRestart) {
+ api.handleSelfRestart();
+ return;
+ }
setActionLoading(null);
};
diff --git a/client/src/pages/Apps.jsx b/client/src/pages/Apps.jsx
index a8e4f9258f..0112f4679e 100644
--- a/client/src/pages/Apps.jsx
+++ b/client/src/pages/Apps.jsx
@@ -133,7 +133,11 @@ export default function Apps() {
const handleRestart = async (app) => {
setActionLoading(prev => ({ ...prev, [app.id]: 'restart' }));
- await api.restartApp(app.id).catch(() => null);
+ const result = await api.restartApp(app.id).catch(() => null);
+ if (result?.selfRestart) {
+ api.handleSelfRestart();
+ return;
+ }
setActionLoading(prev => ({ ...prev, [app.id]: null }));
};
diff --git a/client/src/services/api.js b/client/src/services/api.js
index 8707603f67..712809d5f9 100644
--- a/client/src/services/api.js
+++ b/client/src/services/api.js
@@ -108,6 +108,27 @@ export const deleteApp = (id) => request(`/apps/${id}`, { method: 'DELETE' });
export const startApp = (id) => request(`/apps/${id}/start`, { method: 'POST' });
export const stopApp = (id) => request(`/apps/${id}/stop`, { method: 'POST' });
export const restartApp = (id) => request(`/apps/${id}/restart`, { method: 'POST' });
+
+/**
+ * Handle PortOS self-restart: show a loading toast, poll for server recovery, then reload.
+ * Call this after restartApp() returns { selfRestart: true }.
+ */
+export function handleSelfRestart() {
+ toast.loading('Restarting PortOS...', { id: 'self-restart', duration: Infinity });
+ const poll = async () => {
+ for (let i = 0; i < 30; i++) {
+ await new Promise(r => setTimeout(r, 2000));
+ const ok = await fetch(`${API_BASE}/system/health`).then(() => true).catch(() => false);
+ if (ok) {
+ toast.success('PortOS restarted successfully', { id: 'self-restart' });
+ setTimeout(() => window.location.reload(), 1000);
+ return;
+ }
+ }
+ toast.error('PortOS restart timed out — try reloading manually', { id: 'self-restart' });
+ };
+ poll();
+}
export const archiveApp = (id) => request(`/apps/${id}/archive`, { method: 'POST' });
export const unarchiveApp = (id) => request(`/apps/${id}/unarchive`, { method: 'POST' });
export const openAppInEditor = (id) => request(`/apps/${id}/open-editor`, { method: 'POST' });
diff --git a/server/routes/apps.js b/server/routes/apps.js
index b04f8a3f47..07bac1e483 100644
--- a/server/routes/apps.js
+++ b/server/routes/apps.js
@@ -308,6 +308,22 @@ router.post('/:id/stop', loadApp, asyncHandler(async (req, res) => {
// POST /api/apps/:id/restart - Restart app
router.post('/:id/restart', loadApp, asyncHandler(async (req, res) => {
const app = req.loadedApp;
+
+ // Self-restart: respond first, then restart after a delay so the response reaches the client
+ if (app.id === PORTOS_APP_ID) {
+ await logAction('restart', app.id, app.name, { processNames: app.pm2ProcessNames }, true);
+ notifyAppsChanged('restart');
+ res.json({ success: true, selfRestart: true });
+ setTimeout(async () => {
+ console.log('🔄 Self-restart: restarting PortOS processes');
+ for (const name of app.pm2ProcessNames || []) {
+ await pm2Service.restartApp(name, app.pm2Home)
+ .catch(err => console.error(`❌ Self-restart failed for ${name}: ${err.message}`));
+ }
+ }, 500);
+ return;
+ }
+
const results = {};
for (const name of app.pm2ProcessNames || []) {