Skip to content
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

The handle is invalid - when output is being redirected and a package attempts to write to a filestream #572

Closed
ferventcoder opened this issue Jan 22, 2016 · 6 comments

Comments

@ferventcoder
Copy link
Member

ferventcoder commented Jan 22, 2016

Related to #526 - redirecting output to a filestream seems to cause issues with the PowerShell Host. This is related to the age old POSH bug (The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. This may cause data loss) - read more about it at chocolatey-archive/puppet-chocolatey#18

Workaround: use the switch --use-system-powershell until this issue is resolved.

Update: This is actually related to PSHostRawUserInterface's use of Console. There are some things that need to be done differently with console when output is being redirected.

@ferventcoder
Copy link
Member Author

See https://chocolatey.org/packages/instantwordpress/4.5.0

You can repro as simply as trying to write a file from a write-host or echo.

echo '' >"$($exe.FullName).gui"

@ferventcoder ferventcoder changed the title The Handle is Invalid - When redirecting output to a filestream The Handle is Invalid - When output is being redirected and attempting to write to a filestream Jan 24, 2016
@ferventcoder ferventcoder changed the title The Handle is Invalid - When output is being redirected and attempting to write to a filestream The handle is invalid - when output is being redirected and a package attempts to write to a filestream Jan 29, 2016
@ferventcoder
Copy link
Member Author

Sort of related - #666

ferventcoder added a commit that referenced this issue Mar 29, 2016
When running PowerShell but redirecting the output and a package
attempts to write to a filestream, it runs into an age old PowerShell
bug "The OS handle's position is not what FileStream expected. Do not
use a handle simultaneously in one FileStream and in Win32 code or
another FileStream. This may cause data loss."

Usually this bug is known to affect PowerShell v2, but when running
PowerShell with System.Management.Automation, it appears to affect more
versions (it has been seen in PowerShell v3 and v4).

Running this in newer versions of PowerShell has been tested not to
cause any adverse affects, so it is presumed to be safe.

The fix comes from
http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/
plus information in the comments that actually allows it to work.
ferventcoder added a commit that referenced this issue Mar 29, 2016
* stable:
  (GH-460) Amended Template
  (GH-623) Extract Nuspec on Install
  (GH-674)(GH-672) Color Overrides / Fix Write-Host
  (GH-181) log selection to log file only
  (GH-181)(GH-184) Short prompt/Prompt character
  (GH-675) Template include LICENSE/VERIFICATION
  (GH-258) Rename zip to detector.zip
  (GH-572) Fix: The handle is invalid - Output Redirection
  (maint) comments
  (GH-667) Provide How To Add Profile Manually
  (GH-667) PowerShell Custom Host - Set $Profile
  (GH-666) pass Write-ChocolateyError message param
  (GH-673) Ensure Get-BinRoot Warning is Shown
  (GH-666) setup logs colored write-host w/fallback
@ferventcoder ferventcoder reopened this Mar 31, 2016
@ferventcoder
Copy link
Member Author

This is not quite resolved. And not that easy to reproduce outside of building and running through puppet or vagrant.

ferventcoder added a commit that referenced this issue Apr 2, 2016
When output is being redirected, `IConsole` needs to be able to handle
that as most of `System.Console` throws errors on methods and
properties that would normally be available.

Detect if output is being redirected by checking the StdHandle against
FileType.

Additionally, handle certain calls to find out about console related
information.
ferventcoder added a commit that referenced this issue Apr 2, 2016
When redirecting output, if anything attempts to write to the file
stream, it causes this error. The error is related to using Console
related properties and values in `PoshHostRawUserInterface`. The fix is
to use the `IConsole` adapter instead of calling `System.Console`
directly, as `IConsole` already has the ability to handle redirected
output.
ferventcoder added a commit that referenced this issue Apr 2, 2016
Getting the PowerShell internal host running on the spec tests required
providing `config.Information.ChocolateyVersion` and handling
redirected console output as part of GH-572.
ferventcoder added a commit that referenced this issue Apr 2, 2016
* stable:
  (spec) Spec tests use internal host
  (GH-572) Fix: The handle is invalid
  (GH-572) Console Adapter Redirected Output
  (spec) also write a file
@ferventcoder
Copy link
Member Author

And finally, this is good to go!

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

No branches or pull requests

2 participants