Skip to content

Console stops echoing typed keys after calling ReadKey from background thread and exiting process on Ubuntu #15991

Closed

Description

@DamianEdwards

Repro application is the ASP.NET benchmarks app at https://github.com/aspnet/benchmarks/tree/dev/src/Benchmarks

I'm creating a background thread in order to do non-blocking reads of keys as Console.KeyAvailable isn't in .NET Core yet. Once the app exits however (after Ctrl+C) no keystrokes show up in the console. The keystrokes are still being sent to the shell as commands can be executed. Normality is restored after executing reset.

I attempted to resolve it by changing the while loop in the bg thread to return once the app.Run() had returned and joining it with the main thread, and while it exits fine, the issue remained.

The same app behaves as expected on Windows.

Repro steps:

  1. Clone https://github.com/aspnet/benchmarks
  2. Ensure latest DNX for Core CLR: dnvm install latest -r coreclr -arch x64 -u
  3. Switch to latest DNX: dnvm use 1.0.0-rc2-[version installed above] -r coreclr
  4. Navigate to ~/src/Benchmarks in repo
  5. Restore packages: dnu restore
  6. Run application: dnx run
  7. Hit the spacebar to see console interaction and output from application background thread works
  8. Hit Ctrl+C, application will shutdown
  9. Try typing in the console now, no keystrokes are echoed

Activity

  1. changed the title [-]Console stops echoing typed keys after calling ReadKey from bg thread on Ubuntu[/-] [+]Console stops echoing typed keys after calling ReadKey from background thread on Ubuntu[/+] on Dec 23, 2015
  2. changed the title [-]Console stops echoing typed keys after calling ReadKey from background thread on Ubuntu[/-] [+]Console stops echoing typed keys after calling ReadKey from background thread and exiting process on Ubuntu[/+] on Dec 23, 2015
  3. andyleejordan commented on Dec 24, 2015

    @andyleejordan
    Member

    @DamianEdwards Thanks for reporting this (I hadn't had time to make a small repro yet). Instead of reset, a slightly simpler workaround is stty echo, to re-enable the echo mode of the TTY.

    The native code is supposed restore the termios settings, but for some reason the saved settings already have ECHO removed.

    @pallavit We're encountering this too in our console, it'd be nice not to have to work around it (as I just eliminated the need for a custom native host... so we're almost down to no shim to launch the whole thing 😄).

  4. davidfowl commented on Dec 24, 2015

    @davidfowl
    Member

    Glad you filed this, I was going to as I found it while working on the benchmarks repo

  5. DamianEdwards commented on Dec 24, 2015

    @DamianEdwards
    MemberAuthor

    BTW, if I remove the call to Console.ReadKey from the background thread, the issue doesn't occur (in case that wasn't clear).

  6. pallavit commented on Jan 4, 2016

    @pallavit
    Contributor

    @DamianEdwards Would be possible to provide a repro outside of dnx? If this is a corefx bug we should be able to independently repro it.

  7. andyleejordan commented on Jan 4, 2016

    @andyleejordan
    Member

    @pallavit I could provide one using dotnet-cli if you need.

  8. pallavit commented on Jan 4, 2016

    @pallavit
    Contributor

    @andschwa That will be helpful, thanks :)

  9. stephentoub commented on Jan 4, 2016

    @stephentoub
    Member

    The problem (at least the one I'm able to repro locally) is due to having multiple processes associated with the same terminal (I assume dnx run / dotnet-cli are creating child processes):

    1. The parent .NET process creates the child process.
    2. The child process accesses Console (before the parent process does). In doing so, it snapshots the current terminal settings as echo mode, and the terminal is put into non-echo mode.
    3. Then before the child exits, the parent accesses the console, at which point it snapshots the current terminal settings as being non-echo.
    4. Then the child exits. It resets the terminal back to snapshotted echo mode.
    5. Then the parent exits. It resets the terminal back to the snapshotted non-echo mode.

    This all implies that either a) we need to snapshot when the process starts rather than when the Console is first used, or b) we should always reset back to some presumed correct state (e.g. echo'ing) rather than snapshotting the original settings. Even if (a) was possible, I'm not sure it really solves the problem, as the child process could then end up snapshotting what the parent changed things to and exit after the parent exits... and with System.Console.dll being a separate implementation from coreclr, we don't have a good way to guarantee execute code at app start-up. Which suggests we go with option (b), unless there are other options I'm not considering.

  10. andyleejordan commented on Jan 4, 2016

    @andyleejordan
    Member

    @stephentoub What about c) snapshot like now, and then restore the snapshot with the inverse of what was changed, instead of just some presumed correct state.

    That is, save the settings, disable echo, then restore the settings, then explicitly re-enable echo. That way we save/restore most of the settings, and only possibly overwrite the two settings we modify (ECHO and ICANON).

    Just a thought. Might not be worth the trouble.

  11. stephentoub commented on Jan 4, 2016

    @stephentoub
    Member

    snapshot like now, and then restore the snapshot with the inverse of what was changed, instead of just some presumed correct state

    Seems reasonable. @pallavit, I can take this one.

  12. DamianEdwards commented on Jan 4, 2016

    @DamianEdwards
    MemberAuthor

    AFAIK dnx run is not creating a child process. It's all running in the dnx process.

  13. 7 remaining items

  14. andyleejordan commented on Jan 6, 2016

    @andyleejordan
    Member

    P.S. Simple repro still repros with replaced System.Native.so containing c6a6003.

  15. stephentoub commented on Jan 7, 2016

    @stephentoub
    Member

    Thanks, @andschwa. That was helpful. As it turns out, there's a much simpler repro: just using Console.ReadKey() from Main. I wasn't able to repro it initially as I hadn't built coreclr in a while, but after sync'ing and rebuilding, I was able to repro it both with corerun and dotnet.

    @mikem8361, I think this is somehow being caused by some of your recent debugger-related changes. When we set up the console stuff, we use C's atexit to register a callback that'll run at exit and reset the terminal using tcsetattr(STDIN_FILENO, ...). But that tcsetattr call is failing with an EBADF errno, meaning a bad file descriptor. And it does appear that STDIN_FILENO (0) is being closed prior to this getting run. I debugged through it, and I see this stack:

    #0  close () at ../sysdeps/unix/syscall-template.S:81
    dotnet/corefx#1  0x00007ffff6315be6 in TwoWayPipe::Disconnect (this=0x651cb0)
        at /home/stoub/Source/coreclr/src/debug/debug-pal/unix/twowaypipe.cpp:183
    dotnet/corefx#2  0x00007ffff6302978 in DbgTransportSession::Shutdown (this=0x651b00)
        at /home/stoub/Source/coreclr/src/debug/ee/../shared/dbgtransportsession.cpp:165
    dotnet/corefx#3  0x00007ffff62baa77 in ShutdownTransport () at /home/stoub/Source/coreclr/src/debug/ee/debugger.cpp:914
    dotnet/corefx#4  0x00007ffff62bebe9 in Debugger::StopDebugger (this=0x64a7d0) at /home/stoub/Source/coreclr/src/debug/ee/debugger.cpp:2573
    dotnet/corefx#5  0x00007ffff5dcfd45 in TerminateDebugger () at /home/stoub/Source/coreclr/src/vm/ceemain.cpp:4190
    dotnet/corefx#6  0x00007ffff5dcf5b7 in EEShutDownHelper (fIsDllUnloading=0) at /home/stoub/Source/coreclr/src/vm/ceemain.cpp:1961
    dotnet/corefx#7  0x00007ffff5dcffd1 in EEShutDown (fIsDllUnloading=0) at /home/stoub/Source/coreclr/src/vm/ceemain.cpp:2460
    dotnet/corefx#8  0x00007ffff5c30af4 in CorHost2::UnloadAppDomain (this=0x6104b0, dwDomainId=1, fWaitUntilDone=1)
        at /home/stoub/Source/coreclr/src/vm/corhost.cpp:3108
    dotnet/corefx#9  0x00007ffff5be63f2 in coreclr_shutdown (hostHandle=0x6104b0, domainId=1)
        at /home/stoub/Source/coreclr/src/dlls/mscoree/unixinterface.cpp:284
    dotnet/corefx#10 0x0000000000404560 in ExecuteManagedAssembly (currentExeAbsolutePath=0x608028 "/home/stoub/netcore/corerun", 
        clrFilesAbsolutePath=0x6080a8 "/home/stoub/netcore", managedAssemblyAbsolutePath=0x608068 "/home/stoub/netcore/repro.exe", 
        managedAssemblyArgc=0, managedAssemblyArgv=0x0)
        at /home/stoub/Source/coreclr/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp:360
    dotnet/corefx#11 0x00000000004037f8 in corerun (argc=2, argv=0x7fffffffe028)
        at /home/stoub/Source/coreclr/src/coreclr/hosts/unixcorerun/corerun.cpp:150
    dotnet/corefx#12 0x00000000004038b2 in main (argc=2, argv=0x7fffffffe028) at /home/stoub/Source/coreclr/src/coreclr/hosts/unixcorerun/corerun.cpp:162
    (gdb) frame 1
    dotnet/corefx#1  0x00007ffff6315be6 in TwoWayPipe::Disconnect (this=0x651cb0)
        at /home/stoub/Source/coreclr/src/debug/debug-pal/unix/twowaypipe.cpp:183
    183         close(m_outboundPipe);
    (gdb) p m_outboundPipe
    $1 = 0
    

    So it looks like the debugger code is closing an fd with a value 0, which is STDIN_FILENO.

    Can you take a look?

  16. stephentoub commented on Jan 7, 2016

    @stephentoub
    Member
  17. mikem8361 commented on Jan 7, 2016

    @mikem8361
    Contributor

    Yes, that is the problem. I recently enabled calling the cleanup/disconnect to remove the tmp pipe files created by the transport. Looks it also had this bug. I’ll get this fixes asap.

    mikem

    From: Stephen Toub [mailto:[email protected]]
    Sent: Thursday, January 7, 2016 8:56 AM
    To: dotnet/corefx [email protected]
    Cc: Mike McLaughlin [email protected]
    Subject: Re: [corefx] Console stops echoing typed keys after calling ReadKey from background thread and exiting process on Ubuntu (#5109)

    ps Even though m_outboundPipe is getting initialized to -1, it's getting cleared back to 0 here:
    https://github.com/dotnet/coreclr/blob/7fded4942276b6a5d7b230cc29fcd2e6fdab979c/src/debug/shared/dbgtransportsession.cpp#L58

    —
    Reply to this email directly or view it on GitHubhttps://github.com/dotnet/corefx/issues/5109#issuecomment-169726968.

  18. stephentoub commented on Jan 7, 2016

    @stephentoub
    Member

    Thanks, Mike.

  19. pallavit commented on Jan 7, 2016

    @pallavit
    Contributor

    @stephentoub I think there is value in the fix that you put together and we should check it in as well. Thoughts?

  20. stephentoub commented on Jan 7, 2016

    @stephentoub
    Member

    @pallavit, thanks, probably, though I still want to think about it more. The more pressing issue is getting this issue fixed first.

  21. mikem8361 commented on Jan 7, 2016

    @mikem8361
    Contributor

    @stephentoub, @pallavit – I should have this fixed today. Stephen did you already have a fix?

  22. stephentoub commented on Jan 7, 2016

    @stephentoub
    Member

    Stephen did you already have a fix?

    No, I have a fix for a related but different issue that involves parent/child processes.

  23. stephentoub commented on Jan 8, 2016

    @stephentoub
    Member
  24. andyleejordan commented on Jan 12, 2016

    @andyleejordan
    Member

    Confirmed fixed in our scenario with latest build 23712. Thank you very much @mikem8361, @stephentoub, and @pallavit! ☀️

  25. transferred this issue fromdotnet/corefxon Jan 31, 2020
  26. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  27. ghost locked as resolved and limited conversation to collaborators on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Consoleos-linux

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions