{"record":{"id":"92236383dccae6a8","repo":"databendlabs/databend","slug":"expect-cluster","errorCode":null,"errorMessage":"expect cluster.","messagePattern":"expect cluster\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/query/service/src/sessions/query_ctx_shared.rs","lineNumber":341,"sourceCode":"        self.cluster_cache.read().clone()\n    }\n\n    pub async fn get_warehouse_clusters(&self) -> Result<Arc<Cluster>> {\n        if let Some(warehouse) = self.warehouse_cache.read().as_ref() {\n            return Ok(warehouse.clone());\n        }\n\n        let config = GlobalConfig::instance();\n        let discovery = ClusterDiscovery::instance();\n        let warehouse = discovery.discover_warehouse_nodes(&config).await?;\n\n        let mut write_guard = self.warehouse_cache.write();\n\n        if write_guard.is_none() {\n            *write_guard = Some(warehouse.clone());\n        }\n\n        Ok(write_guard.as_ref().cloned().expect(\"expect cluster.\"))\n    }\n\n    pub fn get_current_catalog(&self) -> String {\n        self.session.get_current_catalog()\n    }\n\n    pub fn set_current_catalog(&self, catalog_name: String) {\n        self.session.set_current_catalog(catalog_name)\n    }\n\n    pub fn get_aborting(&self) -> Arc<AtomicBool> {\n        self.aborting.clone()\n    }\n\n    pub fn check_aborting(&self) -> Result<(), ContextError> {\n        if self.aborting.load(Ordering::Acquire) {\n            Err(self\n                .get_error()","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/service/src/sessions/query_ctx_shared.rs#L323-L359","documentation":"QueryContextShared::get_warehouse_clusters caches the warehouse cluster info in `warehouse_cache`. After inserting the cloned warehouse when the cache was None, it re-reads and unwraps with expect(\"expect cluster.\") — the value must exist immediately after insertion. The panic means the RwLock write guard saw a contradictory state (None after being set), e.g. lock released between check and read or concurrent clearing.","triggerScenarios":"Concurrent mutation of warehouse_cache between the write_guard set and the as_ref().cloned() read within the same guard (shouldn't happen), or code path where warehouse clone failed/cleared the cache, breaking the just-set invariant.","commonSituations":"Multi-threaded query execution in a managed/warehouse-mode deployment racing on the shared context; unusual cluster/warehouse reconfiguration at runtime.","solutions":["Return the local `warehouse.clone()` directly instead of re-reading the guard, eliminating the impossible unwrap.","Check cluster/warehouse configuration stability; avoid reconfiguring warehouses while queries run.","If persistent, capture stack trace and report as a concurrency bug in query_ctx_shared.","Restart the query/node if the cache got into a bad state."],"exampleFix":"// before\nif write_guard.is_none() { *write_guard = Some(warehouse.clone()); }\nOk(write_guard.as_ref().cloned().expect(\"expect cluster.\"))\n// after\nif write_guard.is_none() { *write_guard = Some(warehouse.clone()); }\nOk(warehouse)","handlingStrategy":"fallback","validationCode":"// confirm warehouse/cluster is configured before running queries\nSELECT * FROM system.clusters;","typeGuard":"fn cluster_cached(s: &QueryContextShared) -> bool { s.warehouse_cache.read().is_some() }","tryCatchPattern":"// prefer returning the already-cloned value instead of re-unwrapping:\nOk(warehouse)","preventionTips":["Avoid runtime warehouse reconfiguration while queries execute","Use the local value instead of re-reading the just-written cache","Monitor for concurrent context-sharing across threads","Report persistent hits as concurrency bugs"],"tags":["rust","panic","concurrency","cluster","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}