Skip to content

fix: prevent test hangs when shared IAsyncDiscoveryInitializer partially initializes then throws#4746

Merged
thomhurst merged 1 commit intomainfrom
fix/4715-prevent-hang-on-partial-init-failure
Feb 12, 2026
Merged

fix: prevent test hangs when shared IAsyncDiscoveryInitializer partially initializes then throws#4746
thomhurst merged 1 commit intomainfrom
fix/4715-prevent-hang-on-partial-init-failure

Conversation

@thomhurst
Copy link
Owner

Summary

Fixes #4715. When InitializeAsync() starts resources (e.g. ports, background processes via Application.StartAsync()) then throws, TUnit was removing the failed entry from its initialization caches to allow retry. The retry calls InitializeAsync() again on the same partially-initialized shared object, which hangs because the first resources still hold ports/processes.

  • Stop removing failed/cancelled entries from ObjectInitializer and ObjectLifecycleService caches — the faulted Lazy<Task> / TaskCompletionSource stays in cache so subsequent callers get the same error immediately instead of retrying
  • Fix fast-path in ObjectLifecycleService.EnsureInitializedAsync to also check IsCanceled so cancelled initializations don't silently return an uninitialized object
  • Add 5-minute discovery timeout to the filter execution path (used by VS Test Explorer / dotnet test --filter), matching the existing timeout in the streaming path
  • Add engine test for the attribute-based property injection failure scenario (the exact pattern from [Bug]: Tests hang indefinitely when InitializeAsync throws exception #4715)

Test plan

  • AttributePropertyInjectionFailureTests in TestProject fails fast (~1s), not hanging
  • BackgroundProcessInitFailureTests in TestProject fails fast (~1s), not hanging
  • PropertyInjectionInitFailureTests engine test passes (3/3 — SourceGenerated, Reflection, AOT)
  • AttributePropertyInjectionFailureTests engine test passes (3/3 — SourceGenerated, Reflection, AOT)

🤖 Generated with Claude Code

…tializes then throws (#4715)

When InitializeAsync() starts resources (e.g. ports, background processes)
then throws, TUnit was removing the failed entry from its initialization
caches to allow retry. The retry calls InitializeAsync() again on the same
partially-initialized shared object, which hangs because the first
resources still hold ports/processes.

- Stop removing failed/cancelled entries from ObjectInitializer and
  ObjectLifecycleService caches so the faulted task is returned immediately
  to subsequent callers
- Fix fast-path in ObjectLifecycleService to also check IsCanceled so
  cancelled initializations don't silently return uninitialized objects
- Add 5-minute discovery timeout to the filter execution path matching
  the existing streaming path timeout
- Add engine test for attribute-based property injection failure scenario

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 12, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

The PR correctly addresses issue #4715 by keeping failed/cancelled initialization tasks in cache to prevent retry attempts on partially-initialized objects. The implementation is sound across both source-gen (TUnit.Core) and reflection (TUnit.Engine) modes, with proper thread-safety and error propagation.

@thomhurst thomhurst merged commit 5e18b0e into main Feb 12, 2026
12 of 13 checks passed
@thomhurst thomhurst deleted the fix/4715-prevent-hang-on-partial-init-failure branch February 12, 2026 02:32
This was referenced Feb 12, 2026
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.

[Bug]: Tests hang indefinitely when InitializeAsync throws exception

1 participant