Skip to content

fix(viewer): singleton guard prevents port drift on reactivation#1453

Open
kagura-agent wants to merge 1 commit intoMemTensor:mainfrom
kagura-agent:fix/viewer-self-start-singleton
Open

fix(viewer): singleton guard prevents port drift on reactivation#1453
kagura-agent wants to merge 1 commit intoMemTensor:mainfrom
kagura-agent:fix/viewer-self-start-singleton

Conversation

@kagura-agent
Copy link
Copy Markdown

Summary

Fixes #1430 — viewer self-starts repeatedly under host lifecycle mismatch, causing port drift (18799→18800+) and "Memory unavailable".

Root Cause

When the gateway restarts or reloads, register() is called again. Each call creates a new ViewerServer instance, but the previous one may still hold the port. The new instance hits EADDRINUSE and falls back to port+1, eventually drifting away from the expected 18799.

Fix

  • Module-level singleton: Track the active viewer, hub server, and store instances outside register() scope
  • Cleanup on re-registration: stop() the previous viewer/hub and close() the previous store before creating new ones
  • Reliable stop: ViewerServer.stop() now returns a Promise<void> that resolves only after server.close() callback fires, ensuring the port is fully released

Scope

This PR addresses the plugin side of the lifecycle mismatch. The host-side gap (deferred service.start() call path) would need a separate OpenClaw upstream fix.

Testing

  • npm run build passes

When the host gateway restarts and re-calls activate(), the previous
ViewerServer instance may still hold the port. Without cleanup, the new
instance hits EADDRINUSE and drifts to 18800+, causing Memory unavailable.

- Add module-level singleton tracking for ViewerServer
- Stop previous viewer instance before creating new one in activate()
- Ensure server.stop() fully releases the HTTP port

Fixes MemTensor#1430
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

memos-local-openclaw-plugin@1.0.8 self-start viewer repeats under host lifecycle mismatch, causing port drift (18799→18800+), Memory unavailable

1 participant