MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

[email protected] self-start viewer repeats under host lifecycle mismatch, causing port drift (18799→18800+), Memory unavailable

Open TelDragon opened this issue 7 months ago • 3 comments

Pre-submission checklist | 提交前检查

  • [x] I have searched existing issues and this hasn't been mentioned before | 我已搜索现有问题,确认此问题尚未被提及
  • [x] I have read the project documentation and confirmed this issue doesn't already exist | 我已阅读项目文档并确认此问题尚未存在
  • [x] This issue is specific to MemOS and not a general software issue | 该问题是针对 MemOS 的,而不是一般软件问题

Bug Description | 问题描述

Environment OpenClaw: 2026.4.5 Plugin: @memtensor/[email protected] OS: Windows 11 Node: 24.14.0 Config: plugins.entries.memos-local-openclaw-plugin.config.viewerPort = 18799 Current behavior Even with viewerPort fixed to 18799, Memory still becomes unavailable in status checks.

Observed logs repeatedly show:

memos-local: service.start() not called by host, self-starting viewer... then port fallback: Viewer port 18799 in use, trying 18800 Viewer port 18800 in use, trying 18801 ... openclaw status --deep often reports:

Memory: enabled (plugin memos-local-openclaw-plugin) · unavailable Reproduction (minimal) Install/update plugin to 1.0.8 Set fixed port: Restart gateway Run openclaw status --deep / openclaw plugins inspect ... multiple times Observe self-start logs and viewer moving to 18800+, while Memory may stay unavailable Expected behavior If viewerPort=18799, plugin should either: use exactly that port and fail clearly if occupied, or keep health as available when core memory service is healthy. Repeated plugin register/init in same process should not trigger repeated self-start attempts. status --deep should not degrade Memory health due to duplicate self-start side effects. Actual behavior service.start() is not always called by host path. Plugin fallback self-starting viewer... is triggered repeatedly. Viewer fallback consumes next ports (18800+). Memory health may show unavailable despite plugin DB/embedding being initialized. Root-cause hypothesis Lifecycle mismatch: plugin registerService occurs, but service.start is not guaranteed on all load paths. Fallback self-start is not globally idempotent across repeated register/init cycles. Health status likely coupled too tightly to viewer startup result. Local workaround/patch used I added a process-level singleton guard in index.ts to make startup idempotent per (pid,stateDir,viewerPort):

guard state on globalThis: starting started skip duplicate starts when already starting/started reset guard on stop() This reduced repeated same-process viewer starts and stabilized fixed port behavior.

Patch idea (pseudo) Copy const globalState = globalThis as Record<string, any>; const root = "memos_local_start_guard"; if (!globalState[root]) globalState[root] = {}; const key = ${process.pid}:${stateDir}:${viewerPort}; const singleton = (globalState[root][key] ||= { starting: false, started: false });

if (singleton.started) return; if (singleton.starting) return; singleton.starting = true; try { await viewer.start(); singleton.started = true; } finally { singleton.starting = false; } Suggested upstream fix Make service startup idempotent at plugin level across repeated register/init. Revisit host lifecycle integration to avoid “registered but start hook not fired” path. Decouple Memory health from viewer port fallback side-effects (or treat viewer as optional health dimension). If viewerPort is explicitly configured, consider strict mode (no auto-drift) + explicit warning. Extra notes This is not a ClawHub rate-limit issue (already bypassed with npm install/update). Not primarily embedding failure either; logs show embedding can initialize while health still unavailable.

How to Reproduce | 如何重现

1

Environment | 环境信息

1

Additional Context | 其他信息

1

Willingness to Implement | 实现意愿

  • [ ] I'm willing to implement this myself | 我愿意自己解决
  • [ ] I would like someone else to implement this | 我希望其他人来解决

TelDragon avatar Apr 07 '26 04:04 TelDragon

Confirmed on another environment as well, but with an important extra finding from Linux + systemd.

Symptom | 症状

We saw the same repeated pattern:

memos-local: service.start() not called by host, self-starting viewer...
memos-local: started (embedding: bailian)

In earlier runs this also correlated with delayed initialization and repeated viewer self-start attempts.

在另一台 Linux + systemd 机器上也复现了同样的模式:

memos-local: service.start() not called by host, self-starting viewer...

更早的几轮里,它还伴随初始化明显延后,以及 viewer 重复自启动。

Important Finding | 关键补充发现

The plugin-level symptom is real, but in this environment the root cause was not only inside the plugin.

A host-side lifecycle gap in OpenClaw meant the plugin service could be registered while service.start() was never fired promptly by the host. That made the plugin fall back to self-starting the viewer.

也就是说,这个现象表面看像插件自己重复自启动,但在这台机器上更深一层的根因是 OpenClaw host 生命周期链路里,插件 service 已注册但 service.start() 没有及时被宿主调用,于是插件才走了 self-start viewer 的兜底路径。

Minimal Reproduction | 最小复现

  • OpenClaw 2026.4.8
  • @memtensor/[email protected]
  • Linux + systemctl --user
  • restart gateway and watch logs around ready/init/start

Typical sequence before the host fix:

  1. gateway becomes ready
  2. memos-local is still not started by host
  3. plugin logs service.start() not called by host, self-starting viewer...
  4. viewer comes up through fallback path instead of normal host lifecycle

Local Mitigation | 本地缓解办法

What actually stabilized the system was a two-layer fix:

  1. Host-side:
  • adjust startup ordering so plugin services are started in normal sidecar startup
  • add a reconcile pass for registered-but-not-started services
  1. Plugin-side:
  • keep a singleton/idempotency guard so repeated init/start attempts in the same process do not re-start viewer again

真正让系统稳定下来的,是“两层同时修”:

  1. 宿主层:
  • 调整启动顺序,让 plugin service 在正常 sidecar 阶段被启动
  • 增加 registered-but-not-started service 的 reconcile
  1. 插件层:
  • 增加单例 / 幂等保护,避免同进程重复 init/start 时再次拉起 viewer

After the host-side fix, the service.start() not called by host log disappeared on this machine.

Suggested Upstream Direction | 建议修复方向

I think this issue is worth treating as a combined lifecycle problem:

  • plugin startup should remain idempotent
  • but the host/plugin lifecycle contract should also be hardened so registerService() is consistently followed by service.start() on late-load paths

我建议上游把它当成“插件幂等 + 宿主生命周期契约”两个层面一起看:

  • 插件启动继续保持幂等
  • 同时 host/plugin 生命周期要补强,保证 late-load 场景下 registerService() 之后 service.start() 一定能落到

starm0on avatar Apr 09 '26 16:04 starm0on

This issue has been fixed in the latest version of the plugin. The duplicate Viewer startup and port conflict problems were resolved by introducing a global singleton state to manage the initialization lifecycle. Please update to the latest release!

Hun-ger avatar Apr 17 '26 09:04 Hun-ger

Hey @TelDragon! Thanks for the contribution — already solved the issue, thanks for helping us to hunt the bug! 🐞

Btw if you enjoyed contributing and want to stay connected with the project, we run a Discord server for contributors, a chill space to discuss features, ask questions, or pick up new issues  👉🏻 MemOS Discord Server (Contributor Exclusive)

Thanks again for shipping this, and happy MemOSing 🧠✨

angarchie avatar May 13 '26 12:05 angarchie