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

Console.ReadKey throws in unexpected circumstances #59059

Open
Tracked by #64487
JamesWTruher opened this issue Sep 13, 2021 · 3 comments
Open
Tracked by #64487

Console.ReadKey throws in unexpected circumstances #59059

JamesWTruher opened this issue Sep 13, 2021 · 3 comments
Labels
area-System.Console help wanted [up-for-grabs] Good issue for external contributors needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration os-linux Linux OS (any supported distro)
Milestone

Comments

@JamesWTruher
Copy link
Contributor

Description

it appears that an exited application hangs on to console handles after it exits
The application that I'm seeing in is the erlang program iex.

here's the code that shows off the issue.

using System;
using System.Diagnostics;
class Sample
{
    public static void Main()
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler(handler);
        ProcessStartInfo psi = new ProcessStartInfo("/usr/local/bin/iex");
        //psi.RedirectStandardInput = true;
        do
        {
            using(var p = Process.Start(psi)) {
                p?.WaitForExit();
            }
            Console.WriteLine("\nproc has ended.\npress a key to run it again - 'q' to stop");
        }
        while (Console.ReadKey(true).KeyChar != 'q');
    }

    protected static void handler(object? sender, ConsoleCancelEventArgs args)
    {
        args.Cancel = true;
    }
}

run the program and press CTRL-C twice (timing doesn't seem to matter here - quickly or slowly doesn't affect the outcome).

james@JamesiMac20 p025 % dotnet run
Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit] [dtrace]

Interactive Elixir (1.12.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
^C
proc has ended
press a key to run it again - 'q' to stop
Unhandled exception. System.IO.IOException: The process cannot access the file because it is being used by another process.
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Interop.CheckIo(Int64 result, String path, Boolean isDirectory, Func`2 errorRewriter)
   at System.IO.StdInReader.ReadKey()
   at System.IO.StdInReader.ReadKey(Boolean& previouslyProcessed)
   at System.IO.SyncTextReader.ReadKey(Boolean& previouslyProcessed)
   at System.ConsolePal.ReadKey(Boolean intercept)
   at System.Console.ReadKey(Boolean intercept)
   at Sample.Main() in /Users/james/src/projects/signal/p025/Program.cs:line 18

Configuration

net6.0
osx-x64
Model Name:	iMac
Model Identifier:	iMac20,1
Processor Name:	10-Core Intel Core i9
Processor Speed:	3.6 GHz
System Firmware Version:	1554.140.20.0.0 (iBridge: 18.16.14759.0.1,0)

Regression?

not sure

Other information

if the line which redirects StandardInput (or StandardOutput) is not commented out the error does not occur. i'm confused as to what's going on, the process has exited, but the error suggests that something is hanging on to a console handle.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Console untriaged New issue has not been triaged by the area owner labels Sep 13, 2021
@ghost
Copy link

ghost commented Sep 13, 2021

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

it appears that an exited application hangs on to console handles after it exits
The application that I'm seeing in is the erlang program iex.

here's the code that shows off the issue.

using System;
using System.Diagnostics;
class Sample
{
    public static void Main()
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler(handler);
        ProcessStartInfo psi = new ProcessStartInfo("/usr/local/bin/iex");
        //psi.RedirectStandardInput = true;
        do
        {
            using(var p = Process.Start(psi)) {
                p?.WaitForExit();
            }
            Console.WriteLine("\nproc has ended.\npress a key to run it again - 'q' to stop");
        }
        while (Console.ReadKey(true).KeyChar != 'q');
    }

    protected static void handler(object? sender, ConsoleCancelEventArgs args)
    {
        args.Cancel = true;
    }
}

run the program and press CTRL-C twice (timing doesn't seem to matter here - quickly or slowly doesn't affect the outcome).

james@JamesiMac20 p025 % dotnet run
Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit] [dtrace]

Interactive Elixir (1.12.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
^C
proc has ended
press a key to run it again - 'q' to stop
Unhandled exception. System.IO.IOException: The process cannot access the file because it is being used by another process.
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Interop.CheckIo(Int64 result, String path, Boolean isDirectory, Func`2 errorRewriter)
   at System.IO.StdInReader.ReadKey()
   at System.IO.StdInReader.ReadKey(Boolean& previouslyProcessed)
   at System.IO.SyncTextReader.ReadKey(Boolean& previouslyProcessed)
   at System.ConsolePal.ReadKey(Boolean intercept)
   at System.Console.ReadKey(Boolean intercept)
   at Sample.Main() in /Users/james/src/projects/signal/p025/Program.cs:line 18

Configuration

net6.0
osx-x64
Model Name:	iMac
Model Identifier:	iMac20,1
Processor Name:	10-Core Intel Core i9
Processor Speed:	3.6 GHz
System Firmware Version:	1554.140.20.0.0 (iBridge: 18.16.14759.0.1,0)

Regression?

not sure

Other information

if the line which redirects StandardInput (or StandardOutput) is not commented out the error does not occur. i'm confused as to what's going on, the process has exited, but the error suggests that something is hanging on to a console handle.

Author: JamesWTruher
Assignees: -
Labels:

area-System.Console, untriaged

Milestone: -

@StephenBonikowsky
Copy link
Member

Related to PowerShell/PowerShell#11215

@jozkee jozkee added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner labels Sep 15, 2021
@jozkee jozkee added this to the 7.0.0 milestone Sep 15, 2021
@jozkee
Copy link
Member

jozkee commented Sep 15, 2021

Not a regression, it reproes in 3.1 and 5.0, but definitely we need to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Console help wanted [up-for-grabs] Good issue for external contributors needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration os-linux Linux OS (any supported distro)
Projects
None yet
Development

No branches or pull requests

5 participants