Skip to content

SIGTERM handling #5932

@pmcgrath

Description

@pmcgrath

Hi,
I'm seeing issues with handling sigterm on a linux system, seeing similar behaviour to 516 but on linux

Environment info
OS: ubuntu 16.04 64bit
dotnet --version is 2.1.300

As the docs indicate WaitForShutdown should handle sigterm, sigint and ctrl-c
Can see it handling sigint and ctrl-c as expected, but sigterm is not behaving as documented for me
Since sigterm is what docker will send to a container being stopped this is pretty important for graceful shutdowns with log entries

I create a standard webapi project with dotnet new webapi and altered the Program.cs as follows

using System;
using System.Linq;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;

namespace shutdown
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("About to build web host");
                using (var host = BuildWebHost(args))
                {
                    Console.WriteLine("About to start host");
                    host.Start();

                    var serverAddressFeature = host.ServerFeatures.Get<IServerAddressesFeature>();
                    Console.WriteLine($"Waiting for shutdown, listening on {String.Join(", ", serverAddressFeature.Addresses.ToArray())}");
                    host.WaitForShutdown();

                    Console.WriteLine("Shutting down");
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"Exception encountered, about to die {exception}");
                throw;
            }
            finally
            {
                Console.WriteLine("Exiting finally");
            }
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

So ctlr-c and kill -INT pid are working as expected and the exiting log lines appear

But kill pid is just resulting in the process terminating with no exiting log lines, this I did not expect and is an issue with docker where I need it to handle a sigterm

Thanks in advance
Pat

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-hostingIncludes Hostingarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions