xilun / cbwin

Launch Windows programs from "Bash on Ubuntu on Windows" (WSL)
Other
327 stars 25 forks source link

can not pipe from/to Win32 processes #6

Closed xilun closed 8 years ago

xilun commented 8 years ago

work in progress: I can now call CreateProcess with explicit handle inheritance, and redirection from/to /dev/null is now propagated to Win32 and translated to redirection from/to NUL as a special case. Other redirections will go through a socket.

xilun commented 8 years ago

With 5dc7a4b: somehow works for the general case (i.e. not only /dev/null) redirections for stdin and stdout, but not perfectly yet. Some debugging to do...

xilun commented 8 years ago

I'm not sure the bug is in my code, I will do some additional tests but I already asked on the WSL GitHub bugtracker

xilun commented 8 years ago

I've found a workaround and will commit it shortly.

xilun commented 8 years ago

It seems that if the process that created a Winsocket inherited by others closes all its local handles before the launched processes close all their inherited handles (or implicitly release them when they terminate), it will not eventually linger gracefully when the last handle disappears (system wide). I still have some tests to do but for now this theory seems to match the observed behavior. If that theory is true, commit 291772b should make redirections work reliably for all Win32 workloads that do not create background processes inheriting the standard handles (for what is implemented now; stderr is not implemented yet). So don't use wstart with redirections and expect them to be reliable (maybe we should make it refuse to redirect?)

I must think more precisely about what should happen in this case, because the WSL caller is not supposed to continue to execute after the invoked Win32 process has terminated and flushed its own output, but OTOH if everybody lived in the WSL world (without any outbash&caller machinery) and we redirected to a regular file the behavior would be that the file continues to be filled by the background process... But that seems to be somehow tricky to implement if we have a redirection machinery: we would need to detect the Win32 situation in the first place -- I don't even know how... -- then fork and detach the WSL process so that it can continue the redirection in the background -- but then I'm not even sure the WSL subsys will let it run forever, IIRC it might be killed when the console is closed or something like that...

xilun commented 8 years ago

Will reopen another bug for advanced background issues.