Skip to content

QuartzHostedService.StopAsync throws NullReferenceException if StartAsync hasn't been run #1123

Description

@ArturDorochowicz

Describe the bug

QuartzHostedService.StopAsync throws NullReferenceException if StartAsync has not been run.

StartAsync won't run if there's another hosted service that is registered earlier and throws in StartAsync. Exception in StartAsync causes Asp.Net Core to begin shutdown during which all hosted services are stopped via StopAsync.

IOW hosted services must not assume in StopAsync that StartAsync has been executed.

Version used
3.2.4

To Reproduce

Expected behavior

QuartzHostedService.StopAsync should check that scheduler is not null.

public async Task StartAsync(CancellationToken cancellationToken)
{
scheduler = await schedulerFactory.GetScheduler(cancellationToken);
await scheduler.Start(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return scheduler.Shutdown(options.Value.WaitForJobsToComplete, cancellationToken);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions