Windows sandbox initialization fails with "setup refresh failed with status exit code: 1"
Bug Description
Codex desktop app on Windows fails to initialize the sandbox component, causing intermittent command execution failures. The error message is:
windows sandbox: setup refresh failed with status exit code: 1
Steps to Reproduce
Open Codex desktop app on Windows
Try to execute any shell command or use the js_repl tool
Observe that commands fail with the sandbox initialization error
Occasionally commands succeed, but failure rate is very high (~80-90%)
Expected Behavior
Shell commands and js_repl should execute reliably without sandbox initialization failures.
Actual Behavior
Shell commands fail with: Io(Custom { kind: Other, error: "windows sandbox: setup refresh failed with status exit code: 1" })
js_repl fails with: node_repl kernel exited unexpectedly ... windows sandbox failed: setup refresh failed with status exit code: 1
The error is intermittent - sometimes commands succeed, but most fail
Environment
OS: Windows (Chinese edition)
Codex Desktop App (latest version)
Node.js installed at: D:\nodejs
npm global prefix: D:\npm-global
Workspace: D:\codex
Config File (D:\codex-config\config.toml)
[sandbox]
mode = "danger-full-access"
sandbox.log Content
failed: SetNamedSecurityInfoW sandbox dir failed: 5
[2026-05-25 14:13:15.845 codex-windows-sandbox-setup.exe] setup error: helper_sandbox_lock_failed: lock sandbox bin dir C:\Users\Administrator\.codex\.sandbox-bin failed: SetNamedSecurityInfoW sandbox dir failed: 5
[2026-05-25 14:13:15.854 codex.exe] setup refresh: exited with status ExitStatus(ExitStatus(1))
Additional Context
Error code 5 in SetNamedSecurityInfoW is ERROR_ACCESS_DENIED
Setting mode = "danger-full-access" in config.toml does not resolve the issue
The sandbox setup binary (codex-windows-sandbox-setup.exe) appears to have permission issues accessing C:\Users\Administrator\.codex\.sandbox-bin
This makes the Codex desktop app nearly unusable on Windows as most operations require shell execution
Suggested Investigation
Check if codex-windows-sandbox-setup.exe has proper permissions to modify sandbox directories
Investigate why SetNamedSecurityInfoW fails with access denied
Consider providing a way to completely bypass sandbox setup on Windows
The danger-full-access mode should ideally skip sandbox initialization entirely
3 Comments
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
Proposed Solution
Solution: Codex #24416 — Windows Sandbox Init Fails with
SetNamedSecurityInfoWERROR_ACCESS_DENIEDIssue: openai/codex #24416
Title: Windows sandbox initialization fails with "setup refresh failed with status exit code: 1"
Severity: High (~80-90% command failure rate on affected Windows systems)
Price Tier: $2,000–$3,000
---
1. Root Cause Analysis
1.1 Symptom Chain
1.2 Primary Failure Point:
lock_sandbox_dir()inwin.rsFile:
codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs, lines 290–385The
lock_sandbox_dir()function constructs a new DACL withSetEntriesInAclW()and then callsSetNamedSecurityInfoW()to apply it to the sandbox directory. On certain Windows configurations,this call returns error code 5 (
ERROR_ACCESS_DENIED).Why
SetNamedSecurityInfoWfails with ERROR_ACCESS_DENIED (code 5):Administratoraccount (or a member of theAdministratorsgroup) runs without an elevatedtoken, the process receives a filtered token that lacks the
SE_RESTORE_NAMEandSE_TAKE_OWNERSHIP_NAMEprivileges.SetNamedSecurityInfoWrequiresSE_RESTORE_NAMEwhensetting a DACL that differs from what the caller's token naturally allows.
.sandbox-bindirectory may be created under a parentdirectory (e.g.,
C:\Users\Administrator\.codex) that has protected/inherited ACLs thatprevent even Administrators from freely modifying the DACL. Windows' ACL inheritance model
means the directory inherits deny ACEs or restricted ACEs from the parent.
GitHub Actions self-hosted on Windows), and certain virtualized environments, the security
model is more restrictive. The
Administratorsgroup may be present but lack the specificprivileges needed for ACL manipulation.
handles or mutexes may still be held by orphaned processes, causing
SetNamedSecurityInfoWto fail because the directory is in use.
real_userfield in the elevation payload (setup.rs:204)defaults to
std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string()).In some environments (e.g., Windows Sandbox, service accounts), this may resolve to a
username that doesn't exist or can't be looked up, causing the
resolve_sid(real_user)call in
lock_sandbox_dirto succeed but the resulting SID to not match the actualtoken's groups.
1.3 Secondary Failure Point:
ensure_allow_mask_aces_with_inheritance_impl()inacl.rsFile:
codex-rs/windows-sandbox-rs/src/acl.rs, lines 307–376The
SetNamedSecurityInfoWcall at line 342 also fails hard on error code 5. This function iscalled during write ACE grants and deny ACE operations, and its failure propagates up as a
fatal error.
1.4 Why
mode = "danger-full-access"Doesn't Resolve ItThe
run_setup_refresh()function insetup.rs(lines 114–119) correctly skips setup forDangerFullAccessandExternalSandbox:However, the issue report states that setting
mode = "danger-full-access"does NOT resolve theproblem. This indicates the setup is being invoked through a different code path that does
not check the policy:
run_setup_refresh_with_overrides()(setup.rs:134–139) does not check the policy andalways calls
run_setup_refresh_inner().run_setup_refresh_with_extra_read_roots()which checks the policybut the caller might pass a different policy object.
run_windows_sandbox_capture) inwindows_impl(lib.rs:368–500)does not go through
run_setup_refreshat all — it usesprepare_legacy_spawn_contextandprepare_legacy_session_securitydirectly, which may still invoke ACL operations.1.5 Log Evidence
The error code
5isERROR_ACCESS_DENIEDfromwinerror.h. The pathC:\Users\Administrator\.codex\.sandbox-binconfirms the user is running as the built-inAdministratoraccount, which is the classic scenario for UAC token filtering issues.---
2. Fix Approach
2.1 Fix 1: Graceful Fallback in
lock_sandbox_dir()(Primary Fix)File:
codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rsProblem:
lock_sandbox_dir()returnsErrimmediately whenSetNamedSecurityInfoWfails,causing the entire setup to abort.
Solution: When
SetNamedSecurityInfoWreturnsERROR_ACCESS_DENIED(5), log a warningand continue with the existing permissions instead of failing. The sandbox directory already
exists (created by
std::fs::create_dir_all), and the existing ACLs are likely sufficientfor the sandbox to function.
2.2 Fix 2: Graceful Fallback in
ensure_allow_mask_aces_with_inheritance_impl()(Secondary Fix)File:
codex-rs/windows-sandbox-rs/src/acl.rsProblem: The
SetNamedSecurityInfoWcall at line 342 fails hard on access denied, causingwrite ACE grants to fail.
Solution: Add the same graceful fallback pattern:
2.3 Fix 3: Stale Lock File / Handle Cleanup
File:
codex-rs/windows-sandbox-rs/src/bin/setup_main/win/read_acl_mutex.rsProblem: If a previous setup run crashed, the mutex
Local\CodexSandboxReadAclmay stillexist, causing subsequent runs to skip ACL operations. Additionally, file handles held by
orphaned processes can prevent ACL changes.
Solution: Add a cleanup mechanism that detects stale mutex state and attempts to clean up
before proceeding:
Call this at the beginning of
run_setup_full()before any ACL operations.2.4 Fix 4: Ensure
danger-full-accessMode Is Respected EverywhereFile:
codex-rs/windows-sandbox-rs/src/setup.rsProblem:
run_setup_refresh_with_overrides()does not check the policy, so setup isalways attempted regardless of the sandbox mode.
Solution: Add policy check to
run_setup_refresh_with_overrides():Also ensure the
SandboxSetupRequeststruct exposes the policy for this check.2.5 Fix 5: Retry with Privilege Elevation
File:
codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rsProblem: The setup binary may not have the required privileges (
SE_RESTORE_NAME) tomodify ACLs.
Solution: Before calling
SetNamedSecurityInfoW, attempt to enable the requiredprivileges. If that fails, fall back to the graceful degradation approach:
---
3. Code-Level Fix Summary
Files to Modify
| File | Changes |
|------|---------|
|
codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs| Fix 1 (graceful fallback inlock_sandbox_dir), Fix 5 (privilege elevation attempt) ||
codex-rs/windows-sandbox-rs/src/acl.rs| Fix 2 (graceful fallback inensure_allow_mask_aces_with_inheritance_impl) ||
codex-rs/windows-sandbox-rs/src/bin/setup_main/win/read_acl_mutex.rs| Fix 3 (stale lock cleanup) ||
codex-rs/windows-sandbox-rs/src/setup.rs| Fix 4 (policy check inrun_setup_refresh_with_overrides) |Detailed Patches
Patch 1:
win.rs—lock_sandbox_dir()graceful fallbackPatch 2:
acl.rs—ensure_allow_mask_aces_with_inheritance_impl()graceful fallbackPatch 3:
setup.rs— Policy check inrun_setup_refresh_with_overrides---
4. Testing Plan
4.1 Unit Tests
| Test | Description | Expected Result |
|------|-------------|-----------------|
|
test_lock_sandbox_dir_access_denied_fallback| MockSetNamedSecurityInfoWto return error 5 | Function returnsOk(())with warning logged ||
test_lock_sandbox_dir_other_error_fails| MockSetNamedSecurityInfoWto return error 3 (path not found) | Function returnsErr||
test_ensure_allow_mask_access_denied_fallback| MockSetNamedSecurityInfoWin ACL module to return error 5 | Function returnsOk(false)without adding ACE ||
test_danger_full_access_skips_setup| Callrun_setup_refreshwithDangerFullAccesspolicy | ReturnsOk(())immediately ||
test_overrides_with_danger_full_access| Callrun_setup_refresh_with_overrideswithDangerFullAccess| ReturnsOk(())immediately (new behavior) |4.2 Integration Tests
| Test | Environment | Description |
|------|-------------|-------------|
|
test_setup_as_builtin_admin| Windows with built-in Administrator account | Setup completes without ERROR_ACCESS_DENIED ||
test_setup_in_windows_sandbox| Windows Sandbox container | Setup completes or degrades gracefully ||
test_setup_ci_runner| GitHub Actions Windows runner | Setup completes or degrades gracefully ||
test_setup_with_stale_mutex| After killing setup process mid-run | Stale mutex is cleaned up, setup succeeds ||
test_danger_full_access_no_setup| Any Windows environment | No setup binary is spawned |4.3 Manual Verification Steps
SetNamedSecurityInfoW sandbox dir failed: 5in logsdanger-full-accessmode:mode = "danger-full-access"in config.toml4.4 Regression Testing
---
5. Impact Assessment
5.1 Risk Level: Low
The fix primarily adds graceful degradation rather than changing core behavior:
SetNamedSecurityInfoWsucceeds, nothing changesERROR_ACCESS_DENIED, which currently causes a hard failure5.2 Affected Components
| Component | Impact |
|-----------|--------|
|
lock_sandbox_dir()| Primary fix target — now degrades gracefully ||
ensure_allow_mask_aces_with_inheritance_impl()| Secondary fix target — now degrades gracefully ||
run_setup_refresh_with_overrides()| Now respectsDangerFullAccesspolicy || Setup binary (
codex-windows-sandbox-setup.exe) | Exit code 0 in more scenarios || Desktop app | Fewer "setup refresh failed" errors |
5.3 Security Considerations
DangerFullAccessbehavior: This mode already exists and is designed to bypass sandboxing; the fix just ensures it's properly respected5.4 Performance Impact
5.5 Backward Compatibility
---
6. Implementation Notes
6.1 Error Code Reference
| Code | Constant | Description |
|------|----------|-------------|
| 0 |
ERROR_SUCCESS| Operation succeeded || 5 |
ERROR_ACCESS_DENIED| Access denied (the bug) || 1314 |
ERROR_PRIVILEGE_NOT_HELD| Required privilege not held || 3 |
ERROR_PATH_NOT_FOUND| Path not found |6.2 Related Code Paths
The following code paths also call
SetNamedSecurityInfoWand should be reviewed forsimilar issues:
acl.rs:342—ensure_allow_mask_aces_with_inheritance_impl()(covered by Fix 2)acl.rs:463—add_allow_ace()— already handles errors but could benefit from graceful fallbackacl.rs:500+—add_deny_write_ace()/add_deny_read_ace()— same pattern6.3 Logging Recommendations
When the graceful fallback is triggered, the log message should include:
This helps users and support teams understand what happened without causing alarm.
---
7. Deliverables
danger-full-accessmode behavior---
8. References
I published a small read-only diagnostic CLI for Codex setup issues that might be useful when collecting environment information for Windows sandbox failures:
Repo: https://github.com/warren2008-2020-spec/codex-doctor
npm: https://www.npmjs.com/package/codex-doctor
It does not modify Codex or the sandbox. It only reports local signals such as:
For issues like this one, my intent is to make it easier to paste a safe setup report without exposing tokens or private config. If there are specific Windows sandbox checks that would help triage this failure mode, I can add them.