Skip to content

With UseWindowsService, when main thread exits, service is left running and can't be stopped #2836

Closed

Description

@davidmatson

Describe the bug

If a host uses UseWindowsService and its If Program.Main returns without throwing (for example, catches and logs unhandled top-level exceptions), the Windows service is left in the running status and can't be stopped - only killing the processing works.

To Reproduce

  1. Use the code below.
  2. Start the service.
  3. Try to stop the service.

Expected behavior

The service is either not started or stops successfully.

Actual behavior

The service is stuck running and cannot be stopped.

Additional context

Program.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    public static void Main()
    {
        try
        {
            new HostBuilder()
                .ConfigureServices(s =>
                {
                    s.AddHostedService<TestService>();
                })
                .UseWindowsService()
                .Build()
                .Run();
        }
        catch
        {
            // Log or something similar; anything other than rethrowing/throwing...
        }
    }

    class TestService : IHostedService
    {
        public Task StartAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}

Activity

  1. davidmatson commented on Jan 8, 2020

    @davidmatson
    Author

    As a more real-world example, here's a Program.Main I'm using in practice that triggers this behavior:

    public static int Main()
    {
        try
        {
            Run(); // HostBuilder Run etc here...
            return 0;
        }
        catch (Exception exception)
        {
            Console.Error.WriteLine(exception);
            return exception.HResult;
        }
    }

    (Which happens to return a failed exit code from the process without having an unhandled exception set to the .NET runtime.)

  2. added this to the Backlog milestone on Jan 15, 2020
  3. analogrelay commented on Apr 24, 2020

    @analogrelay

    Triage: We create a new thread in

    new Thread(Run).Start(); // Otherwise this would block and prevent IHost.StartAsync from finishing.
    and don't make it a background thread. That's probably why it's not shutting down properly. Should be an easy fix: Just make the thread a background thread.

  4. added
    good first issue
    help wanted
    on Apr 24, 2020
  5. ghost locked as resolved and limited conversation to collaborators on Jun 28, 2020
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

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions