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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-12602]): concurrent requests to an idle local runtime now share one wake instead of starting it twice
[._.]/ Adam Eivy2d7fce068940
diff --git a/server/lib/managedDaemon.js b/server/lib/managedDaemon.jsindex 6333fe53d1..3e5bb0dacf 100644--- a/server/lib/managedDaemon.js+++ b/server/lib/managedDaemon.js@@ -344,10 +344,24 @@ export function createOnDemandDaemon({* Resolves `{ ready, reason }` rather than throwing: a caller in front of an* inference request wants to report "could not be started" alongside its own* error, not have a lazy start unwind its stack.+ *+ * Single-flight: concurrent callers (two CoS agents, an agent plus a voice+ * turn) share one wake. Without it each saw the daemon offline and called+ * `start`, whose `pm2 delete` killed the sibling launch, so both failed with+ * "exited while loading". Joining is safe because the launch line comes from+ * live/saved state, not from the caller. The slot clears on settle, so a later+ * wake after another stop starts the daemon again.*/- const ensureRunning = async () => {+ let wakeInFlight = null;++ const ensureRunning = () => {markUsed();+ if (wakeInFlight) return wakeInFlight;+ wakeInFlight = wake().finally(() => { wakeInFlight = null; });+ return wakeInFlight;+ };+ const wake = async () => {const pm2Status = await getStatusStrict();if (pm2Status?.status === 'online') return { ready: true, reason: null };diff --git a/server/lib/managedDaemon.test.js b/server/lib/managedDaemon.test.jsindex cb433af0bf..549e8cdce3 100644--- a/server/lib/managedDaemon.test.js+++ b/server/lib/managedDaemon.test.js@@ -170,6 +170,28 @@ describe('createOnDemandDaemon', () => {expect(probe).toHaveBeenCalledWith('http://127.0.0.1:9200/v1');});+ // A second concurrent wake used to call `start` too; its `pm2 delete` killed+ // the first launch and both callers failed with "exited while loading".+ it('shares one start between concurrent wakes and starts again after the wake settles', async () => {+ let finish;+ const start = vi.fn(() => new Promise((resolve) => { finish = resolve; }));+ const { onDemand } = makeOnDemand({ start });++ const first = onDemand.ensureRunning();+ const second = onDemand.ensureRunning();+ await vi.waitFor(() => expect(start).toHaveBeenCalledTimes(1));+ finish({ online: false, error: new Error('port bind failed') });++ expect(await first).toEqual({ ready: false, reason: 'port bind failed' });+ expect(await second).toEqual({ ready: false, reason: 'port bind failed' });+ expect(start).toHaveBeenCalledTimes(1);++ const third = onDemand.ensureRunning();+ await vi.waitFor(() => expect(start).toHaveBeenCalledTimes(2));+ finish({ online: true });+ expect(await third).toEqual({ ready: true, reason: null });+ });+it('waitForReady reports the shared exit-tail diagnosis when the process dies before answering', async () => {const { onDemand, setStatus } = makeOnDemand({start: vi.fn(async () => ({ online: false, endpoint: 'http://127.0.0.1:9000/v1' })),diff --git a/server/services/ollamaManager.js b/server/services/ollamaManager.jsindex 58652eb611..dcbf4b7cb7 100644--- a/server/services/ollamaManager.js+++ b/server/services/ollamaManager.js@@ -98,6 +98,8 @@ let lastInstalledModelsError = nulllet lastCheckAt = nulllet _managedProcess = nulllet managedProcessPid = null+// `{ signature, promise }` of the `startServer` spawn currently in flight.+let startInFlight = null// What PortOS knows about the Ollama daemon that is up RIGHT NOW. Every field// describes ONE live process, so the whole record is written by `recordDaemon`// and dropped by `forgetDaemon` — "which env" and "which process" can never@@ -440,10 +442,31 @@ async function terminateManagedProcess() {* second, context-only parameter: two spellings of one variable would need a* precedence rule, and the latches below would have to read both.*+ * Single-flight per launch env: concurrent callers asking for the same env+ * share one spawn and one result. Two racing `ollama serve` children left the+ * loser to fail its bind and exit, and its exit cleared `managedProcessPid`,+ * leaving the surviving daemon untracked and unstoppable on Windows. A caller+ * asking for a DIFFERENT env waits for the in-flight start, then runs its own,+ * which sees the daemon up and reports `alreadyRunning` exactly as a+ * sequential call would, instead of claiming a start under an env it never got.+ ** @param {{ env?: Record<string, string> }} [options]* @returns {Promise<{ success: boolean, running?: boolean, alreadyRunning?: boolean, pid?: number, error?: string }>}*/async function startServer({ env = null } = {}) {+ const signature = envSignature(env)+ while (startInFlight) {+ if (startInFlight.signature === signature) return startInFlight.promise+ await startInFlight.promise.catch(() => {})+ }+ const promise = spawnServer(env).finally(() => {+ if (startInFlight?.promise === promise) startInFlight = null+ })+ startInFlight = { signature, promise }+ return promise+}++async function spawnServer(env) {if (await checkOllamaAvailable(true)) {return { success: true, running: true, alreadyRunning: true }}diff --git a/server/services/ollamaManager.test.js b/server/services/ollamaManager.test.jsindex 30eb130a6e..d9967bab60 100644--- a/server/services/ollamaManager.test.js+++ b/server/services/ollamaManager.test.js@@ -151,6 +151,38 @@ describe('ollamaManager startup failures', () => {})})+// Two racing `ollama serve` children left the loser's exit clearing the+// tracked pid, so the surviving daemon became unstoppable on Windows.+describe('ollamaManager concurrent starts', () => {+ afterEach(() => vi.unstubAllGlobals())++ it('shares one spawn for the same env, and a different env reports the running daemon instead of a start', async () => {+ const state = { up: false }+ vi.stubGlobal('fetch', vi.fn(async () => {+ if (!state.up) throw new Error('ECONNREFUSED')+ const body = { version: '0.32.13' }+ return { ok: true, status: 200, json: async () => body, text: async () => JSON.stringify(body) }+ }))+ const { spawn } = await import('../lib/childProcess.js')+ spawn.mockReset().mockImplementation(() => {+ state.up = true+ return { pid: 4242, stderr: { on: () => {} }, on: () => {}, unref: () => {} }+ })+ const { startServer } = await loadManager()++ const [a, b, other] = await Promise.all([+ startServer(),+ startServer({ env: {} }),+ startServer({ env: { OLLAMA_FLASH_ATTENTION: '1' } }),+ ])++ expect(spawn).toHaveBeenCalledTimes(1)+ expect(a).toEqual({ success: true, running: true, pid: 4242 })+ expect(b).toBe(a)+ expect(other).toEqual({ success: true, running: true, alreadyRunning: true })+ })+})+describe('ollamaManager model capability sentinel', () => {afterEach(() => vi.unstubAllGlobals())