Skip to content

Give the startup checks a scope of their own - #80

Merged
readingdancer merged 1 commit into
developfrom
fix/startup-checks-disposed-provider
Nov 25, 2026
Merged

readingdancer merged 1 commit into
developfrom
fix/startup-checks-disposed-provider

Conversation

@readingdancer

Copy link
Copy Markdown
Member

Summary

Umbraco resolves notification handlers from a scope that ends once the notification has been handled. StartupChecks starts its checks in a background task and returns, so the checks could start after that scope had gone. Resolving FlagDiagnostics from the handler's IServiceProvider then threw ObjectDisposedException. The checks never ran, and every start logged a warning.

It's a race, so it shows on some machines and not others. The demo site here never logged it, while the reporter saw it on every start.

-internal sealed class StartupChecks(IServiceProvider services, ILogger<StartupChecks> logger)
+internal sealed class StartupChecks(IServiceScopeFactory scopes, ILogger<StartupChecks> logger)
 ...
-            foreach (DiagnosticResult result in await services.GetRequiredService<FlagDiagnostics>().RunAsync(cancellationToken))
+            await using AsyncServiceScope scope = scopes.CreateAsyncScope();
+            foreach (DiagnosticResult result in await scope.ServiceProvider.GetRequiredService<FlagDiagnostics>().RunAsync(cancellationToken))

HandleAsync reads IRuntimeState and IHostApplicationLifetime through a short scope of its own too, so nothing in the class depends on the handler's scope.

The 17.x, 18.x and 19.x packages are built from this same source, so the fix applies to all three.

Evidence

  • New test The_checks_still_run_after_the_scope_the_handler_came_from_has_ended: it ends the handler's scope before the checks run, so the order is certain rather than a race. The checks still run and report what they find, with no "couldn't run its startup checks" warning.
  • With a throwaway test (not committed), I confirmed the old pattern throws ObjectDisposedException once the handler's scope has ended. That's the exception in the issue.
  • Unit tests: 377 passed. Integration tests: 52 passed.
  • Not checked: a Windows site on Umbraco 17.8.2 like the reporter's.

Merge danger

Two-way door. StartupChecks is internal, so its constructor isn't public API. There are no settings or behaviour changes, except that the checks now run at startup as intended. Sites that set something up wrongly will start seeing the errors and warnings these checks were always meant to log.

Closes #78

Umbraco resolves notification handlers from a scope that ends once the
notification is handled, but the startup checks run on in the
background. When that scope ended first, resolving FlagDiagnostics threw
ObjectDisposedException, so the checks never ran and every start logged a
warning. The checks now take an IServiceScopeFactory and create a scope
that lasts as long as they do.

Fixes #78
@readingdancer

Copy link
Copy Markdown
Member Author

Review: ready to merge

Reviewed at 3eaaf53 against develop. No issues found.

What I checked

  • The fix: StartupChecks now takes an IServiceScopeFactory, and the checks run in an async scope of their own instead of the handler's IServiceProvider. The scope is created inside the existing try, so if it can't be created, that's still logged as the "couldn't run its startup checks" warning rather than thrown from the background task. HandleAsync reads IRuntimeState and IHostApplicationLifetime through a short scope, and the ApplicationStopping token comes from a singleton, so it stays valid after that scope ends.
  • The new test: The_checks_still_run_after_the_scope_the_handler_came_from_has_ended ends the handler's scope before the checks run, so it reproduces the order from Startup checks fail with ObjectDisposedException on every start #78 every time rather than as a race. A factory that died with its scope would throw, the warning would be logged, and the test would fail.
  • Tests: all 377 unit tests pass on this branch. I didn't run the integration tests or the smoke test.
  • Conventions: it targets develop, follows the PR template (summary, evidence, merge danger) and closes Startup checks fail with ObjectDisposedException on every start #78. StartupChecks is internal, so no public API changes.

Fine to merge.

@readingdancer
readingdancer merged commit 514f863 into develop Nov 25, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

Startup checks fail with ObjectDisposedException on every start

1 participant