Fix memory leak and stale-model results when hot-swapping PredictionEnginePool
Description
This PR makes zero-downtime model hot-swapping in Microsoft.Extensions.ML safe under live traffic. Before this change, reloading a PredictionEnginePool had three problems. It leaked native memory, because the pool was built with new DefaultObjectPool, which never disposes the prediction engines it holds, so every reload abandoned a generation of native engines. It could also serve predictions from the old model, because a returned engine always went back to the current pool, so an engine rented against the previous model could be handed out by the new pool after a swap. And it risked disposing the shared model out from under other engines, because each engine was created with ownsTransformer set to true and so believed it solely owned the single shared transformer.
This PR builds each pool generation with DefaultObjectPoolProvider, which returns the framework's DisposableObjectPool for disposable types and disposes engines for us. It routes each rented engine back to the exact generation it came from using a ConditionalWeakTable and disposes the old generation atomically on swap. It creates engines with ownsTransformer set to false, so PoolLoader owns the shared model's lifetime per generation and disposes it only when no surviving generation still uses it. PredictionEnginePool now also implements IDisposable, guards against use after disposal, and disposes its loaders so the file and uri watchers are torn down. New tests cover serving across an in-flight hot-swap, loader disposal, and a regression test that forces overflow disposal and confirms the shared model is never disposed.
Codecov Report
:x: Patch coverage is 71.25749% with 48 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 69.60%. Comparing base (548d4d0) to head (57cf192).
:warning: Report is 3 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #7626 +/- ##
==========================================
+ Coverage 69.59% 69.60% +0.01%
==========================================
Files 1484 1484
Lines 273606 273764 +158
Branches 27949 27969 +20
==========================================
+ Hits 190410 190567 +157
+ Misses 75832 75816 -16
- Partials 7364 7381 +17
| Flag | Coverage Δ | |
|---|---|---|
| Debug | 69.60% <71.25%> (+0.01%) |
:arrow_up: |
| production | 63.85% <49.43%> (+0.01%) |
:arrow_up: |
| test | 89.64% <96.15%> (+<0.01%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Files with missing lines | Coverage Δ | |
|---|---|---|
| ...rosoft.Extensions.ML/PredictionEnginePoolPolicy.cs | 100.00% <100.00%> (+28.57%) |
:arrow_up: |
| ...t.Extensions.ML.Tests/PredictionEnginePoolTests.cs | 96.73% <96.15%> (-3.27%) |
:arrow_down: |
| ...rc/Microsoft.Extensions.ML/PredictionEnginePool.cs | 56.17% <33.33%> (+18.47%) |
:arrow_up: |
| src/Microsoft.Extensions.ML/PoolLoader.cs | 62.85% <56.60%> (-7.15%) |
:arrow_down: |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@kotlarmilos, I guess you already tried so asking mainly to get better context - there is a DisposableObjectPool already in the dotnet/aspnetcore and it should be actually used when we ask for a pool of a disposable type (see here) so is the problem really in the disposing and do we make the pool the right way or it's a bug in the pool we should report (not with fixing it temporarily with our own patched pool for sure)?
Edit: I actually see the place where we create DefaultObjectPool directly.
/ba-g https://github.com/dotnet/machinelearning/pull/7653, ubuntu and macOS tests succeeded