runtime: dotnet CLI commands no longer work in .NET 7/8 with detached console

Describe the bug

We have some Azure pipelines that run some .NET commands through Wine on Linux agents. Upon trying to upgrade from .NET 6 to .NET 8, we can no longer run any dotnet command from Wine (for example, /opt/wine-stable/bin/wine64 $HOME/win-dotnet/dotnet.exe --version) on our pipelines. Instead we get an exception:

Unhandled exception. System.IO.IOException: Invalid access.
   at System.ConsolePal.SetConsoleOutputEncoding(Encoding enc)
   at System.Console.set_OutputEncoding(Encoding value)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)

I’m using dotnet --version as an example in this bug, but it happens for dotnet *anything*.

  • Running /path/to/wine64 /path/to/dotnet.exe --version from the Azure agent pipeline job works on .NET 5/6
  • Running /path/to/wine64 /path/to/dotnet.exe --version in a regular Terminal on the same agent works on .NET 5/6/7/8
  • Native Linux .NET 8 commands work from the pipeline, if dotnet-sdk is installed

In Wine, we get the following warning when running from an Azure agent: 0024:warn:console:console_ioctl Incorrect access to Shell-no-window console (ioctl=50403c)

Which possibly was not a problem until hitting this code that was added: https://github.com/dotnet/sdk/commit/697d62c7fdec87ff1b2c81a47e02b1294dab9e7b

To Reproduce

Prereq: an Azure agent that runs on Linux

  1. Install the latest version of wine-stable on the agent
  2. Download and unzip the latest version of .NET 7 or .NET 8 for Windows somewhere on the agent, for example $HOME/win-dotnet
  3. In a pipeline script targeting the agent, run /opt/wine-stable/bin/wine64 $HOME/win-dotnet/dotnet.exe --version
  4. Run pipeline -> hit exception.

Example pipeline file: https://gist.github.com/Wedgetable/e75b08ae1eb3b2fb10cc2a53a9953745

Exceptions (if any)

Unhandled exception. System.IO.IOException: Invalid access.
   at System.ConsolePal.SetConsoleOutputEncoding(Encoding enc)
   at System.Console.set_OutputEncoding(Encoding value)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)

Further technical details

  • Agent version: 3.232.1
  • OS: Ubuntu 20.04.6 LTS 64-bit
  • wine64 version: wine-9.0
  • .NET SDK for Windows version(s): 7.0.405, 8.0.101

Not sure if this is a .NET bug, a Wine bug, or an Azure pipelines agent bug. We are currently getting technical support from CodeWeavers on this issue as well.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 21 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Moved to dotnet/runtime and updated the title to make it clear that this is not a Wine-specific problem.

If we want to make console apps and Console.OutputEncoding API work well when the process is launched with DETACHED_PROCESS, does it need to be better fixed in System.Console?

On Windows, Console.OutputEncoding API does two things:

  • sets the encoding using SetConsoleOutputCP
  • sets the encoding to use by the System.Console to encode Unicode to set.

It is still valid to do the second part for redirected output.

If we go with the proposed fix to catch exceptions around Console.OutputEncoding, the second step is not going to happen, and the redirected output will be encoded using some random encoding instead of UTF-8 that was explicitly set.

I think the correct fix should be in System.Console. System.Console should not call SetConsoleOutputCP when the output is redirected or ignore the invalid handle error returned by SetConsoleOutputCP .

cc @adamsitnik

As I said, the bug is in the runtime. This should not throw with redirected standard output, but does:

NativeMethods.FreeConsole();
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("Output CP = " + NativeMethods.GetConsoleOutputCP());

I stuck NativeMethods.FreeConsole(); on the top because the reproduction is difficult otherwise, but it is possible. The wine setup is giving you something more like ssh hostname dotnet --version instead of dotnet --version. Your console process doesn’t have a console yet.

Run the attached project with dotnet run > log 2> log2 and the exception appears in log2.

r38613.zip

Calling SetConsoleOutputCP() for a redirected handle is a bug even when it doesn’t crash. It will mess up the caller’s console by changing the glyph mappings. Call isatty first to check for redirected standard output/error.

ignore INVALID_ACCESS?

It would be ok with me.

If we take the workaround in the SDK, we are saying that all uses of Console.OutputEncoding setter should better have try/catch around it. It does not make sense to me. For example, are you going to hit the problem for similar call in Roslyn next?

I think that the fix in the console APIs can be very low-risk and simple. I would just ignore the “Invalid Handle” error code from SetConsoleOutputCP API.

@nick-beer we’d gladly accept such a PR - @marcpopMSFT was mainly getting at that the .NET team doesn’t directly support Wine, so we would prefer to use our team-time on things that are related to the platforms we do support.