BigPizzaV3/CodexPlusPlus · error

app-server 未连接

Error message

app-server 未连接

What it means

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.

Solutions

  1. Always call rpc() (which awaits ensureAppServer()) rather than rpcRaw() directly from UI code.
  2. On this error, reset appServerSessionId and re-run ensureAppServer() before retrying.
  3. Add heartbeat/session-validation so a dead session is detected and re-established proactively.
  4. Check relay server logs for app-server spawn/exit failures.

Example fix

// before
const result = await rpcRaw("thread/start", {});
// after
if (!appServerSessionId) await ensureAppServer();
const result = await rpc("thread/start", {});
Defensive patterns

Strategy: retry

Validate before calling

if (!appServerSessionId) await ensureAppServer();

Type guard

function hasAppServerSession() { return typeof appServerSessionId === "string" && appServerSessionId.length > 0; }

Try / catch

try { return await rpc(method, params); } catch (e) { if (e.message.includes("app-server 未连接")) { appServerSessionId = null; await ensureAppServer(); return rpc(method, params); } throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/55c8a06e69a2d064. Report an issue: GitHub.

Appendix: source

Thrown at apps/codex-plus-mobile-relay/src/main.rs:885

async function ensureAppServer() {
  if (appServerConnected && appServerSessionId) return;
  appServerSessionId = appServerSessionId || `mobile-${Date.now()}-${Math.random().toString(16).slice(2)}`;
  const id = `${Date.now()}-${requestId++}`;
  const packet = await encrypt({ type: "appServerConnect", id, sessionId: appServerSessionId });
  socket.send(JSON.stringify(packet));
  await new Promise((resolve, reject) => {
    pending.set(id, { resolve, reject });
    setTimeout(() => { if (pending.delete(id)) reject(new Error("app-server 连接超时")); }, 30000);
  });
  await rpcRaw("initialize", { clientInfo: { name: "Codex++ Mobile Relay", version: "1.0.0" }, capabilities: { experimentalApi: true } });
}
async function rpc(method, params = {}) {
  await ensureAppServer();
  return await rpcRaw(method, params);
}
async function rpcRaw(method, params = {}) {
  if (!socket || socket.readyState !== WebSocket.OPEN) throw new Error("未连接");
  if (!appServerSessionId) throw new Error("app-server 未连接");
  const id = rpcId++;
  const payload = { jsonrpc: "2.0", id, method, params };
  const packet = await encrypt({ type: "appServerMessage", sessionId: appServerSessionId, message: JSON.stringify(payload) });
  const promise = new Promise((resolve, reject) => {
    pendingRpc.set(String(id), { resolve, reject });
    setTimeout(() => { if (pendingRpc.delete(String(id))) reject(new Error(`${method} 超时`)); }, method === "turn/start" ? 60000 : 30000);
  });
  socket.send(JSON.stringify(packet));
  return promise;
}
function handleAppServerMessage(text) {
  let message;
  try { message = JSON.parse(text); } catch { return; }
  if (message.id != null) {
    const resolver = pendingRpc.get(String(message.id));
    if (!resolver) return;
    pendingRpc.delete(String(message.id));
    if (message.error) resolver.reject(new Error(message.error.message || "请求失败"));

View on GitHub (pinned to b1ed92e5e4)