GFramework/ai-plan/public/archive/microsoft-di-container-disposal/traces/microsoft-di-container-disposal-trace.md
gewuyou 0ec8aa076b fix(core): 修复容器释放与基准资源泄漏
- 修复 MicrosoftDiContainer 的 IDisposable 释放逻辑、根 ServiceProvider 清理与释放后访问保护
- 更新 CQRS benchmarks 的容器 cleanup,并补齐 RequestStartupBenchmarks 的冷启动容器释放路径
- 补充 Core 容器生命周期回归测试并归档 issue 327 的 ai-plan topic
2026-05-06 19:08:48 +08:00

2.0 KiB

MicrosoftDiContainer Disposal Trace

2026-05-06

MDC-DISPOSE-RP-001 Issue #327 disposal repair

  • Trigger:
    • issue #327 reports that MicrosoftDiContainer holds a ReaderWriterLockSlim and a frozen IServiceProvider but never releases either resource explicitly
    • CQRS benchmark types keep MicrosoftDiContainer fields alive across runs and currently only dispose the MediatR ServiceProvider side
    • RequestStartupBenchmarks also creates temporary GFramework runtimes whose backing containers are never surfaced for cleanup
  • Decisions:
    • treat the fix as a container lifetime contract update, not only a benchmark workaround
    • add the disposal contract at the IIocContainer abstraction so callers holding interface references can release the container explicitly
    • keep runtime ownership unchanged; benchmarks that create containers remain responsible for disposing them
  • Implementation notes:
    • MicrosoftDiContainer now releases its frozen root IServiceProvider, clears registration state, disposes the internal ReaderWriterLockSlim, and rejects all later operations with ObjectDisposedException
    • RequestStartupBenchmarks was rewritten so the steady-state runtime keeps an explicit container field and the cold-start benchmark disposes its temporary container in the same measured invocation
    • other benchmark classes that own MicrosoftDiContainer fields now dispose them during GlobalCleanup
  • Validation milestone:
    • python3 scripts/license-header.py --check passed
    • dotnet test GFramework.Core.Tests/GFramework.Core.Tests.csproj -c Release --filter "FullyQualifiedName~MicrosoftDiContainer|FullyQualifiedName~IocContainerLifetimeTests" passed (55/55)
    • dotnet build GFramework.Cqrs.Benchmarks/GFramework.Cqrs.Benchmarks.csproj -c Release passed with 0 warnings / 0 errors
    • dotnet build GFramework.sln -c Release passed with 0 warnings / 0 errors
  • Immediate next step:
    • archive the topic and push the branch