openclaw - 💡(How to fix) Fix Gateway startup extremely slow (70-100s), causing TUI timeouts and spontaneous restarts [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#68781Fetched 2026-04-19 15:07:36
View on GitHub
Comments
1
Participants
2
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
commented ×1

Error Message

history failed: Error: gateway request timeout for chat.history sessions list failed: Error: gateway request timeout for sessions.list gateway disconnected: closed | idle

Root Cause

Root Cause Analysis

Code Example

history failed: Error: gateway request timeout for chat.history
   sessions list failed: Error: gateway request timeout for sessions.list
   gateway disconnected: closed | idle

---

activateRuntimeSecrets(activate=true)
→ prepareSecretsRuntimeSnapshot
collectCandidateAgentDirs (9 dirs)
for dir in dirs:
loadAuthStore(dir)  ← first call takes ~58s
      → loadAuthProfileStoreForRuntime
        → loadAuthProfileStoreForAgent
          → overlayExternalAuthProfiles  ← triggers plugin loading!
            → resolveExternalAuthProfilesWithPlugins
              → resolveProviderPluginsForHooks → jiti compiles all plugins
RAW_BUFFERClick to expand / collapse

Problem

Users report three interrelated issues:

  1. Extremely slow startup: openclaw gateway start takes 70-100+ seconds before the gateway is usable, causing the TUI to timeout
  2. TUI connection timeouts:
    history failed: Error: gateway request timeout for chat.history
    sessions list failed: Error: gateway request timeout for sessions.list
    gateway disconnected: closed | idle
  3. Spontaneous restarts: The gateway disconnects during operation, TUI reports closed (1000) / idle, requiring manual gateway restart

Root Cause Analysis

Startup Profiling Timeline

PhaseDurationCumulativeDescription
loadGatewayStartupConfigSnapshot~5s~5sRead config files
activateRuntimeSecrets (preflight)~58s~63sLargest bottleneck
ensureGatewayStartupAuth~0ms~63sNegligible
activateRuntimeSecrets (activate=true)~0.5s~64sFast with cache
prepareGatewayStartupConfig total~64s~64s
loadGatewayStartupPlugins~27s~91sjiti compilation of 5 plugins
createGatewayRuntimeState~0ms~91sNegligible
startGatewayPostAttachRuntime~3s~94sChannel startup
Total~94s

Root Cause Chain (src/agents/auth-profiles/store.ts)

activateRuntimeSecrets(activate=true)
→ prepareSecretsRuntimeSnapshot
  → collectCandidateAgentDirs (9 dirs)
  → for dir in dirs:
    → loadAuthStore(dir)  ← first call takes ~58s
      → loadAuthProfileStoreForRuntime
        → loadAuthProfileStoreForAgent
          → overlayExternalAuthProfiles  ← triggers plugin loading!
            → resolveExternalAuthProfilesWithPlugins
              → resolveProviderPluginsForHooks → jiti compiles all plugins

The core issue: during secrets runtime snapshot preparation, the plugin cache has not been established, so all plugins get jiti-compiled (~58s). The second call hits the plugin cache and only takes 8-13ms per loadAuthStore.

TUI timeout: The TUI defaults to a 30s request timeout (client.ts:207), but the gateway spends 70-100s initializing before it can respond.

Spontaneous restarts: The gateway is unresponsive during initialization → process managers think it is dead → auto-restart → TUI connection drops.

Fixes Already Implemented

Our branch perf/startup-latency-fixes addresses some of these issues:

  • src/agents/auth-profiles/store.ts — skip overlayExternalAuthProfiles (auth store 60s → 3.7s)
  • src/gateway/server-discovery-runtime.ts — disable bonjour on Windows (eliminates memory leak)
  • src/gateway/server-startup-plugins.ts — parallelize startup tasks (~5s saved)
  • src/agents/pi-embedded-runner/run/attempt-http-runtime.ts — LLM timeout cap
  • src/agents/pi-embedded-runner/run/llm-idle-timeout.ts — idle timeout cap
  • src/infra/json-files.ts — async lock 30s timeout

Total startup reduced from ~100s to ~73s, but 73s is still too slow and the TUI 30s timeout is still not covered.

Suggested Further Fixes

  1. Defer plugin loading: Push overlayExternalAuthProfiles to after the gateway is fully up (lazy loading)
  2. Plugin cache warm-up: Establish a global plugin cache before loadGatewayStartupPlugins
  3. Dynamic TUI handshake timeout: Return an initializing status code instead of hanging while the gateway boots
  4. Progressive startup: Start core channels first so they respond to TUI, then load non-critical plugins asynchronously
  5. Health check endpoint: Add GET /health returning initializing / ready / degraded so TUI and process managers can decide whether to restart
  6. Stability-first focus: We strongly recommend the team establish a "stability milestone" — pause non-urgent new features and focus on startup performance, connection stability, and plugin loading reliability

Related Files

  • src/agents/auth-profiles/store.ts
  • src/agents/auth-profiles/external-auth.ts
  • src/gateway/server-startup-post-attach.ts
  • src/gateway/server-startup-plugins.ts
  • src/gateway/server-discovery-runtime.ts
  • src/infra/json-files.ts
  • src/agents/pi-embedded-runner/run/attempt-http-runtime.ts
  • src/agents/pi-embedded-runner/run/llm-idle-timeout.ts

extent analysis

TL;DR

Implementing a combination of deferred plugin loading, plugin cache warm-up, and dynamic TUI handshake timeout can significantly reduce the gateway's startup time and improve overall stability.

Guidance

  1. Defer plugin loading: Modify src/agents/auth-profiles/store.ts to push overlayExternalAuthProfiles to after the gateway is fully up, allowing for lazy loading of plugins and reducing initial startup time.
  2. Establish a global plugin cache: Before loadGatewayStartupPlugins, create a global plugin cache to avoid jiti compilation of all plugins during the first loadAuthStore call, which currently takes ~58s.
  3. Implement dynamic TUI handshake timeout: Instead of hanging during gateway boot, return an initializing status code to the TUI, allowing it to wait for the gateway to become fully operational before attempting to connect.
  4. Review and apply other suggested fixes: Consider implementing progressive startup, adding a health check endpoint, and focusing on stability-first development to further improve the gateway's performance and reliability.
  5. Monitor and adjust: After implementing these fixes, closely monitor the gateway's startup time and TUI connection stability, making adjustments as necessary to ensure optimal performance.

Example

// src/agents/auth-profiles/store.ts
async function loadAuthStore(dir: string) {
  // ... existing code ...
  // Defer overlayExternalAuthProfiles to after gateway initialization
  if (gatewayIsInitializing) {
    await gatewayInitializedPromise;
  }
  // ... existing code ...
  await overlayExternalAuthProfiles();
}

Notes

The provided fixes and suggestions are based on the detailed analysis of the issue and may require additional modifications to fully resolve the problem. It's essential to thoroughly test each change to ensure they do not introduce new issues.

Recommendation

Apply the suggested fixes, starting with deferred plugin loading and plugin cache warm-up, to reduce the gateway's startup time and improve overall stability.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING