Bug 10106 - Console.OpenStandardInput() fails when STD_INPUT_HANDLE is INVALID_HANDLE_VALUE
Summary: Console.OpenStandardInput() fails when STD_INPUT_HANDLE is INVALID_HANDLE_VALUE
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-02-06 22:29 UTC by Jonathan Pryor
Modified: 2013-02-06 22:30 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

invalid-handle.zip (1.71 KB, application/zip)
2013-02-06 22:29 UTC, Jonathan Pryor

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 10106 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jonathan Pryor 2013-02-06 22:29:02 UTC
Created attachment 3339 [details]

This is a Windows-specific issue.

If the caller/parent process mono calls SetStdHandle(STD_INPUT_HANDLE, INVALID_HANDLE_VALUE), then mono will bomb during process startup.

To reproduce:

1. Download invalid-handle.zip
2. Extract it.
3. From CMD.EXE:

> cd path\to\invalid-handle
> NMAKE all
> NMAKE run

Expected results: To do what .NET does:

> # Clearing stdin...
> [stdout] Hello, world!
> [stdout]
> [stderr]
> # exit code: 0

Actual results: standard error contains:

> [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.Console ---> System.ArgumentException: handle
> Parameter name: Invalid.
>   at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync, Boolean isZeroSize)
>   at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (intptr,System.IO.FileAccess,bool,int,bool,bool)
>   at System.Console.Open (IntPtr handle, FileAccess access, Int32 bufferSize)
>   at System.Console.OpenStandardInput (Int32 bufferSize)
>   at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding)
>   at System.Console..cctor ()
>   --- End of inner exception stack trace ---
>   at Test.Main ()

Export MONO_LOG_LEVEL=debug, and the last useful message written to stdout is:

> Mono: Searching for 'GetConsoleOutputCP'.

Based on those two items, what appears to be happening is that Console.OpenStandardInput(int) calls Console.Open(MonoIO.ConsoleInput, FileAccess.Read, bufferSize).


MonoIO.ConsoleInput is an internal call into mono/metadata/file-io.c!ves_icall_System_IO_MonoIO_get_ConsoleInput():


The problem is that because the caller has deliberately/explicitly set STD_INPUT_HANDLE to INVALID_HANDLE_VALUE, MonoIO.ConsoleInput returns INVALID_HANDLE_VALUE, which causes the FileStream constructor to throw.

What should happen? Perhaps ves_icall_System_IO_MonoIO_get_ConsoleInput() should return NULL if GetStdHandle() returns INVALID_FILE_HANDLE?