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

Lifetime scope in an unobserved task #971

Closed
rosskostron opened this issue Mar 26, 2019 · 2 comments
Closed

Lifetime scope in an unobserved task #971

rosskostron opened this issue Mar 26, 2019 · 2 comments

Comments

@rosskostron
Copy link

since updating from 4.8.1, the following scenario is now throwing an ObjectDisposedException when calling Resolve() inside of DoWork() even though I do not call Dispose() until after DoWork() is complete

public void TriggerWork()
{
    ILifetimeScope lifetimeScope = this._lifetimeScope.BeginLifetimeScope();

    _ = Task.Run(async () =>
    {
        try
        {
            await this.DoWork(lifetimeScope);
        }
        finally
        {
            lifetimeScope.Dispose();
        }
    });
}

private async Task DoWork(ILifetimeScope nestedLifetimeScope)
{
    IService service = nestedLifetimeScope.Resolve<IService>();
    await service.DoLotsOfWork();
    
    IAnotherService anotherService = nestedLifetimeScope.Resolve<IAnotherService>();
    await anotherService.DoEvenMoreWork();
}
@tillig
Copy link
Member

tillig commented Mar 26, 2019

I'm not able to reproduce this in a unit test. On first glance it appears there's some sort of condition where lifetimeScope is going out of scope for the TriggerWork method and the closure isn't being respected or something, but I've not got a lot of experience with unobserved tasks, fire and forget style. It might be something you could ping Stephen Cleary on to see if he has ideas, since he seems to be the community expert.

I did notice that unobserved task exceptions don't bubble up to the caller, so either you're seeing it in a debugger or this is partial code that doesn't show how you're figuring out there's an exception going on.

Without some sort of a way to repro it, I can't debug it.

In 4.9.0 we added code that will actively throw an ObjectDisposedException if an activator is called from a lifetime that's been disposed - #945, #960. It may be that there is something going on where you were inadvertently making use of a bug that we've fixed. I'm guessing; I don't actually know.

We do use SourceLink for debugging now to make it easier to step into the Autofac core code. If you can't create a repro for us, that may be a way to figure out on your end what's going on and give us some pointers.

@tillig
Copy link
Member

tillig commented Jul 5, 2019

Since we haven't heard back on this, I'm going to close it. If there's a way we can reproduce it in a unit test scenario, let us know by opening a new issue, otherwise we're sort of stuck. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants