Skip to content

Fix Process.exec stream redirection on Windows#14986

Merged
straight-shoota merged 5 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-process-exec
Sep 16, 2024
Merged

Fix Process.exec stream redirection on Windows#14986
straight-shoota merged 5 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-process-exec

Conversation

@HertzDevil
Copy link
Contributor

The following should print the compiler help message to the file foo.txt:

File.open("foo.txt", "w") do |f|
  Process.exec("crystal", output: f)
end

It used to work on Windows in Crystal 1.12, but is now broken since 1.13. This is because LibC._wexecvp only inherits file descriptors in the C runtime, not arbitrary Win32 file handles; since we stopped calling LibC._open_osfhandle, the C runtime knows nothing about any reopened standard streams in Win32. Thus the above merely prints the help message to the standard output.

This PR creates the missing C file descriptors right before LibC._wexecvp. It also fixes a different regression of #14947 where reconfiguring STDIN.blocking always fails.

Related: #14422

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:system kind:regression Something that used to correctly work but no longer works labels Sep 7, 2024
@HertzDevil HertzDevil marked this pull request as draft September 8, 2024 06:06
@HertzDevil HertzDevil marked this pull request as ready for review September 8, 2024 09:23
Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Could we get a spec for this? 🤔

@straight-shoota straight-shoota modified the milestones: 1.14.0, 1.13.3 Sep 9, 2024
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@straight-shoota straight-shoota merged commit 9134f9f into crystal-lang:master Sep 16, 2024
@HertzDevil HertzDevil deleted the bug/windows-process-exec branch September 16, 2024 13:41
straight-shoota added a commit that referenced this pull request Sep 17, 2024
The following should print the compiler help message to the file `foo.txt`:

```crystal
File.open("foo.txt", "w") do |f|
  Process.exec("crystal", output: f)
end
```

It used to work on Windows in Crystal 1.12, but is now broken since 1.13. This is because `LibC._wexecvp` only inherits file descriptors in the C runtime, not arbitrary Win32 file handles; since we stopped calling `LibC._open_osfhandle`, the C runtime knows nothing about any reopened standard streams in Win32. Thus the above merely prints the help message to the standard output.

This PR creates the missing C file descriptors right before `LibC._wexecvp`. It also fixes a different regression of #14947 where reconfiguring `STDIN.blocking` always fails.

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@crysbot
Copy link
Collaborator

crysbot commented Sep 19, 2024

This pull request has been mentioned on Crystal Forum. There might be relevant details there:

https://forum.crystal-lang.org/t/crystal-1-13-3-is-released/7191/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:regression Something that used to correctly work but no longer works platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:system

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants