Skip to content

🛡️ Sentinel: [MEDIUM] Fix Process deadlocks by reading pipes before waitUntilExit#82

Open
NSEvent wants to merge 1 commit into
mainfrom
sentinel-fix-process-deadlock-3474175059985067787
Open

🛡️ Sentinel: [MEDIUM] Fix Process deadlocks by reading pipes before waitUntilExit#82
NSEvent wants to merge 1 commit into
mainfrom
sentinel-fix-process-deadlock-3474175059985067787

Conversation

@NSEvent

@NSEvent NSEvent commented Jul 9, 2026

Copy link
Copy Markdown
Owner

🚨 Severity: MEDIUM
💡 Vulnerability: DoS / Resource Exhaustion via Foundation.Process deadlock. When a child process writes more data than the OS pipe buffer capacity (typically ~64KB), the child blocks on writing. If the parent application is simultaneously blocked on process.waitUntilExit() without draining the pipe, a thread deadlock occurs, leading to application freeze.
🎯 Impact: An attacker or misbehaving automation step could cause the main application thread to hang indefinitely, resulting in a Denial of Service.
🔧 Fix: Refactored multiple occurrences across the codebase (UniversalControlMouseRelay, AutomationExecutor, OBSWebSocketLiveIntegrationTests) to always read from pipe.fileHandleForReading.readDataToEndOfFile() before invoking process.waitUntilExit().
Verification: Verified via static code syntax checks and code review that the correct sequence is followed.


PR created automatically by Jules for task 3474175059985067787 started by @NSEvent

Summary by CodeRabbit

  • Bug Fixes
    • Improved subprocess handling to prevent rare hangs caused by filled output buffers.
    • Ensured process output is read before waiting for exit in multiple paths, making command execution more reliable.
    • Adjusted binary-path detection to avoid blocking while resolving external tools.

…aitUntilExit

Co-authored-by: NSEvent <44446865+NSEvent@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Multiple subprocess-handling code paths are reordered so that pipe output is fully read before calling waitUntilExit() or checking terminationStatus, addressing a potential deadlock when child process output exceeds pipe buffer capacity. A sentinel markdown file documents this vulnerability class.

Changes

Pipe Deadlock Fix

Layer / File(s) Summary
Vulnerability documentation
.Jules/sentinel.md
Adds a sentinel entry documenting the process output pipe deadlock DoS risk and mitigation guidance.
AutomationExecutor subprocess reordering
TriggerKit/Sources/TriggerKitRuntime/AutomationExecutor.swift
In runProcess and childPIDs(of:), pipe output is now read before calling waitUntilExit().
UniversalControlMouseRelay tailscale reordering
XboxControllerMapper/.../UniversalControlMouseRelay.swift
tailscalePeerHosts() reads pipe output before waiting for exit and checking termination status.
Test helper which-binary reordering
XboxControllerMapper/XboxControllerMapperTests/OBSWebSocketLiveIntegrationTests.swift
resolveBinaryPath() reads which stdout before waitUntilExit() and the termination status check.

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

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: fixing process deadlocks by draining pipes before waiting for exit.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sentinel-fix-process-deadlock-3474175059985067787

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@XboxControllerMapper/XboxControllerMapper/Services/Input/UniversalControlMouseRelay.swift`:
- Around line 1408-1410: The stderr pipe in UniversalControlMouseRelay’s
tailscale command handling is never drained, so the deadlock risk remains even
though stdout is read before waitUntilExit(). Update the Process setup around
the tailscale invocation to stop using a Pipe for process.standardError and
redirect stderr to FileHandle.nullDevice instead, following the existing pattern
used in AutomationExecutor’s childPIDs helper. Keep the stdout read and
terminationStatus check as-is.

In
`@XboxControllerMapper/XboxControllerMapperTests/OBSWebSocketLiveIntegrationTests.swift`:
- Around line 170-172: In OBSWebSocketLiveIntegrationTests, the which process is
started with try? which.run() and then its stdout is read with
readDataToEndOfFile(), which can hang if the process never launches; switch this
flow to the same do-catch/early-return pattern used in AutomationExecutor so you
only read from outPipe after a successful run and process exit. Also address the
undrained which.standardError Pipe by redirecting stderr to a non-blocking sink
or null device so the child cannot deadlock if it writes heavily to stderr.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7657df77-c615-4795-9234-4b2f6630865a

📥 Commits

Reviewing files that changed from the base of the PR and between c431981 and 6b5c7ef.

📒 Files selected for processing (4)
  • .Jules/sentinel.md
  • TriggerKit/Sources/TriggerKitRuntime/AutomationExecutor.swift
  • XboxControllerMapper/XboxControllerMapper/Services/Input/UniversalControlMouseRelay.swift
  • XboxControllerMapper/XboxControllerMapperTests/OBSWebSocketLiveIntegrationTests.swift

Comment on lines +1408 to 1410
let data = pipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
guard process.terminationStatus == 0 else { return [] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Stderr pipe not drained — same deadlock risk remains for stderr.

The stdout read is correctly reordered before waitUntilExit(), but process.standardError (line 1400) is set to a Pipe() that is never read. If the tailscale command writes more than the OS pipe buffer (~64KB) to stderr, the child blocks on the write while the parent blocks on readDataToEndOfFile() or waitUntilExit() — the exact deadlock this PR aims to prevent.

Since stderr output is not used here, redirect it to FileHandle.nullDevice, matching the pattern already used in childPIDs in AutomationExecutor.swift.

🔧 Proposed fix (line 1400, outside selected range)
-        process.standardError = Pipe()
+        process.standardError = FileHandle.nullDevice
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let data = pipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
guard process.terminationStatus == 0 else { return [] }
process.standardError = FileHandle.nullDevice
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@XboxControllerMapper/XboxControllerMapper/Services/Input/UniversalControlMouseRelay.swift`
around lines 1408 - 1410, The stderr pipe in UniversalControlMouseRelay’s
tailscale command handling is never drained, so the deadlock risk remains even
though stdout is read before waitUntilExit(). Update the Process setup around
the tailscale invocation to stop using a Pipe for process.standardError and
redirect stderr to FileHandle.nullDevice instead, following the existing pattern
used in AutomationExecutor’s childPIDs helper. Keep the stdout read and
terminationStatus check as-is.

Comment on lines +170 to 172
let data = outPipe.fileHandleForReading.readDataToEndOfFile()
which.waitUntilExit()
if which.terminationStatus == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

try? which.run() can cause indefinite block on pipe read; stderr pipe also undrained.

Line 169 uses try? to discard run errors, then line 170 calls readDataToEndOfFile(). If which.run() fails (process never starts), the pipe's write end remains open and readDataToEndOfFile() blocks indefinitely. The previous code gated the pipe read behind terminationStatus == 0, which could skip the read on failure. The other call sites in AutomationExecutor use do-catch with early returns — this one should too.

Additionally, which.standardError (line 168) is set to an undrained Pipe(), leaving the same stderr deadlock risk if the child writes >64KB to stderr.

🔧 Proposed fixes (lines 168-169, outside selected range)

Handle the run error before reading:

-        try? which.run()
+        do {
+            try which.run()
+        } catch {
+            throw XCTSkip("mediamtx not found. Install with `brew install mediamtx` or set MEDIAMTX_BIN")
+        }

Redirect stderr to null device:

-        which.standardError = Pipe()
+        which.standardError = FileHandle.nullDevice
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let data = outPipe.fileHandleForReading.readDataToEndOfFile()
which.waitUntilExit()
if which.terminationStatus == 0 {
which.standardError = FileHandle.nullDevice
do {
try which.run()
} catch {
throw XCTSkip("mediamtx not found. Install with `brew install mediamtx` or set MEDIAMTX_BIN")
}
let data = outPipe.fileHandleForReading.readDataToEndOfFile()
which.waitUntilExit()
if which.terminationStatus == 0 {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@XboxControllerMapper/XboxControllerMapperTests/OBSWebSocketLiveIntegrationTests.swift`
around lines 170 - 172, In OBSWebSocketLiveIntegrationTests, the which process
is started with try? which.run() and then its stdout is read with
readDataToEndOfFile(), which can hang if the process never launches; switch this
flow to the same do-catch/early-return pattern used in AutomationExecutor so you
only read from outPipe after a successful run and process exit. Also address the
undrained which.standardError Pipe by redirecting stderr to a non-blocking sink
or null device so the child cannot deadlock if it writes heavily to stderr.

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.

1 participant