You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Console stops echoing typed keys after calling ReadKey from background thread and exiting process on Ubuntu #15991
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.
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
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
@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 😄).
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):
The parent .NET process creates the child process.
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.
Then before the child exits, the parent accesses the console, at which point it snapshots the current terminal settings as being non-echo.
Then the child exits. It resets the terminal back to snapshotted echo mode.
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.
@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).
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.
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)
—
Reply to this email directly or view it on GitHubhttps://github.com/dotnet/corefx/issues/5109#issuecomment-169726968.
Activity
[-]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[/+][-]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[/+]andyleejordan commentedon Dec 24, 2015
@DamianEdwards Thanks for reporting this (I hadn't had time to make a small repro yet). Instead of
reset, a slightly simpler workaround isstty echo, to re-enable the echo mode of the TTY.The native code is supposed restore the
termiossettings, but for some reason the saved settings already haveECHOremoved.@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 😄).
davidfowl commentedon Dec 24, 2015
Glad you filed this, I was going to as I found it while working on the benchmarks repo
DamianEdwards commentedon Dec 24, 2015
BTW, if I remove the call to Console.ReadKey from the background thread, the issue doesn't occur (in case that wasn't clear).
pallavit commentedon Jan 4, 2016
@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.
andyleejordan commentedon Jan 4, 2016
@pallavit I could provide one using
dotnet-cliif you need.pallavit commentedon Jan 4, 2016
@andschwa That will be helpful, thanks :)
stephentoub commentedon Jan 4, 2016
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):
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.
andyleejordan commentedon Jan 4, 2016
@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.
stephentoub commentedon Jan 4, 2016
Seems reasonable. @pallavit, I can take this one.
DamianEdwards commentedon Jan 4, 2016
AFAIK
dnx runis not creating a child process. It's all running in thednxprocess.7 remaining items
andyleejordan commentedon Jan 6, 2016
P.S. Simple repro still repros with replaced
System.Native.socontainingc6a6003.stephentoub commentedon Jan 7, 2016
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:
So it looks like the debugger code is closing an fd with a value 0, which is STDIN_FILENO.
Can you take a look?
stephentoub commentedon Jan 7, 2016
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
mikem8361 commentedon Jan 7, 2016
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.
stephentoub commentedon Jan 7, 2016
Thanks, Mike.
pallavit commentedon Jan 7, 2016
@stephentoub I think there is value in the fix that you put together and we should check it in as well. Thoughts?
stephentoub commentedon Jan 7, 2016
@pallavit, thanks, probably, though I still want to think about it more. The more pressing issue is getting this issue fixed first.
mikem8361 commentedon Jan 7, 2016
@stephentoub, @pallavit – I should have this fixed today. Stephen did you already have a fix?
stephentoub commentedon Jan 7, 2016
No, I have a fix for a related but different issue that involves parent/child processes.
stephentoub commentedon Jan 8, 2016
Fixed by dotnet/coreclr#2561
andyleejordan commentedon Jan 12, 2016
Confirmed fixed in our scenario with latest build 23712. Thank you very much @mikem8361, @stephentoub, and @pallavit! ☀️