question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Triggers transform to Error state - Cannot access a disposed object. Object name: 'IServiceProvider'

See original GitHub issue

Describe the bug

In some rare occasion we are getting the following exception: Quartz.SchedulerException: Problem instantiating type '<Job full name>': Cannot access a disposed object. Object name: 'IServiceProvider' This will transform the trigger into an Error state which will cause it cease firing… We are using the MicrosoftDependencyInjectionJobFactory and noticed it is happening when the service provider tried to create a scope (Even before it started to initialize the Job itself). So it tries to use a disposed IServiceProvider which shouldn’t occur in the first place, I dont have clear idea why it is getting disposed, but I guess it could happen in some rare cases, like right before restarting a pod. In any case such scenarios shouldn’t transfer a trigger to an Error state since the initialization of the job was not even started.

Version used

3.4

To Reproduce

not easy one since it is rare, the obvious option is to dispose the service provider while Quartz is running.

Expected behavior

Not transforming the trigger state to Error

Additional context Here is the stuck trace:

Quartz.SchedulerException: Problem instantiating type 'SchedulerService.Scheduler.QuartzNet.Jobs.AutoSyncImport.AutoSyncImportJob: Cannot access a disposed object.
Object name: 'IServiceProvider'.'
 ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.CreateScope(IServiceProvider provider)
   at Quartz.Simpl.MicrosoftDependencyInjectionJobFactory.InstantiateJob(TriggerFiredBundle bundle, IScheduler scheduler)
   at Quartz.Simpl.PropertySettingJobFactory.NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
   at Quartz.Core.JobRunShell.Initialize(QuartzScheduler sched, CancellationToken cancellationToken)
   --- End of inner exception stack trace --- [See nested exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.CreateScope(IServiceProvider provider)
   at Quartz.Simpl.MicrosoftDependencyInjectionJobFactory.InstantiateJob(TriggerFiredBundle bundle, IScheduler scheduler)
   at Quartz.Simpl.PropertySettingJobFactory.NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
   at Quartz.Core.JobRunShell.Initialize(QuartzScheduler sched, CancellationToken cancellationToken)]

Our setup:

            services.AddQuartz(q =>
            {
                q.UseSimpleTypeLoader();
                q.UseDefaultThreadPool(tp =>
                {
                    tp.MaxConcurrency = 10;
                });

                q.UseMicrosoftDependencyInjectionScopedJobFactory(options =>
                {
                    options.AllowDefaultConstructor = true;
                });
            });
            
            services.AddQuartzServer(options =>
            {
                options.WaitForJobsToComplete = true;
            });

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DorSedoffcommented, Sep 18, 2022

Hopefully I’ll have some time handling this and reproducing the next week, thanks!

1reaction
lahmacommented, Sep 18, 2022

Looking at the code again, I think changing exception type won’t help here. Would really require a proper detection that host is going down and should not even try to do anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access a disposed object. Object name
When I trigger a database update, be it save or delete, I get this error: Cannot access a disposed object. Object name: 'IServiceProvider'....
Read more >
Cannot access a disposed object. ...
I'm experiencing troubles with running locally JavaScript template, generated by VS Code 1.31.1 with Azure Functions 0.40 extension. func ...
Read more >
c# System.ObjectDisposedException: 'Cannot access a ...
but i having issue i trying to get the validate db to work for the life of me i cant i get the...
Read more >
Cannot access a disposed object in ASP.NET Core when ...
A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the...
Read more >
Cannot Access a Disposed Object ( Reasoning and ...
The causes of the cannot access a disposed object object name error include opening the database connection before accessing the database, changing your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found