{"record":{"id":"55c8a06e69a2d064","repo":"BigPizzaV3/CodexPlusPlus","slug":"app-server","errorCode":null,"errorMessage":"app-server 未连接","messagePattern":"app-server 未连接","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/codex-plus-mobile-relay/src/main.rs","lineNumber":885,"sourceCode":"async function ensureAppServer() {\n  if (appServerConnected && appServerSessionId) return;\n  appServerSessionId = appServerSessionId || `mobile-${Date.now()}-${Math.random().toString(16).slice(2)}`;\n  const id = `${Date.now()}-${requestId++}`;\n  const packet = await encrypt({ type: \"appServerConnect\", id, sessionId: appServerSessionId });\n  socket.send(JSON.stringify(packet));\n  await new Promise((resolve, reject) => {\n    pending.set(id, { resolve, reject });\n    setTimeout(() => { if (pending.delete(id)) reject(new Error(\"app-server 连接超时\")); }, 30000);\n  });\n  await rpcRaw(\"initialize\", { clientInfo: { name: \"Codex++ Mobile Relay\", version: \"1.0.0\" }, capabilities: { experimentalApi: true } });\n}\nasync function rpc(method, params = {}) {\n  await ensureAppServer();\n  return await rpcRaw(method, params);\n}\nasync function rpcRaw(method, params = {}) {\n  if (!socket || socket.readyState !== WebSocket.OPEN) throw new Error(\"未连接\");\n  if (!appServerSessionId) throw new Error(\"app-server 未连接\");\n  const id = rpcId++;\n  const payload = { jsonrpc: \"2.0\", id, method, params };\n  const packet = await encrypt({ type: \"appServerMessage\", sessionId: appServerSessionId, message: JSON.stringify(payload) });\n  const promise = new Promise((resolve, reject) => {\n    pendingRpc.set(String(id), { resolve, reject });\n    setTimeout(() => { if (pendingRpc.delete(String(id))) reject(new Error(`${method} 超时`)); }, method === \"turn/start\" ? 60000 : 30000);\n  });\n  socket.send(JSON.stringify(packet));\n  return promise;\n}\nfunction handleAppServerMessage(text) {\n  let message;\n  try { message = JSON.parse(text); } catch { return; }\n  if (message.id != null) {\n    const resolver = pendingRpc.get(String(message.id));\n    if (!resolver) return;\n    pendingRpc.delete(String(message.id));\n    if (message.error) resolver.reject(new Error(message.error.message || \"请求失败\"));","sourceCodeStart":867,"sourceCodeEnd":903,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/apps/codex-plus-mobile-relay/src/main.rs#L867-L903","documentation":"rpcRaw() forwards JSON-RPC messages to the codex app-server through an established tunnel session identified by appServerSessionId. If the app-server handshake (ensureAppServer) hasn't produced a session id — handshake still in flight, failed, or the session was closed — sending is rejected with this error even though the relay WebSocket itself is open.","triggerScenarios":"Calling rpc(method, params) when the WebSocket is OPEN but `appServerSessionId` is falsy: ensureAppServer() not awaited/failed, app-server process exited, session invalidated server-side, or a second tab never completed the handshake.","commonSituations":"Relay restarted and lost the app-server session while the browser kept its socket; app-server crashed; user interacted before the initialize handshake completed; stale page kept open overnight after server-side session cleanup.","solutions":["Always call rpc() (which awaits ensureAppServer()) rather than rpcRaw() directly from UI code.","On this error, reset appServerSessionId and re-run ensureAppServer() before retrying.","Add heartbeat/session-validation so a dead session is detected and re-established proactively.","Check relay server logs for app-server spawn/exit failures."],"exampleFix":"// before\nconst result = await rpcRaw(\"thread/start\", {});\n// after\nif (!appServerSessionId) await ensureAppServer();\nconst result = await rpc(\"thread/start\", {});","handlingStrategy":"retry","validationCode":"if (!appServerSessionId) await ensureAppServer();","typeGuard":"function hasAppServerSession() { return typeof appServerSessionId === \"string\" && appServerSessionId.length > 0; }","tryCatchPattern":"try { return await rpc(method, params); } catch (e) { if (e.message.includes(\"app-server 未连接\")) { appServerSessionId = null; await ensureAppServer(); return rpc(method, params); } throw e; }","preventionTips":["Never call rpcRaw() directly from UI code.","Invalidate appServerSessionId whenever the relay restarts or a session error appears.","Add a heartbeat that verifies the app-server session and re-handshakes when stale."],"tags":["websocket","jsonrpc","session","app-server"],"backgroundTag":"connection-refused","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}