Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bus is always disposed before generic host is stopping hosted services #83

Open
mksergiy opened this issue Nov 15, 2023 · 1 comment
Open
Assignees

Comments

@mksergiy
Copy link

Registering bus.Dispose() in IHostApplicationLifetime.ApplicationStopping causes the bus to be disposed before the generic host stops the hosted services

code from RebusInitializer:

// stopping the bus here will ensure that we've finished executing all message handlers when the container is disposed
stoppingToken.Register(() =>
{
    logger?.LogDebug("Stopping token signaled - disposing bus instance {busInstance}", bus);
    bus.Dispose();
    logger?.LogInformation("Bus instance {busInstance} successfully disposed", bus);
});

When I commented out the registration mentioned above, everything worked fine, including the order of stopping RebusBackgroundService.

@mookid8000
Copy link
Member

Hmm actually this might call for giving Rebus the ability to wait for currently executing message handlers to finish - this way, the stopping token above could just set the number of workers to 0 (thus preventing that additional messages are fetched), and then block, waiting for currently executing message handlers to finish.

This bus could then be disposed at a later point in time when everything else has shut down.

I'll run a couple of experiments to see if that would be feasible.

@mookid8000 mookid8000 self-assigned this Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants