-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix login-shell probe cleanup #2124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steipete
merged 4 commits into
steipete:main
from
anagnorisis2peripeteia:codex/fix-path-probe-timeout-descendants
Jul 14, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d480a5
Fix login-shell probe cleanup
anagnorisis2peripeteia ffe0972
fix: harden login PATH probe cleanup
steipete 9c21ad7
Merge remote-tracking branch 'origin/main' into codex/pr-2124
steipete bc40e4b
fix: restore atomic Linux probe pipes
steipete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import Foundation | ||
|
|
||
| struct CodexExecutableResolution: Sendable { | ||
| let executable: String | ||
| let loginPATH: [String]? | ||
| } | ||
|
|
||
| typealias CodexExecutableResolver = @Sendable ( | ||
| _ environment: [String: String], | ||
| _ executable: String) -> CodexExecutableResolution? | ||
|
|
||
| private func executableIsScript(_ path: String) -> Bool { | ||
| guard let handle = FileHandle(forReadingAtPath: path) else { return true } | ||
| defer { try? handle.close() } | ||
| return (try? handle.read(upToCount: 2)) == Data("#!".utf8) | ||
| } | ||
|
|
||
| func resolveCodexExecutableForRPC( | ||
| environment: [String: String], | ||
| executable: String, | ||
| captureLoginPATH: () -> [String]?) -> CodexExecutableResolution? | ||
| { | ||
| if let override = environment["CODEX_CLI_PATH"], | ||
| FileManager.default.isExecutableFile(atPath: override) | ||
| { | ||
| // Native overrides can launch directly. Script overrides can depend on a | ||
| // login-shell PATH through `#!/usr/bin/env node` or helper commands. | ||
| let loginPATH = executableIsScript(override) ? captureLoginPATH() : nil | ||
| return CodexExecutableResolution(executable: override, loginPATH: loginPATH) | ||
| } | ||
|
|
||
| // Capture before the general resolver to preserve login-shell PATH precedence | ||
| // over bundled fallbacks and to make `/usr/bin/env node` launchers usable. | ||
| let loginPATH = captureLoginPATH() | ||
| guard let resolved = BinaryLocator.resolveCodexBinary(env: environment, loginPATH: loginPATH) | ||
| ?? TTYCommandRunner.which(executable) | ||
| else { return nil } | ||
| return CodexExecutableResolution(executable: resolved, loginPATH: loginPATH) | ||
| } | ||
|
|
||
| let defaultCodexExecutableResolver: CodexExecutableResolver = { environment, executable in | ||
| // Resolve the login-shell PATH at the actual RPC launch boundary. Warming it | ||
| // from UsageFetcher.init was both racy for env-based launchers and unsafe for | ||
| // short-lived CLI hosts because the fire-and-forget probe could outlive them. | ||
| resolveCodexExecutableForRPC( | ||
| environment: environment, | ||
| executable: executable, | ||
| captureLoginPATH: { | ||
| LoginShellPathCache.shared.currentOrCapture(shell: environment["SHELL"]) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CodexBarCore | ||
|
|
||
| struct CodexExecutableResolverTests { | ||
| @Test | ||
| func `explicit native override skips login path capture`() { | ||
| let resolved = resolveCodexExecutableForRPC( | ||
| environment: ["CODEX_CLI_PATH": "/usr/bin/true"], | ||
| executable: "codex", | ||
| captureLoginPATH: { | ||
| Issue.record("Native override should not capture a login-shell PATH") | ||
| return nil | ||
| }) | ||
|
|
||
| #expect(resolved?.executable == "/usr/bin/true") | ||
| #expect(resolved?.loginPATH == nil) | ||
| } | ||
|
|
||
| @Test | ||
| func `explicit script override captures login path for env based launchers`() throws { | ||
| let scriptURL = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("codexbar-script-override-\(UUID().uuidString)") | ||
| try Data("#!/usr/bin/env node\n".utf8).write(to: scriptURL) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: scriptURL.path) | ||
| defer { try? FileManager.default.removeItem(at: scriptURL) } | ||
|
|
||
| let loginPATH = ["/custom/node/bin", "/usr/bin"] | ||
| var captureCount = 0 | ||
| let resolved = resolveCodexExecutableForRPC( | ||
| environment: ["CODEX_CLI_PATH": scriptURL.path], | ||
| executable: "codex", | ||
| captureLoginPATH: { | ||
| captureCount += 1 | ||
| return loginPATH | ||
| }) | ||
|
|
||
| #expect(resolved?.executable == scriptURL.path) | ||
| #expect(resolved?.loginPATH == loginPATH) | ||
| #expect(captureCount == 1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When another subprocess is launched outside
ShellCommandLocatorduring the small window betweenpipe()and the laterfcntl(FD_CLOEXEC), that child can inherit these probe descriptors becauseshellSpawnLockis local to this helper and does not cover otherProcess.run()/spawn sites. The new output-holder cleanup then scans for any process holding these pipes and can SIGTERM/SIGKILL an unrelated live child, so this still regresses concurrent app activity on Darwin despite serializing overlapping shell probes.Useful? React with 👍 / 👎.