This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git


The following commit(s) were added to refs/heads/master by this push:
     new afec0c9adcce fix(hive-sync): close proxied IMetaStoreClient in 
HoodieHiveSyncClient.close() to prevent HMS connection leak (#19331)
afec0c9adcce is described below

commit afec0c9adcce1e67dea0a9fb475834bb6a1bdec9
Author: Shihuan Liu <[email protected]>
AuthorDate: Mon Jul 20 18:16:21 2026 -0700

    fix(hive-sync): close proxied IMetaStoreClient in 
HoodieHiveSyncClient.close() to prevent HMS connection leak (#19331)
---
 .../org/apache/hudi/hive/HoodieHiveSyncClient.java | 12 ++++
 .../hudi/hive/TestHoodieHiveSyncClientClose.java   | 78 ++++++++++++++++++++++
 2 files changed, 90 insertions(+)

diff --git 
a/hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveSyncClient.java
 
b/hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveSyncClient.java
index 77acc3d0e3d3..9c5033c80bd4 100644
--- 
a/hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveSyncClient.java
+++ 
b/hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveSyncClient.java
@@ -598,6 +598,18 @@ public class HoodieHiveSyncClient extends HoodieSyncClient 
{
     try {
       ddlExecutor.close();
       if (client != null) {
+        // Close the proxied IMetaStoreClient directly before 
Hive.closeCurrent().
+        // When RetryingMetaStoreClient rebuilds the underlying client on a 
transient
+        // TException, the fresh MSC is reachable only through this proxy, 
while the
+        // thread-local Hive singleton still references the older instance. So
+        // Hive.closeCurrent() alone closes the stale MSC and orphans the 
retry-created
+        // one, leaking a connection per sync cycle. client.close() releases 
the live
+        // MSC by identity; Hive.closeCurrent() remains a fallback for the 
singleton path.
+        try {
+          client.close();
+        } catch (Exception e) {
+          log.warn("Failed to close IMetaStoreClient directly; 
Hive.closeCurrent() will run anyway", e);
+        }
         Hive.closeCurrent();
         client = null;
       }
diff --git 
a/hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientClose.java
 
b/hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientClose.java
new file mode 100644
index 000000000000..53c725dc6a3b
--- /dev/null
+++ 
b/hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientClose.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hudi.hive;
+
+import org.apache.hudi.hive.ddl.DDLExecutor;
+
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Unit tests for {@link HoodieHiveSyncClient#close()} connection cleanup.
+ *
+ * <p>Regression coverage for the metastore connection leak: when
+ * {@code RetryingMetaStoreClient} rebuilds the underlying client on a 
transient
+ * error, {@code Hive.closeCurrent()} alone closes the stale singleton-bound
+ * client and orphans the retry-created one. {@code close()} must therefore
+ * release the client held on the proxy field directly.
+ */
+class TestHoodieHiveSyncClientClose {
+
+  @Test
+  void closeReleasesProxiedMetastoreClientDirectly() throws Exception {
+    HoodieHiveSyncClient syncClient = mock(HoodieHiveSyncClient.class, 
CALLS_REAL_METHODS);
+    IMetaStoreClient metaStoreClient = mock(IMetaStoreClient.class);
+    DDLExecutor ddlExecutor = mock(DDLExecutor.class);
+    setField(syncClient, "client", metaStoreClient);
+    setField(syncClient, "ddlExecutor", ddlExecutor);
+
+    syncClient.close();
+
+    verify(metaStoreClient).close();
+    verify(ddlExecutor).close();
+  }
+
+  @Test
+  void closeSwallowsProxyCloseFailure() throws Exception {
+    HoodieHiveSyncClient syncClient = mock(HoodieHiveSyncClient.class, 
CALLS_REAL_METHODS);
+    IMetaStoreClient metaStoreClient = mock(IMetaStoreClient.class);
+    DDLExecutor ddlExecutor = mock(DDLExecutor.class);
+    doThrow(new RuntimeException("transient close 
failure")).when(metaStoreClient).close();
+    setField(syncClient, "client", metaStoreClient);
+    setField(syncClient, "ddlExecutor", ddlExecutor);
+
+    // A transient failure closing the proxied client must not propagate.
+    assertDoesNotThrow(syncClient::close);
+    verify(metaStoreClient).close();
+  }
+
+  private static void setField(Object target, String name, Object value) 
throws Exception {
+    Field field = HoodieHiveSyncClient.class.getDeclaredField(name);
+    field.setAccessible(true);
+    field.set(target, value);
+  }
+}

Reply via email to