Skip to content

fix: replace try! with try? for stdout/stderr writes in ProcessIO - #1784

Merged
jglogan merged 1 commit into
apple:mainfrom
SEPURI-SAI-KRISHNA:fix/processio-force-write-crash
Jun 23, 2026
Merged

fix: replace try! with try? for stdout/stderr writes in ProcessIO#1784
jglogan merged 1 commit into
apple:mainfrom
SEPURI-SAI-KRISHNA:fix/processio-force-write-crash

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

In ProcessIO.swift, the readability handlers for stdout and stderr used
try! when writing data to the output file handles. If the pipe is
broken (e.g. the terminal is closed mid-execution), this crashes the
entire process instead of gracefully continuing.

Changed both occurrences to try? so a failed write is silently dropped
the process and its exit code are still correctly handled.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

@jglogan jglogan 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.

@SEPURI-SAI-KRISHNA Thank you for the contribution! Please have a look at the comments and let me know what you think.

return
}
try! pout.write(contentsOf: data)
try? pout.write(contentsOf: data)

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.

Thanks for catching these force-try cases.

I think we'd be better served in this case if we treat these failed writse like an EOF:

Suggested change
try? pout.write(contentsOf: data)
do {
try pout.write(contentsOf: data)
} catch {
rout.readabilityHandler = nil
cc.yield()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@

return
}
try! perr.write(contentsOf: data)
try? perr.write(contentsOf: data)

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.

Suggested change
try? perr.write(contentsOf: data)
do {
try perr.write(contentsOf: data)
} catch {
rerr.readabilityHandler = nil
cc.yield()
}

@SEPURI-SAI-KRISHNA
SEPURI-SAI-KRISHNA force-pushed the fix/processio-force-write-crash branch from a1f02be to 1c833f3 Compare June 23, 2026 04:31
@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor Author

@jglogan try? silently drops the error, but treating a failed write as EOF is the right behavior (stop reading, signal completion). Let me apply it.

@SEPURI-SAI-KRISHNA
SEPURI-SAI-KRISHNA force-pushed the fix/processio-force-write-crash branch from 1c833f3 to bc4c9e7 Compare June 23, 2026 04:43
@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor Author

@jglogan please check now, i have made the changes.

@github-actions

Copy link
Copy Markdown

Code Coverage

Tier Line Coverage
Unit 33.34%
Integration 23.02%
Combined 55.51%

@jglogan jglogan 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.

@SEPURI-SAI-KRISHNA Thank you for the fix! It's nice to chip away at this technical debt.

And thanks for submitting these as individual PRs - it's easier to review and approve them individually (I'm still thinking about the app-root one, so having it as a separate change means it doesn't hold up these).

@jglogan
jglogan merged commit 0e3651b into apple:main Jun 23, 2026
3 checks passed
@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor Author

@jglogan Appreciate your support and patience. Thanks for sharing your thoughts on separate PR. I follow this across GIT which I consider a good practice rather than bulky PR with huge code changes. Would definitely love to contribute further.

stephenlclarke pushed a commit to stephenlclarke/container that referenced this pull request Jun 24, 2026
…ple#1784)

- In `ProcessIO.swift`, the readability handlers for stdout and stderr
  used `try!` when writing data to the output file handles. This would
  cause crashes If the pipe is broken such that the force-try executes.
- Changed to handle a failed write similarly to an EOF.
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.

3 participants