question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

External terminal not registering Ctrl+C with .NET Core

See original GitHub issue
  • VSCode Version: 1.38.1
  • OS Version: Win10 (10.0.17763)

The following used to work in .NET Core 2.2 app with a debug profile for the externalTerminal in earlier versions of VSC (or OmniSharp if that’s where the problem actually lies here), but it doesn’t work with new 3.0 framework bits and the latest public VSC.

Debug a .NET Core app in the externalTerminal using either the Microsoft.NET.Sdk.Web or Microsoft.NET.Sdk.Worker project SDK that calls UseConsoleLifetime on the host builder so that the app should listen for Ctrl+C to stop the app. The app doesn’t stop.

Steps to Reproduce:

  1. Create a .NET Core console app with UseConsoleLifetime

    public class Program
    {
        public static async Task Main(string[] args)
        {
            using (var host = Host.CreateDefaultBuilder(args)
                .UseConsoleLifetime()
                .Build())
            {
                // Start the host
                await host.StartAsync();
    
                // Wait for the host to shutdown
                await host.WaitForShutdownAsync();
            }
        }
    }
    

    The preceding code works if the app is run with dotnet run in a command shell …

    Capture1
  2. Use a console debugger profile and run the same app in the externalTerminal

    {
       "version": "0.2.0",
       "configurations": [
           {
               "name": ".NET Core Launch (console)",
               "type": "coreclr",
               "request": "launch",
               "preLaunchTask": "build",
               "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/ConsoleSample.dll",
               "args": [],
               "cwd": "${workspaceFolder}",
               "stopAtEntry": false,
               "console": "externalTerminal"
           }
        ]
    }
    

    The app will not respond to Ctrl+C. In prior versions (.NET Core 2.2), Ctrl+C would work when using the externalTerminal in this scenario.

I asked ASP.NET Core engineering, and they thought it might be on the VSC-side of the equation given that Ctrl+C works when the app is run in a command shell. See item response 4 at https://github.com/aspnet/AspNetCore.Docs/issues/12841#issuecomment-533206352.

I put up a repro project at https://github.com/guardrex/ConsoleSample.

The sample app that I put up also has a keystroke-monitoring service that logs presses of the w key. That code works. Running in the externalTerminal, the app reports that it hears the w key clicks, while the app doesn’t respond to Ctrl+C. Sure, the app could listen for Ctrl+C there and shut the app down, but that’s not the point … that would be a hack. UseConsoleLifetime should work the way that this app works in a normal command shell (I think 😄).

Does this issue occur when all extensions are disabled? That can’t be checked because the configured debug type is coreclr, which isn’t supported without either (or both) C# OmniSharp. In fact, I’m not sure if this bug report should be filed here or over on https://github.com/OmniSharp/omnisharp-vscode.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Tyriarcommented, Sep 23, 2019

@guardrex you can download the zip, close all your current windows and then open the zip executable. If you try running the 64-bit version while the other is running it will probably just open another 32-bit window.

0reactions
connor4312commented, Nov 11, 2020

I can repro this, but have no idea why it is happening. Simple program:

process.on('SIGINT', () => {
  console.log('got SIGINT');
  process.exit(0);
})

setInterval(() => {}, 1000)

Running the same command that VS Code is running from a cmd.exe instance manually, it works fine. But in an external terminal, no SIGINT is sent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I trap Ctrl+C (SIGINT) in a C# console app?
I have found that that Console.CancelKeyPress will stop working if the console is closed. Running an app under mono/linux with systemd, or if...
Read more >
CTRL+C and CTRL+BREAK Signals - Windows Console
In this article. The CTRL + C and CTRL + BREAK key combinations receive special handling by console processes. By default, when a...
Read more >
Debug C++ in Visual Studio Code
When debugging with LLDB, if the Terminal window is closed while in break mode, debugging does not stop. Debugging can be stopped by...
Read more >
Keybinding Ctrl + Shift + C opens a new external terminal in ...
In Visual Studio Code / Codium on Linux, the key binding Ctrl+Shift+c opens a new external terminal. This does not work together with...
Read more >
Hosting services in .NET Core console application
NET Core console applications with IHost and HostBuild to take advantage of IHostedService, graceful shutdown, dependency injection, logging ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found