Skip to content

fix(testing): read the canonical authority worker portably on Windows - #9236

Closed
BigDataDZ wants to merge 1 commit into
loopx-project:mainfrom
BigDataDZ:codex/canonical-worker-windows-read-20261122
Closed

BigDataDZ wants to merge 1 commit into
loopx-project:mainfrom
BigDataDZ:codex/canonical-worker-windows-read-20261122

Conversation

@BigDataDZ

Copy link
Copy Markdown
Contributor

Motivation / 动机

d77219074(#9201,"pytest speedups")把 canonical authority fixture 的 _read_line 改为 selectors.DefaultSelector + select() 读 worker 的 stdout 管道。Windows 的 select() 只支持套接字,因此 native Windows 上所有使用 canonical authority worker 的测试(_write_live_fixture 的全部消费者)都失败:

OSError: [WinError 10038] 在一个非套接字上尝试了一个操作。
AssertionError: canonical authority worker failed after respawn:   # stderr 尾为空

Linux CI 不受影响(管道可 select),所以该回归只伤害 native Windows 开发/验证环境——正是 #8604 系列修复所依赖的环境。

Change / 变更

tests/control_plane/canonical_authority_fixture.py:

  • Windows(os.name == "nt")下由守护线程把 worker 的响应行排入 queue.SimpleQueue,EOF 放入哨兵;_read_line 在 Windows 分支带 deadline 等待队列,保持与原实现相同的 TimeoutError/EOFError 语义。respawn 时在 _spawn 内重建队列,避免跨代残留。
  • POSIX 路径的 selectors 实现逐字保留,不影响 Linux CI。

Validation / 验证

基于 main 7821e04f0,真实 Windows(Python 3.12):

  • 未修复 main:test_loopx_turn_driver.py -k "quota_cli_projects_outer or heartbeat or receipt or should_run" → 9 failed / 1 passed(全部 WinError 10038);test_builtin_claim_preflight_refusal_preserves_lease_receipts_and_journal 6/6 变体失败。
  • 本修复后:同选择器 → 2 failed / 8 passed;上例 6 变体中 4 个通过。每个恢复的失败都是 WinError 10038 消失、worker 正常应答。
  • 剩余 2 个 [interval_wait-file/sqlite] 失败与本修复无关:错误为 turn_execution_preparation_claim_failed(builtin claim 子进程在本机启动失败,恢复完整系统 PATH 仍复现;Linux CI 不涉及),未修复 main 上同样失败,属独立本地环境问题。

_read_line registered the worker's stdout pipe with selectors and
selected on it. Windows select() only supports sockets, so every
canonical-fixture setup failed natively with WinError 10038 and an
empty stderr tail. Drain response lines from a daemon thread into a
queue on Windows and keep the selector path unchanged on POSIX.

Signed-off-by: BigDataDZ <76271875+BigDataDZ@users.noreply.github.com>
@BigDataDZ

Copy link
Copy Markdown
Contributor Author

两个失败检查均与本 PR 无关(既有故障留证)

  1. kernel-contracts 失败于 test_cli_output_budget.py::test_manifest_covers_the_declared_agent_facing_surface_set(assert set(CLI_OUTPUT_COMMAND_CLASSIFICATION_BY_ID) == help_command_ids)。本地复现确认:该测试在上游 main dc79482c8(含 f7cd7aaec/7821e04f0)上同样失败,属 main 既有 CLI 清单漂移,非本 PR 引入——本 PR 只改 tests/control_plane/canonical_authority_fixture.py 的读取方式,不触及任何 agent-facing CLI 面。

  2. windows-powershell 为跨 PR 既有故障(见 fix(coordination): tolerate Windows ctime skew in source lease reads #8761 / fix(coordination): order the lease inventory by member name #8762 的同名失败分析)。

本 PR 的目标场景(canonical authority worker 的 native Windows 读取)已在 PR 正文用修复前后对照验证(9 挂 → 2 挂,恢复的失败全部因 WinError 10038 消失)。

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · gpt-6.2-sol · OpenAI · runtime_reported · xhigh

动机 / Motivation

在原生 Windows 上验证 LoopX 权威状态的贡献者,需要测试夹具能够稳定读取常驻 Node worker 的响应。 原读取器依靠 POSIX 选择器等待子进程 stdout。Windows 选择器不支持该管道,作者在真实 Windows 看到多项初始化失败。 在真实 Node 子进程上仅选择新 Windows 队列分支,令第一条请求超过期限、随后才写出响应。队列抛出 Empty,worker 留在原进程。下一请求收到上次超时的响应。本机观测覆盖 Python 队列和 OS 管道,Windows 原生跑法仍以作者记录为依据。 当前头在 Windows 分支通过独立读取线程把响应放入队列,POSIX 保持原读取方式。原有环境成功路径仍可用。 独立验证在 macOS 上运行,使用真实子进程管道并精确选择 Windows 分支。未独立运行原生 Windows。

改动思路 / Approach

保留单个常驻 worker 和既有资源所有者。Windows 需要专用的管道运输策略,超时与进程生命周期应继续共用。 修改只作用于会启动真实本地权威 worker 的测试夹具。无生产代码改动。 把 queue.Empty 归一到 worker 的既有超时关闭或重建路径,确保超时进程结束后才可消费新响应。读取线程捕获本代队列,补上延迟响应和下一轮实际返回值的回归覆盖。

具体改动 / Changes

逐项检查当前头 64be165 和 merge base 1818a28,1 个文件,+26/-0。只有一个测试夹具路径。_spawn 添加 Windows 读线程和队列,_read_line 在此分支用 deadline 等待,request 仍负责关闭和重生。 核对管道可用性(pipe-portability)、超时隔离(timeout-isolation)和 POSIX 兼容(posix-parity)。 当前 main 可干净整合,唯一修改文件与精确评审头一致。 评审依据为 64be165。 目标规范

关键代码讲解

对主干的风险 / Risks

[P2] Windows 队列超时后会把上一条响应交给下一请求。 在真实 Node 子进程上仅选择新 Windows 队列分支,令第一条请求超过期限、随后才写出响应。队列抛出 Empty,worker 留在原进程。下一请求收到上次超时的响应。本机观测覆盖 Python 队列和 OS 管道,Windows 原生跑法仍以作者记录为依据。 把 queue.Empty 归一到 worker 的既有超时关闭或重建路径,确保超时进程结束后才可消费新响应。读取线程捕获本代队列,补上延迟响应和下一轮实际返回值的回归覆盖。 夹具可能用上一条请求的结果判断这次权威状态,造成后续测试误通过或者误失败。

精确头在本机的常驻 worker、两种本地 provider 真实读取与配额入口等 19 个用例通过。独立管道对照的即刻响应成功,旧基线与新头的 POSIX 对照超时后下次均读取正确响应。 实际 worker、SQLite/File 及 quota 入口定向 19 项通过。本地真实管道 3 组对照复现 Windows 分支跨请求污染,两个 POSIX 对照不出现错读。 精确头标准 premerge 的 5 项直接检查与 3 项选定检查通过。 独立验证在 macOS 上运行,使用真实子进程管道并精确选择 Windows 分支。未独立运行原生 Windows。

我的整体评价 / Verdict

请求修改。保留单个常驻 worker 和既有资源所有者。Windows 需要专用的管道运输策略,超时与进程生命周期应继续共用。 修改只作用于会启动真实本地权威 worker 的测试夹具。无生产代码改动。 已超时请求的响应不能作为下一条请求的结果。 请求修补该分支,并保留已回归验证的原流程。 操作者影响限于测试结果的稳定性。 原生 Windows 独立复验仍待验证。

English verdict: REQUEST_CHANGES at 64be165. The Windows queue branch propagates queue.Empty without closing the worker. A real delayed child response then becomes the result of the next request. Both POSIX controls preserve response identity.

remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("canonical authority worker timed out waiting for response")
line = self._line_queue.get(timeout=remaining)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Windows 队列超时后会把上一条响应交给下一请求。在真实 Node 子进程上仅选择新 Windows 队列分支,令第一条请求超过期限、随后才写出响应。队列抛出 Empty,worker 留在原进程。下一请求收到上次超时的响应。本机观测覆盖 Python 队列和 OS 管道,Windows 原生跑法仍以作者记录为依据。 把 queue.Empty 归一到 worker 的既有超时关闭或重建路径,确保超时进程结束后才可消费新响应。读取线程捕获本代队列,补上延迟响应和下一轮实际返回值的回归覆盖。

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · Gemini · Google · self_reported

动机 / Motivation

在原生 Windows 上运行 canonical authority 夹具测试(例如通过 _write_live_fixture 启动常驻 Node worker 的控制面测试)的贡献者与测试执行器。
改造前,selectors.DefaultSelector 在 Windows 上不支持匿名管道并抛出 WinError 10038 导致测试初始化全部失败;当前补丁虽改用后台线程与 queue.SimpleQueue 避开 select() 限制,但在超时场景下 _line_queue.get(timeout=remaining) 会抛出 queue.Empty 而非 TimeoutError,导致 request() 未关闭超时进程,上一条延迟响应会留在队列中污染下一次请求。
当前头在正常响应路径上恢复了通道读写,但未实现超时后关闭或重建 worker 的等价隔离语义,后续请求仍可能收到上一条超时请求的响应。
本评审与当前 PR 仅覆盖测试夹具 tests/control_plane/canonical_authority_fixture.py 的跨平台管道读取,不修改生产控制面或权威存储实现。

改动思路 / Approach

该 PR 维持“单进程常驻 Node worker + 串行锁 call_lock”的既定资源模型:POSIX 继续走原有的 selectors.DefaultSelector 非阻塞读取路径,仅在 os.name == "nt" 时由守护线程持续 readline() 并投入 queue.SimpleQueue,_read_line 再按截止时间从队列取行。该方向正确地将平台差异限制在测试夹具的管道传输层,没有额外引入新的外部依赖或并发控制面状态;但队列等待必须与原有的 TimeoutError / 进程清理边界对齐,否则超时后的残留进程与队列消息会破坏请求—响应的一一对应关系。

具体改动 / Changes

  • 改动分类(64be16576493eae2c1cb793ff4691f30d8d97300,相对 merge base 1818a2836bb47b086e526e4bd9f1bed6b43cf182):仅修改 1 个测试夹具文件 tests/control_plane/canonical_authority_fixture.py(+26/-0),无生产代码、文档或 RFC 变更。
  • 行为链路:
    • _CanonicalAuthorityWorker.__init__ 初始化 self._line_queue: queue.SimpleQueue。
    • _CanonicalAuthorityWorker._spawn 在 os.name == "nt" 时重置 self._line_queue 并启动 _drain_lines 守护线程读取 proc.stdout、EOF 时放入 None;但闭包直接引用 self._line_queue 而非局部队列实例,且仅捕获 OSError(关闭管道并发可能抛出 ValueError)。
    • _CanonicalAuthorityWorker._read_line 在 Windows 分支直接调用 self._line_queue.get(timeout=remaining),未捕获 queue.Empty 并转为 TimeoutError。
  • 规范依据:无独立书面规范文档,以 PR 描述与原夹具 _CanonicalAuthorityWorker.request 的请求—响应隔离语义为评审依据。

对主干的风险 / Risks

  • [P2][Blocking] Windows 分支超时抛出 queue.Empty 导致不关闭 worker 且后续请求读到上一条超时响应
    • 触发路径:在 os.name == "nt" 分支下发送一条耗时超过 _WORKER_REQUEST_TIMEOUT_SECONDS 才输出的请求。_read_line 在 self._line_queue.get(timeout=remaining) 超时时直接抛出 queue.Empty。由于 queue.Empty 既不是 TimeoutError 也不是 (BrokenPipeError, EOFError, OSError),request() 不会执行 self.close(),原 Node 子进程与 _line_queue 继续存活;当延迟响应随后写入队列,下一条 request() 就会直接从队列取出上一条请求的结果。
    • 附加风险(次要):_drain_lines 捕获的是可变属性 self._line_queue 而非当次 _spawn 的局部变量,若旧进程在重建后才结束,finally 写入的 None 会误投递到新一代队列;同时 request() 中原有的 except (BrokenPipeError, EOFError, OSError): 实际在 Python MRO 中覆盖了后面的 except TimeoutError:(issubclass(TimeoutError, OSError) 为 True),修复时应确保 TimeoutError 按预期语义先关闭超时进程。
    • 独立对照实测(macOS 隔离工作树,真实 Node 子进程管道):
      • POSIX 路径(基线对照):快速请求返回 {'tag': 'posix-fast'},超时请求关闭原进程(经 OSError 捕获并重建重试后报 AssertionError),随后新请求正常返回 {'tag': 'posix-after'}。
      • Windows 队列路径(模拟 caf.os.name == "nt" 与真实 Node 子进程):快速请求返回 {'tag': 'nt-fast'},超时请求抛出 queue.Empty 且未关闭子进程,等待 450ms 后发起新请求 {"tag": "nt-after"} 却错误返回了上一条请求的 {'tag': 'nt-timeout'}。
  • 仓库验证与卫生扫描(精确头 64be16576493eae2c1cb793ff4691f30d8d97300):
    • pytest tests/control_plane/test_canonical_authority_fixture.py tests/control_plane/test_todo_external_wait.py -q:PASS(16 passed)。
    • ruff check tests/control_plane/canonical_authority_fixture.py:PASS。
    • loopx check --scan-path tests/control_plane/canonical_authority_fixture.py:PASS(errors=0, warnings=0)。
    • loopx canary premerge --from-git-diff --git-diff-base origin/main:PASS(4/4 直接检查、3/3 目录 canary 全部通过)。
    • 本机环境为 macOS,未直接在原生 Windows 主机上执行;Windows 队列分支通过真实 Node 子进程管道与 os.name == "nt" 代理对象复现。

我的整体评价 / Verdict

本 PR 准确定位了原生 Windows 下 selectors 不支持子进程 stdout 管道的问题,且仅改测试夹具文件,方向可取;但 Windows 队列分支在超时时直接抛出 queue.Empty 而不关闭 worker,已通过真实 Node 子进程对照实测证实会导致后续请求错读上一条超时请求的响应(long_horizon 与 user_experience 均存在测试夹具状态污染退化),因此当前提交尚不能合入。仅修改测试夹具文件,不涉及任何前端界面渲染视图(visual_review 不适用,未验证范围:原生 Windows 实机环境)。

最小修复要求:在 Windows _read_line 分支将 queue.Empty 转换为 TimeoutError(并在 request() 中将 except TimeoutError: 置于 except OSError: 之前以显式区分超时关闭与管道重连,或在确认预期语义后统一关闭超时进程),在 _spawn 内让 _drain_lines 绑定局部 line_queue 变量并捕获 (OSError, ValueError),同时在 tests/control_plane/test_canonical_authority_fixture.py 补齐延迟超时后下一次请求不被旧响应污染的回归测试。

English verdict: REQUEST_CHANGES at 64be165 — the Windows queue reader propagates queue.Empty without closing the timed-out worker, causing a subsequent request on a real Node pipe to consume the previous timed-out response; convert queue.Empty to TimeoutError, bind a per-spawn queue instance in _drain_lines, and add a delayed-response isolation regression test.

@mergify

mergify Bot commented Nov 22, 2026

Copy link
Copy Markdown

This pull request has merge conflicts with main and cannot be merged
until they are resolved. Please rebase or merge the base branch, @BigDataDZ.

Choose the remote for the base repository, not an out-of-date fork.
For a fork clone, first inspect git remote -v; upstream must point
to https://github.com/loopx-project/loopx.git. If it is absent, add it
with git remote add upstream https://github.com/loopx-project/loopx.git.
Then run:

git fetch upstream
git rebase upstream/main
# Resolve each conflict, git add the resolved files, then git rebase --continue.
git push --force-with-lease origin HEAD

For a same-repository clone whose origin points to
https://github.com/loopx-project/loopx.git, use origin instead of
upstream for fetch/rebase. If you prefer merging the base, use
git merge <base-remote>/main and push normally.

Keep the DCO Signed-off-by trailer on every commit when you rebase.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 22, 2026
@BigDataDZ

Copy link
Copy Markdown
Contributor Author

关闭为已被 #9218 取代:main 的 portable pipes 实现与本 PR 思路一致(Windows 上线程 drain 管道进队列、POSIX 保留原路径),且覆盖更完整(stderr 同步处理、OSError 经队列传播)。上游版本更优,本 PR 不再需要。感谢快速合入官方修复。

@BigDataDZ BigDataDZ closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-rebase Mergify: the pull request has merge conflicts with its base branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants