Windows service shutdown leaves running threads and keeps running in background (with Redis)?

Tags: #<Tag:0x00007f2356862920>

Hi Sergey,

I’ve encountered very interesting behavior where after initiating service shutdown, SCM reports service as stopped, while the process is kept running in the background.

When attached to process in VS, in Debug->Threads window, I’m seeing Hangfire and Hangfire.Redis-related threads running in the background.

I’m using DI container (Ninject) which handles calls to dispose methods.

Before that, I’m triggering server.SendStop method, I even tried with WaitForShutdownAsync (with cancellation token) and explicit dispose. Nothing helped.

Furthermore, I’ve tried disposing of the storage-connection manually, but that didn’t help either.

I’m seeing correct log entries:
Execution loop Worker:c3177f68 stopped in 0 ms
Server xxxx:37352:3511c942 is reporting itself as stopped
Server xxxx:37352:3511c942 has been stopped in total 253.0794 ms

I’ve completely ditched my business logic from (commented out scheduled task and relevant logic) and this still happens.

Only thing that helps is calling Environment.Exit method after DI container finishes its shutdown process.
If I don’t call Environment.Exit, process shuts down itself after about ~10 minutes (haven’t measured)

Hangfire.Core 1.7.9
Hangfire.Pro 2.2.3
Hangfire.Pro.Redis 2.8.3
Hangfire.Ninject 1.2.0

Fun fact is that this doesn’t happen in completely different windows service with different tasks. I’ve compared line by line and haven’t figured it out. Same build target, same boilerplate code for startup and shutdown.

Any ideas/suggestions appreciated. Thanks.

Hi Ivan,

Can you send me screenshot of the Debug->Threads window (or alternatively output of the stdump utility)? That’s strange threads are still running, but “stopped in…” events are emitted. Also every thread in Hangfire is marked with IsBackground = true property to abort it when host process is shutting down. Also how does your Windows Service look like, can you show me code that’s responsible for startup and shutdown and list all the properties?

Hey Sergey,

Sorry for delay,

I think I’ve gathered most of requested info here:

Here is screenshot from Debug->Threads window.

SD.Tools.OrmProfiler is referring to SolutionsDesign - LLBGen/ORM profiler.

This screenshot is captured after the program continued executing after last bracket in Main method in Program.cs

Let me know if you need anything else.