Skip to content

Console.ReadKey throws in unexpected circumstances #59059

Open

Description

@JamesWTruher

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.

Activity

  1. ghost added
    untriaged
    on Sep 13, 2021
  2. StephenBonikowsky commented on Sep 15, 2021

    @StephenBonikowsky
    Contributor
  3. added
    needs-further-triage
    and removed
    untriaged
    on Sep 15, 2021
  4. added this to the 7.0.0 milestone on Sep 15, 2021
  5. jozkee commented on Sep 15, 2021

    @jozkee
    Member

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

  6. modified the milestones: 7.0.0, Future on Jul 9, 2022
  7. added
    help wanted
    os-linux
    on Nov 13, 2023
  8. jeffhandley commented on Feb 9, 2025

    @jeffhandley
    Member

    This is marked as help wanted [up-for-grabs] Good issue for external contributors for assistance getting to a root cause and possible fix.

  9. removed
    needs-further-triage
    on Feb 9, 2025
  10. JamesWTruher commented on Apr 20, 2025

    @JamesWTruher
    ContributorAuthor

    @jeffhandley I've tried this again with v9.0.2 and this doesn't repro any longer.
    I'm not one to look a gift horse in the mouth, but I sure wish I knew how this got fixed :).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Consolehelp wantedos-linux

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions