Skip to content

fix(platform): only request CREATE_BREAKAWAY_FROM_JOB when the job allows it - #1325

Merged
iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
koeria:fix/launch-breakaway-access-denied
Nov 27, 2026
Merged

iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
koeria:fix/launch-breakaway-access-denied

Conversation

@koeria

@koeria koeria commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

codexhost launch 由 Desktop shell 拉起时可能直接失败,报 Cannot run + "codexhost launch" + : 拒绝访问 (os error 5)。

根因:Windows CreateProcess 在调用方进程位于一个未设置 JOB_OBJECT_LIMIT_BREAKAWAY_OK 的 Job(例如 AppX 激活或其它外部 Job 下的 shell 进程)时,会拒绝 CREATE_BREAKAWAY_FROM_JOB 请求并返回 ERROR_ACCESS_DENIED,进程根本不会启动。configure_detached_service_command 此前无条件附加该 flag,遇到这类环境必然失败。

修复:spawn 前用 IsProcessInJob + QueryInformationJobObject 探测当前 Job 是否允许 breakaway——不在任何 Job 中(flag 是 no-op)或 Job 明确允许时才附加 CREATE_BREAKAWAY_FROM_JOB;否则不带该 flag 启动,分离出来的服务嵌套在调用方 Job 中,而不是让整个启动失败。

Related issues

fixes #1321

UI Demo

No visual changes: 仅调整 Windows 进程启动 flag 的探测逻辑,无 UI 改动。

Test plan

  • cargo check -p codexhost-platform(linux,通过)
  • cargo clippy -p codexhost-platform --all-targets -- -D warnings(linux,通过,0 warning)
  • rustup target add x86_64-pc-windows-msvc 后 cargo check -p codexhost-platform --target x86_64-pc-windows-msvc(通过)与 cargo clippy -p codexhost-platform --target x86_64-pc-windows-msvc --all-targets -- -D warnings(通过,0 warning)
  • 未验证部分:Windows 实机(AppX 激活下 shell 在受限 Job 中的端到端启动)无法在本环境复现,等待 CI 与实机反馈。

…lows it

CreateProcess fails with ERROR_ACCESS_DENIED (os error 5) when the
caller lives in a job without JOB_OBJECT_LIMIT_BREAKAWAY_OK, e.g. the
Desktop shell started under AppX activation, so `codexhost launch`
could not start at all. Probe job membership and breakaway permission
first and request the breakaway only when it is safe; otherwise start
the detached service nested in the caller's job instead of failing.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug 修复
    • 改善 Windows 上 detached 服务的启动兼容性:根据当前进程是否处于作业中及作业限制,决定是否请求脱离作业;无法确认状态或不允许脱离时,不请求该标志。进程组和分离进程标志保持不变。

Windows detached service 创建时,代码根据进程的 job 状态和 job 的 breakaway 限制,决定是否设置 CREATE_BREAKAWAY_FROM_JOB。CREATE_NEW_PROCESS_GROUP 和 DETACHED_PROCESS 仍会设置。

Changes: Windows detached service 启动

Layer / File(s) Summary
按 job 状态选择 breakaway 标志
crates/platform/Cargo.toml, crates/platform/src/background.rs
Windows windows 依赖新增 Win32_System_JobObjects 功能。进程不属于 job,或 job 查询成功且允许 breakaway 时,代码才请求 CREATE_BREAKAWAY_FROM_JOB。进程或 job 状态查询失败时,不请求该标志。CREATE_NEW_PROCESS_GROUP 和 DETACHED_PROCESS 仍会设置。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

  • BytePioneer-AI/codex-host#1189:该 PR 处理相同的 Windows 启动错误,并在 spawn 返回 os error 5 后移除 breakaway 标志并重试;本 PR 则在启动前根据 Job 状态判断是否请求该标志。

Suggested reviewers: bytepioneer-ai

Merge Risk: Minimal · up to a0f6b

On Windows, codexhost launch now requests job breakaway only when the caller's Job allows it. This avoids the access-denied failure from restricted Jobs, such as when launched from Codex Desktop. When breakaway is not allowed, the service starts nested in the caller's Job instead of failing. No concrete defect was found, and the main residual uncertainty is that real Windows end-to-end startup has not yet been confirmed.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题准确描述了主要变更:仅当 Job 允许时才请求 CREATE_BREAKAWAY_FROM_JOB。变更摘要中的 process_creation_flags_for_detached_service 与 background.rs 的 Windows 启动逻辑与此一致。标题具体,且与改动直接相关。
Description check Passed 描述说明了问题根因(Windows CreateProcess 在未设置 JOB_OBJECT_LIMIT_BREAKAWAY_OK 的 Job 中拒绝 breakaway 请求)、修复方式(使用 IsProcessInJob 与 QueryInformationJobObject 探测)、关联 issue #1321 以及测试计划。与变更集直接相关。
Linked Issues check Passed [#1321] 报告的故障是 codexhost launch 在受限 Job 中调用 CreateProcess 时返回 ERROR_ACCESS_DENIED (os error 5)。当前 head 的 crates/platform/src/background.rs 新增 process_creation_flags_for_detached_service()。该函数先调用 IsPro…
Out of Scope Changes check Passed 变更范围限于 Windows 进程启动 flag 的探测逻辑。包括:crates/platform/src/background.rs 中 configure_detached_service_command 的 flag 合并,以及新增 process_creation_flags_for_detached_service();crates/platform/Cargo.toml 中为 wind…
Full details: Linked Issues check

Explanation

[#1321] 报告的故障是 codexhost launch 在受限 Job 中调用 CreateProcess 时返回 ERROR_ACCESS_DENIED (os error 5)。当前 head 的 crates/platform/src/background.rs 新增 process_creation_flags_for_detached_service()。该函数先调用 IsProcessInJob。若不在任何 Job 中,则请求 CREATE_BREAKAWAY_FROM_JOB。若在 Job 中,则仅当 QueryInformationJobObject 成功且 JOB_OBJECT_LIMIT_BREAKAWAY_OK 已设置时才请求该 flag。其他情况(包括判断失败)不请求该 flag。因此,在不允许 breakaway 的 Job 中,启动不再因该 flag 失败。该代码满足 #1321 描述的故障模式。问题未给出验收标准,无其他编码需求。风险:QueryInformationJobObject 第一个参数传入 None,而非当前进程所属 Job 的有效句柄。若查询失败,代码在允许 breakaway 的 Job 中也不请求该 flag。这会使分离服务仍嵌套在调用方 Job 中,但不会导致 #1321 的失败复现。Windows 实机端到端行为未在本次评审中验证。

Full details: Out of Scope Changes check

Explanation

变更范围限于 Windows 进程启动 flag 的探测逻辑。包括:crates/platform/src/background.rs 中 configure_detached_service_command 的 flag 合并,以及新增 process_creation_flags_for_detached_service();crates/platform/Cargo.toml 中为 windows crate 启用 Win32_System_JobObjects 功能。这些变更直接服务于 #1321 的修复。未发现与该目标无关的变更。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 94e3319 and a0f6b31.

Files selected for processing: 1

  • crates/platform/src/background.rs
Files skipped from review as they are similar to previous changes: 1

  • crates/platform/src/background.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Recent review details

Context from checks skipped due to timeout: 8

  • GitHub Check: CodeRabbit
  • GitHub Check: Vitest
  • GitHub Check: Static checks
  • GitHub Check: Check macos-26
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Web Host tests
  • GitHub Check: Check windows-latest
  • GitHub Check: Check Linux ARM64

@koeria

koeria commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

备注:#1189 针对 #1162 报告的是同一根因(受限制 Job 下 CREATE_BREAKAWAY_FROM_JOB 导致 ERROR_ACCESS_DENIED),采用 spawn 失败后重试的回退实现。本 PR 是更早拦截的实现:spawn 前探测 Job 是否允许 breakaway,正常路径保持单一调用点;如需取舍可二选一,谢谢。

@iruriQ
iruriQ merged commit b225227 into BytePioneer-AI:main Nov 27, 2026
6 of 8 checks passed
@iruriQ

iruriQ commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

感谢贡献!在共享的 crates/platform/src/background.rs 中按 Job JOB_OBJECT_LIMIT_BREAKAWAY_OK 条件请求 breakaway 的方案非常干净,已顺手补齐 cargo fmt 并合入 main。

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.

[Bug] Cannot run codexhost launch: 拒绝访问。 (os error 5)

3 participants