github-actions[bot] commented on code in PR #65409:
URL: https://github.com/apache/doris/pull/65409#discussion_r3551908813


##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalDatabase.java:
##########
@@ -120,12 +120,19 @@ public void resetMetaToUninitialized() {
             LOG.debug("resetToUninitialized db name {}, id {}, isInitializing: 
{}, initialized: {}",
                     this.name, this.id, isInitializing, initialized, new 
Exception());
         }
+        MetaCache<T> cacheToInvalidate = null;
         synchronized (this) {
             this.initialized = false;
             this.lowerCaseToTableName = Maps.newConcurrentMap();
-            if (metaCache != null) {
-                metaCache.invalidateAll();
-            }
+            cacheToInvalidate = metaCache;
+        }
+        // Invalidate cache outside the synchronized block to avoid deadlock:
+        // ExternalDatabase monitor -> Caffeine internal locks (REFRESH path)
+        // vs Caffeine internal locks -> ExternalDatabase monitor (cache 
loader path).
+        // Do NOT null metaCache here — callers dereference it outside the 
monitor
+        // after makeSureInitialized() returns (e.g. getTableNullable, 
getTableNamesWithLock).
+        if (cacheToInvalidate != null) {
+            cacheToInvalidate.invalidateAll();

Review Comment:
   Moving the invalidation outside the monitor fixes the Caffeine lock 
inversion, but this ordering now publishes `initialized = false` while the old 
`metaCache` is still live and not yet invalidated. A query thread can enter 
`makeSureInitialized()` in this window, see `initialized == false`, call 
`buildMetaCache()` as a no-op because `metaCache` is non-null, set `initialized 
= true`, and then `getTableNullable()`/`getTableNamesWithLock()` can read the 
old cache before this line invalidates it. That means `REFRESH DATABASE` can 
still allow a new reader to plan from stale table metadata during the refresh. 
Please publish a fresh cache/snapshot under the monitor before invalidating the 
old one, or otherwise keep readers from reinitializing until the old cache has 
been invalidated outside the monitor.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to