Bug 12552 - Console.ReadLine() does not read from console if stdout (but not stdin) is redirected
Summary: Console.ReadLine() does not read from console if stdout (but not stdin) is re...
Status: CONFIRMED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 2.8.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-06-04 23:51 UTC by Greg Najda
Modified: 2017-10-30 22:00 UTC (History)
5 users (show)

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

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 12552 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:
Status:
CONFIRMED

Description Greg Najda 2013-06-04 23:51:17 UTC
Run the following program copied from my other bug report with stdout redirected but not stdin. On .NET it waits for a keypress, then exits.

C:\Users\Greg\Documents\Programming\consoletest\consoletest\bin\Debug>consoletest.exe > output.txt

output.txt contains:

Press a key
Key pressed: 78



On Mono in Linux, it exits immediately.

greg@Kubuntu:~/dev/consoletest/consoletest/bin/Debug$ mono consoletest.exe > output.txt
greg@Kubuntu:~/dev/consoletest/consoletest/bin/Debug$ cat output.txt
Press a key
Key pressed: 0
greg@Kubuntu:~/dev/consoletest/consoletest/bin/Debug$ mono --version
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.2)

This also prevents using programs that use ReadKey() with utilities like tee because piping to tee redirects stdout.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consoletest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press a key");
            bool exceptionThrown = false;
            ConsoleKeyInfo key = new ConsoleKeyInfo();
            try
            {
                key = Console.ReadKey(intercept: true);
            }
            catch (InvalidOperationException)
            {
                // This is the documented behavior when reading from a non-console stdin
                Console.WriteLine("InvalidOperationException thrown.");
                exceptionThrown = true;
            }
            if (!exceptionThrown)
            {
                // Mono instead returns a ConsoleKeyInfo with a Key value of 0.
                Console.WriteLine("Key pressed: {0}", (int)key.Key);
            }
        }
    }
}
Comment 1 Greg Najda 2013-06-09 18:02:14 UTC
Console.ReadKey(bool intercept) calls ConsoleDriver.ReadKey(bool intercept) which calls IConsoleDriver.ReadKey(bool intercept). The static constructor of ConsoleDriver determines which implementation of IConsoleDriver is used for console operations.

static ConsoleDriver ()
{
	// Put the actual new statements into separate methods to avoid initalizing
	// three classes when only one is needed.
	if (!IsConsole) {
		driver = CreateNullConsoleDriver ();
	} else if (Environment.IsRunningOnWindows) {
		driver = CreateWindowsConsoleDriver ();
	} else {
		string term = Environment.GetEnvironmentVariable ("TERM");

		// Perhaps we should let the Terminfo driver return a
		// success/failure flag based on the terminal properties
		if (term == "dumb"){
			is_console = false;
			driver = CreateNullConsoleDriver ();
		} else
			driver = CreateTermInfoDriver (term);
	}
}


IsConsole effectively returns (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput))

In other words, if stdout or stdin or both are not a console, then NullConsoleDriver is used. This prevents you from doing input console operations if stdout is redirected. A possible fix would be to have TermInfoDriver and WindowsConsoleDriver correctly handle the case where stdin or stdout is not a console and use TermInfoDriver/WindowsConsoleDriver even if one of stdin or stdout is not a console. Maybe even get rid of NullConsoleDriver entirely and only use TermInfoDriver/WindowsConsoleDriver and have them be aware of whether stdin and stdout are actually a console.
Comment 2 Miguel de Icaza [MSFT] 2015-06-12 12:32:03 UTC
This needs a new driver InputConsoleProvider that would allow input, but remove the Terminfo features
Comment 3 Vladimir Kargov 2016-04-09 09:53:30 UTC
Seems like this issue reveals itself in ASP.NET Core apps since stdout/stderr are always redirected:
https://github.com/dotnet/cli/blob/7e73e5c/src/dotnet/commands/dotnet-run/RunCommand.cs#L172

Example of usage that would cause the problem:
https://github.com/aspnet/benchmarks/blob/7b996e6/src/Benchmarks/Program.cs#L66
Comment 4 Ludovic Henry 2017-10-30 22:00:51 UTC
I can reproduce with Mono 5.8.0.28 (2017-10/863ab3cb164).

I compile the repro case provided in https://bugzilla.xamarin.com/show_bug.cgi?id=12552#c0 and  run the following command:

> mcs repro.cs
> mono repro.exe | cat

The expected output is :

> Press a key
> Key pressed: 65 // After pressing "a"

The actual output is:

> Press a key
> Key pressed: 0