[Bug]: ModelRegistry: registerFactory never invalidates resolvedCache, and concurrent first resolve creates orphaned instances
Bug description
Two problems in ModelRegistry's factory-resolution cache (resolvedCache):
1. registerFactory does not invalidate the cache, so a newer factory never takes effect for ids that were already resolved.
The Javadoc of registerFactory says "Newly registered factories are consulted before older user registrations and before SPI providers." But resolve checks resolvedCache before it looks at the factories (ModelRegistry.java#L131-L137), and registerFactory never touches the cache (#L91-L96). So for any id resolved before the re-registration, the older factory's instance keeps being returned for the life of the JVM. For example, after rotating a provider's API key or base URL and re-registering the factory, already-resolved ids keep using the old key or URL. There is no public way to evict a single entry: only reset() exists, and that also drops every named model and every factory.
2. Concurrent first resolution of the same id creates N instances and orphans N-1 of them.
The cache is filled with get → create → put (#L131-L161). Different callers can receive different instances, and the losers are dropped without being closed, even though model instances typically own HTTP clients and connection pools.
Reproduction (2.0.4 and current main 16cb024, unchanged between them)
ModelRegistry.registerFactory("acme:.*", id -> fake("old-factory"));
ModelRegistry.resolve("acme:m1"); // old-factory
ModelRegistry.registerFactory("acme:.*", id -> fake("new-factory"));
ModelRegistry.resolve("acme:m1"); // still old-factory <-- bug 1
ModelRegistry.resolve("acme:m2"); // new-factory
// bug 2: 8 threads, first resolve of the same id, factory sleeps 50 ms
// output: factory invoked 8 times, distinct instances returned 8
Actual output:
1st resolve: old-factory
after registering newer factory, resolve acme:m1 -> old-factory
never-resolved id acme:m2 -> new-factory
concurrent first resolve x8: factory invoked 8 times, distinct instances returned 8, cached now inst-8
(fake(tag) is a java.lang.reflect.Proxy of Model. I can attach the full single-file program if useful.)
Suggested fix
- In
registerFactory, evict the cache entries whosemodelIdmatches the new pattern. At minimum, document that re-registration does not affect already-resolved ids, and add anevict(modelId)/invalidate(Predicate<String>). - Fill the cache with
resolvedCache.computeIfAbsent(cacheKey, k -> create(...)). Note that creation must stay outside any lock that could deadlock if a factory re-entersresolve. A per-keyMono.cache()/FutureTaskapproach works too.
Context
We hit this downstream: our platform re-registers its factory whenever an admin adds, edits or removes a model provider. Because of (1), we had to make the factory return a late-binding wrapper that looks up the current config on every call, instead of a concrete client. Separately, factories can never be unregistered, so each re-registration leaves the old ProviderEntry in userFactories permanently. Our test suite also leaked cached instances across test classes until we added reset() in @AfterAll. I'm happy to send a PR for both parts if the direction above looks right.
I'll take this one and open a PR shortly (cache invalidation on registerFactory + single-instance creation on concurrent first resolve), with regression tests for both.