Skip to content

fix(runtime-host): reap stale Runtime Host control directories #4712

Open

Description

@me2seeks

What happened

Every State Root ever opened leaves a permanent directory under the Runtime Host control namespace — ~/.cache/maka/runtime-hosts/<rootId>/ on Linux (see resolveRootControlNamespace in packages/storage/src/root-authority.ts) — containing only a 0-byte owner.lock. There is no reaping path anywhere in the codebase: the namespace is created with mkdir and locked, but never cleaned up.

On one long-lived machine this has accumulated to 91,026 directories (first seen 2026-07-20, still growing as of 2026-09-04). The bytes are negligible (~908 KB total), but the accumulation is unbounded and the directory itself becomes hostile to tooling — a plain glob over it fails with Argument list too long, and backup/indexing tools scan 91k dentries. Normal (non-development) usage accumulates one entry per State Root ever opened, forever.

Expected: stale control directories are reaped. The design already makes this safe — the durable State Root owner lock lives separately under state-root-owners/, and the code comment states the cache directory is disposable ("deleting a cache directory while a Host is running must never make the same State Root acquirable again", preserved by the durable lock). A reaper can e.g. attempt to acquire a directory's owner.lock on Host startup: if acquirable, no live Host holds it, and the directory can be removed after a grace period.

How to reproduce

  1. Run Maka (or its test suites) for some time; each new State Root mints one entry.
  2. ls ~/.cache/maka/runtime-hosts | wc -l — count grows monotonically.
  3. Inspect any entry: it contains only an empty owner.lock.

Environment

  • Maka version or commit: source checkout b39e8d36ef; observed installation tracks recent main
  • OS and version: Ubuntu 26.04 LTS, x86_64
  • Surface: Runtime Host
  • Node.js version, if running from source: v26.3.0

Logs, screenshots, or additional context

$ ls -f ~/.cache/maka/runtime-hosts | wc -l
91026
$ du -sh ~/.cache/maka/runtime-hosts
908K
$ ls ~/.cache/maka/runtime-hosts/<any-entry>
owner.lock   # 0 bytes

Note: development machines mint State Roots much faster (every test run / temporary workspace creates one), so the slope there is steeper than for typical users — but there is no cleanup for either.

Activity

  1. added
    bug
    on Sep 4, 2026
  2. Yx01-me commented on Sep 4, 2026

    @Yx01-me
    Contributor

    take

  3. Yx01-me commented on Sep 13, 2026

    @Yx01-me
    Contributor
    Image

    Proposed Solution: Runtime Host Control Directory Reaping

    I have completed a working implementation for reclaiming stale Runtime Host control directories.

    Design

    The cleanup process consists of three stages:

    1. Lock validation

      A directory is eligible for reclamation only when both owner.lock and .maka-artifact-writer.lock can be acquired exclusively.

      This protects active Runtime Hosts, Root Readers, and Artifact Writers from accidental deletion.

    2. Atomic quarantine

      Instead of deleting runtime-hosts/<rootId> directly, the directory is atomically renamed to:

      runtime-hosts/.reap/<claimUuid>/<rootId>
      

      Cleanup only operates on this quarantined path. If another Runtime Host concurrently recreates the original <rootId> path, the newly created directory cannot be deleted by the previous cleanup operation.

    3. Background reaping

      A normal Runtime Host shutdown attempts immediate cleanup.

      Directories left behind by crashes are reclaimed by a later Runtime Host after that Host has already reached the ready state. The scan is streaming and bounded to 1,024 entries or 500 ms per batch, whichever comes first.

    Safety Guarantees

    • A normal Runtime Host shutdown does not leave its <rootId> directory behind when cleanup is supported.
    • Active Runtime Hosts, Root Readers, and Artifact Writers are protected by the two-lock protocol.
    • The original <rootId> path is never recursively deleted.
    • Symlinks, invalid root IDs, unstable directory identities, and lock-file identity changes are rejected.
    • Cleanup failures are best-effort and never cause Runtime Host shutdown to fail.
    • Tombstones left by a crash between rename and deletion are reclaimed by a later sweep.
    • The existing state-root-owners/<rootId>.lock lifecycle and leader-election model remain unchanged.

    Readiness and Scale

    The reaper starts asynchronously only after the Runtime Host has reached the ready state. Therefore, a large number of stale directories does not delay Host startup.

    In a WSL/ext4 test containing 100,000 directories:

    • Runtime Host ready latency was approximately 18 ms.
    • The background sweep completed independently in approximately 16.36 seconds.
    • Directory traversal used streaming iteration rather than constructing a 100,000-entry array or launching 100,000 concurrent promises.

    Validation

    The Linux/WSL implementation has been validated for:

    • Normal shutdown cleanup
    • Active Host, Reader, and Writer protection
    • Cross-process reaper contention
    • Recovery after SIGKILL following quarantine
    • Concurrent recreation of the original <rootId> path
    • Symlink and invalid-path protection
    • Real 100,000-directory readiness behavior

    Windows Behavior

    Native Windows may reject renaming a directory while lock-file handles inside it remain open. In that situation, the implementation safely skips reclamation instead of falling back to a close-then-delete sequence that could introduce a race.

    The original issue targets Ubuntu, so the Linux/WSL implementation satisfies its current platform scope. Full native Windows reclamation would require a separate external GC gate and should be handled as a follow-up design.

    Scope

    This change only reclaims disposable control-plane data under runtime-hosts/<rootId>. It does not delete State Root data or user session data.

  4. me2seeks commented on Sep 14, 2026

    @me2seeks
    ContributorAuthor

    Image

    Proposed Solution: Runtime Host Control Directory Reaping

    建议的解决方案:运行时主机控制目录的重新整理/优化
    I have completed a working implementation for reclaiming stale Runtime Host control directories.我已经完成了用于恢复已失效的运行时主机控制目录的实用程序的编写工作。

    Design

    The cleanup process consists of three stages:清理过程分为三个阶段:

    1. Lock validation  锁验证
      A directory is eligible for reclamation only when both owner.lock and .maka-artifact-writer.lock can be acquired exclusively.只有当 owner.lock 和 .maka-artifact-writer.lock 都能被独占获取时,该目录才具备被回收的资格。
      This protects active Runtime Hosts, Root Readers, and Artifact Writers from accidental deletion.这样可以防止运行中的主机、根读取器以及数据写入器被意外删除。

    2. Atomic quarantine  原子级隔离/原子级隔离措施
      Instead of deleting runtime-hosts/<rootId> directly, the directory is atomically renamed to:而不是直接删除 runtime-hosts/<rootId> ,该目录会被原子性地重命名为:

      runtime-hosts/.reap/<claimUuid>/<rootId>
      

      Cleanup only operates on this quarantined path. If another Runtime Host concurrently recreates the original <rootId> path, the newly created directory cannot be deleted by the previous cleanup operation.清理操作仅针对被隔离的路径进行。如果另一个运行时主机同时重新创建了原来的 <rootId> 路径,那么新创建的目录将无法被之前的清理操作所删除。

    3. Background reaping  背景收割
      A normal Runtime Host shutdown attempts immediate cleanup.在正常情况下,运行时主机在关闭时会立即进行清理操作。
      Directories left behind by crashes are reclaimed by a later Runtime Host after that Host has already reached the ready state. The scan is streaming and bounded to 1,024 entries or 500 ms per batch, whichever comes first.因程序崩溃而遗留的目录,会由后续进入“就绪状态”的运行时主机来回收。扫描过程是连续进行的,但每批处理的条目数最多为 1,024 条,或者每批处理的時間最多为 500 毫秒,以先满足其中的条件为准。

    Safety Guarantees

    • A normal Runtime Host shutdown does not leave its <rootId> directory behind when cleanup is supported.在支持清理功能的情况下,正常的运行时主机关闭过程不会留下其 <rootId> 目录。
    • Active Runtime Hosts, Root Readers, and Artifact Writers are protected by the two-lock protocol.活跃的运行时主机、根读取器以及数据生成器都受到双锁协议的保护。
    • The original <rootId> path is never recursively deleted.原始的 <rootId> 路径永远不会被递归删除。
    • Symlinks, invalid root IDs, unstable directory identities, and lock-file identity changes are rejected.符号链接、无效的根 ID、不稳定的目录标识以及锁文件的变更都会被拒绝。
    • Cleanup failures are best-effort and never cause Runtime Host shutdown to fail.清理操作属于“尽力而为”的类型,绝不会导致运行时主机的关闭失败。
    • Tombstones left by a crash between rename and deletion are reclaimed by a later sweep.由 rename 与删除操作共同导致的墓碑式数据结构,会被后续的清理操作所清除。
    • The existing state-root-owners/<rootId>.lock lifecycle and leader-election model remain unchanged.现有的 state-root-owners/<rootId>.lock 生命周期和领导者选举机制保持不变。

    Readiness and Scale

    The reaper starts asynchronously only after the Runtime Host has reached the ready state. Therefore, a large number of stale directories does not delay Host startup.只有当运行时主机处于就绪状态后,收割器才会开始异步工作。因此,大量过时的目录不会延迟主机的启动过程。

    In a WSL/ext4 test containing 100,000 directories:在包含 100,000 个目录的 WSL/ext4 测试环境中:

    • Runtime Host ready latency was approximately 18 ms.运行时主机的准备延迟大约为 18 毫秒。
    • The background sweep completed independently in approximately 16.36 seconds.背景扫描过程在大约 16.36 秒内自动完成。
    • Directory traversal used streaming iteration rather than constructing a 100,000-entry array or launching 100,000 concurrent promises.在目录遍历过程中,采用了流式迭代的方式,而不是构建包含 10 万个元素的数组,或是同时启动 10 万个异步操作。

    Validation

    The Linux/WSL implementation has been validated for:Linux/WSL 版本的实现已经过验证,适用于以下情况:

    • Normal shutdown cleanup  正常关机后的清理工作
    • Active Host, Reader, and Writer protection主动式主机、读取器和写入器保护功能
    • Cross-process reaper contention跨进程收割器竞争问题
    • Recovery after SIGKILL following quarantine在 SIGKILL 隔离期结束后恢复情况
    • Concurrent recreation of the original <rootId> path对原始 <rootId> 路径的同步再现/重新生成
    • Symlink and invalid-path protection符号链接及无效路径保护机制
    • Real 100,000-directory readiness behavior真正的、具备 10 万条记录的完整功能表现

    Windows Behavior

    Native Windows may reject renaming a directory while lock-file handles inside it remain open. In that situation, the implementation safely skips reclamation instead of falling back to a close-then-delete sequence that could introduce a race.如果某个目录中被使用的锁文件仍然处于打开状态,那么原生 Windows 系统会拒绝对该目录进行重命名操作。在这种情况下,系统会安全地跳过重命名操作,而不会采取先关闭文件再删除的步骤,因为后者可能会导致竞争条件。

    The original issue targets Ubuntu, so the Linux/WSL implementation satisfies its current platform scope. Full native Windows reclamation would require a separate external GC gate and should be handled as a follow-up design.该问题最初是针对 Ubuntu 系统提出的,因此,Linux/WSL 版本的实现已经满足了当前平台的需求。要实现完全原生的 Windows 环境支持,就需要另外添加一个外部垃圾回收机制,这可以作为后续的设计任务来处理。

    Scope

    This change only reclaims disposable control-plane data under runtime-hosts/<rootId>. It does not delete State Root data or user session data.这一变更仅会回收 runtime-hosts/<rootId> 下那些可被丢弃的控制平面数据。它不会删除状态根数据或用户会话数据。

    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?
    Looks good. Do you have a PR for this implementation yet?

  5. Yx01-me commented on Sep 14, 2026

    @Yx01-me
    Contributor

    Image

    Proposed Solution: Runtime Host Control Directory Reaping

    建议的解决方案:运行时主机控制目录的重新整理/优化
    I have completed a working implementation for reclaiming stale Runtime Host control directories.我已经完成了用于恢复已失效的运行时主机控制目录的实用程序的编写工作。

    Design

    The cleanup process consists of three stages:清理过程分为三个阶段:

    1. Lock validation  锁验证
      A directory is eligible for reclamation only when both owner.lock and .maka-artifact-writer.lock can be acquired exclusively.只有当 owner.lock 和 .maka-artifact-writer.lock 都能被独占获取时,该目录才具备被回收的资格。
      This protects active Runtime Hosts, Root Readers, and Artifact Writers from accidental deletion.这样可以防止运行中的主机、根读取器以及数据写入器被意外删除。

    2. Atomic quarantine  原子级隔离/原子级隔离措施
      Instead of deleting runtime-hosts/<rootId> directly, the directory is atomically renamed to:而不是直接删除 runtime-hosts/<rootId> ,该目录会被原子性地重命名为:

      runtime-hosts/.reap/<claimUuid>/<rootId>
      

      Cleanup only operates on this quarantined path. If another Runtime Host concurrently recreates the original <rootId> path, the newly created directory cannot be deleted by the previous cleanup operation.清理操作仅针对被隔离的路径进行。如果另一个运行时主机同时重新创建了原来的 <rootId> 路径,那么新创建的目录将无法被之前的清理操作所删除。

    3. Background reaping  背景收割
      A normal Runtime Host shutdown attempts immediate cleanup.在正常情况下,运行时主机在关闭时会立即进行清理操作。
      Directories left behind by crashes are reclaimed by a later Runtime Host after that Host has already reached the ready state. The scan is streaming and bounded to 1,024 entries or 500 ms per batch, whichever comes first.因程序崩溃而遗留的目录,会由后续进入“就绪状态”的运行时主机来回收。扫描过程是连续进行的,但每批处理的条目数最多为 1,024 条,或者每批处理的時間最多为 500 毫秒,以先满足其中的条件为准。

    Safety Guarantees

    • A normal Runtime Host shutdown does not leave its <rootId> directory behind when cleanup is supported.在支持清理功能的情况下,正常的运行时主机关闭过程不会留下其 <rootId> 目录。
    • Active Runtime Hosts, Root Readers, and Artifact Writers are protected by the two-lock protocol.活跃的运行时主机、根读取器以及数据生成器都受到双锁协议的保护。
    • The original <rootId> path is never recursively deleted.原始的 <rootId> 路径永远不会被递归删除。
    • Symlinks, invalid root IDs, unstable directory identities, and lock-file identity changes are rejected.符号链接、无效的根 ID、不稳定的目录标识以及锁文件的变更都会被拒绝。
    • Cleanup failures are best-effort and never cause Runtime Host shutdown to fail.清理操作属于“尽力而为”的类型,绝不会导致运行时主机的关闭失败。
    • Tombstones left by a crash between rename and deletion are reclaimed by a later sweep.由 rename 与删除操作共同导致的墓碑式数据结构,会被后续的清理操作所清除。
    • The existing state-root-owners/<rootId>.lock lifecycle and leader-election model remain unchanged.现有的 state-root-owners/<rootId>.lock 生命周期和领导者选举机制保持不变。

    Readiness and Scale

    The reaper starts asynchronously only after the Runtime Host has reached the ready state. Therefore, a large number of stale directories does not delay Host startup.只有当运行时主机处于就绪状态后,收割器才会开始异步工作。因此,大量过时的目录不会延迟主机的启动过程。
    In a WSL/ext4 test containing 100,000 directories:在包含 100,000 个目录的 WSL/ext4 测试环境中:

    • Runtime Host ready latency was approximately 18 ms.运行时主机的准备延迟大约为 18 毫秒。
    • The background sweep completed independently in approximately 16.36 seconds.背景扫描过程在大约 16.36 秒内自动完成。
    • Directory traversal used streaming iteration rather than constructing a 100,000-entry array or launching 100,000 concurrent promises.在目录遍历过程中,采用了流式迭代的方式,而不是构建包含 10 万个元素的数组,或是同时启动 10 万个异步操作。

    Validation

    The Linux/WSL implementation has been validated for:Linux/WSL 版本的实现已经过验证,适用于以下情况:

    • Normal shutdown cleanup  正常关机后的清理工作
    • Active Host, Reader, and Writer protection主动式主机、读取器和写入器保护功能
    • Cross-process reaper contention跨进程收割器竞争问题
    • Recovery after SIGKILL following quarantine在 SIGKILL 隔离期结束后恢复情况
    • Concurrent recreation of the original <rootId> path对原始 <rootId> 路径的同步再现/重新生成
    • Symlink and invalid-path protection符号链接及无效路径保护机制
    • Real 100,000-directory readiness behavior真正的、具备 10 万条记录的完整功能表现

    Windows Behavior

    Native Windows may reject renaming a directory while lock-file handles inside it remain open. In that situation, the implementation safely skips reclamation instead of falling back to a close-then-delete sequence that could introduce a race.如果某个目录中被使用的锁文件仍然处于打开状态,那么原生 Windows 系统会拒绝对该目录进行重命名操作。在这种情况下,系统会安全地跳过重命名操作,而不会采取先关闭文件再删除的步骤,因为后者可能会导致竞争条件。
    The original issue targets Ubuntu, so the Linux/WSL implementation satisfies its current platform scope. Full native Windows reclamation would require a separate external GC gate and should be handled as a follow-up design.该问题最初是针对 Ubuntu 系统提出的,因此,Linux/WSL 版本的实现已经满足了当前平台的需求。要实现完全原生的 Windows 环境支持,就需要另外添加一个外部垃圾回收机制,这可以作为后续的设计任务来处理。

    Scope

    This change only reclaims disposable control-plane data under runtime-hosts/<rootId>. It does not delete State Root data or user session data.这一变更仅会回收 runtime-hosts/<rootId> 下那些可被丢弃的控制平面数据。它不会删除状态根数据或用户会话数据。

    Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet? Looks good. Do you have a PR for this implementation yet?

    #5268 @me2seeks this

  6. Astro-Han commented on Sep 18, 2026

    @Astro-Han
    Contributor

    Working on this issue, there is other half of the problem.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions