dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.47k stars 10.03k forks source link

When hosting as Windows Service, Singletons are disposed AFTER the Service has reported to be stopped #39264

Closed lucas-natraj closed 2 years ago

lucas-natraj commented 2 years ago

Is there an existing issue for this?

Describe the bug

Pretty much what the title describes. It appears that the Service does wait for IHostedService implementations to finish before reporting the status as stopped, but NOT for IDisposable (or IASyncDisposable) singletons.

This has an awkward side-effect when Restarting the WindowsService as the previous process singleton instances may still be around when new singletons are instantiated in the newly launched process.

Expected Behavior

I would have expected that the service would wait until all singletons are disposed (within some timeout-limit) before reporting the Service as Stopped.

Steps To Reproduce

The repository at https://github.com/lucas-natraj/dueling-services represents a minimal setup to reproduce the issue.

  1. build the solution -> resulting in the executable binary Lastly.exe
  2. register the exe as a windows service using sc.exe create Lastly binPath= {path-to-lastly.exe}
  3. install seq for view aggregated logs. or monitor the log***.txt files in the executable directory.
  4. start the service (via TaskManager)
  5. hit the webapi endpoint by visiting http://localhost:5000/ping.
  6. Restart the window service (via TaskManager)
  7. view logs

The aggregated logs in Seq show the service reporting as stopped before the Ping singleton is disposed. Additionally it can be seen that a new service instance is launched before the singleton from the previous instance has been disposed. Note that the hosted service is appropriately 'cleaned up' before the service reports as stopped.

seq-logs

Exceptions (if any)

No response

.NET Version

6.0.100

Anything else?

dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  5.0.401 [C:\Program Files\dotnet\sdk]
  5.0.402 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.2.21505.57 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Tratcher commented 2 years ago

Closing as duplicate of https://github.com/dotnet/runtime/issues/63284 since this code lives in the runtime repo.