You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by si...@apache.org on 2026/10/24 05:30:55 UTC

(hadoop) branch trunk updated: HADOOP-20022. Use ExitUtil instead of Runtime.exit in SecurityUtil.doAsLoginUserOrFatal (#8827)

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

smengcl pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new dead2676895 HADOOP-20022. Use ExitUtil instead of Runtime.exit in SecurityUtil.doAsLoginUserOrFatal (#8827)
dead2676895 is described below

commit dead2676895ba003ed042413dad22bf3d99d9953
Author: Jose Luis López López <jo...@gmail.com>
AuthorDate: Sat Oct 24 07:30:48 2026 +0200

    HADOOP-20022. Use ExitUtil instead of Runtime.exit in SecurityUtil.doAsLoginUserOrFatal (#8827)
    
    Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
    Co-authored-by: copilot-swe-agent[bot] <19...@users.noreply.github.com>
---
 .../org/apache/hadoop/security/SecurityUtil.java   | 14 +++++-----
 .../apache/hadoop/security/TestSecurityUtil.java   | 29 ++++++++++++++++++++
 .../yarn/TestYarnUncaughtExceptionHandler.java     | 31 +++++++++++++---------
 3 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SecurityUtil.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SecurityUtil.java
index bdcb5b93f25..545fec73461 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SecurityUtil.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SecurityUtil.java
@@ -54,6 +54,7 @@
 import org.apache.hadoop.thirdparty.com.google.common.cache.CacheBuilder;
 import org.apache.hadoop.thirdparty.com.google.common.cache.CacheLoader;
 import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache;
+import org.apache.hadoop.util.ExitUtil;
 import org.apache.hadoop.util.StopWatch;
 import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.util.ZKUtil;
@@ -520,21 +521,22 @@ public static Text buildTokenService(URI uri) {
   /**
    * Perform the given action as the daemon's login user. If the login
    * user cannot be determined, this will log a FATAL error and exit
-   * the whole JVM.
+   * the whole JVM via {@link ExitUtil#terminate(int, Throwable)}.
    *
    * @param action action.
    * @param <T> generic type T.
    * @return generic type T.
    */
-  public static <T> T doAsLoginUserOrFatal(PrivilegedAction<T> action) { 
+  public static <T> T doAsLoginUserOrFatal(PrivilegedAction<T> action) {
     if (UserGroupInformation.isSecurityEnabled()) {
-      UserGroupInformation ugi = null;
-      try { 
+      UserGroupInformation ugi;
+      try {
         ugi = UserGroupInformation.getLoginUser();
       } catch (IOException e) {
         LOG.error("Exception while getting login user", e);
-        e.printStackTrace();
-        Runtime.getRuntime().exit(-1);
+        ExitUtil.terminate(-1, e);
+        // Not reached: terminate() either exits the JVM or throws.
+        throw new IllegalStateException(e);
       }
       return ugi.doAs(action);
     } else {
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestSecurityUtil.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestSecurityUtil.java
index 7007e84c6de..ade8c435aa7 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestSecurityUtil.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestSecurityUtil.java
@@ -50,10 +50,12 @@
 import org.apache.hadoop.security.alias.LocalJavaKeyStoreProvider;
 import org.apache.hadoop.security.token.Token;
 import org.apache.hadoop.security.token.TokenIdentifier;
+import org.apache.hadoop.util.ExitUtil;
 import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.util.ZKUtil.ZKAuthInfo;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
 import org.mockito.Mockito;
 
 import org.apache.hadoop.thirdparty.com.google.common.io.Files;
@@ -656,4 +658,31 @@ public void testInvalidHostThrowsException() {
       qualifiedHostResolver.getByName(invalidHost);
     }, "Resolving an invalid host should throw UnknownHostException");
   }
+
+  /**
+   * A failure to get the login user with security enabled must go through
+   * {@link ExitUtil#terminate(int, Throwable)} rather than exiting the JVM
+   * directly.
+   */
+  @Test
+  public void testDoAsLoginUserOrFatalTerminatesOnLoginFailure() {
+    IOException loginFailure = new IOException("login failed");
+    ExitUtil.disableSystemExit();
+    ExitUtil.resetFirstExitException();
+    try (MockedStatic<UserGroupInformation> ugi =
+        Mockito.mockStatic(UserGroupInformation.class)) {
+      ugi.when(UserGroupInformation::isSecurityEnabled).thenReturn(true);
+      ugi.when(UserGroupInformation::getLoginUser).thenThrow(loginFailure);
+
+      ExitUtil.ExitException ee = assertThrows(ExitUtil.ExitException.class,
+          () -> SecurityUtil.doAsLoginUserOrFatal(() -> {
+            throw new AssertionError("action should not run");
+          }));
+      assertEquals(-1, ee.getExitCode());
+      assertSame(loginFailure, ee.getCause());
+    } finally {
+      ExitUtil.enableSystemExit();
+      ExitUtil.resetFirstExitException();
+    }
+  }
 }
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnUncaughtExceptionHandler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnUncaughtExceptionHandler.java
index e0201cfcd15..d6f0bdb7f66 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnUncaughtExceptionHandler.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnUncaughtExceptionHandler.java
@@ -72,19 +72,24 @@ public void run() {
   void testUncaughtExceptionHandlerWithError()
       throws InterruptedException {
     ExitUtil.disableSystemExit();
-    final YarnUncaughtExceptionHandler spyErrorHandler = spy(exHandler);
-    final java.lang.Error error = new java.lang.Error("test-error");
-    final Thread errorThread = new Thread(new Runnable() {
-      @Override
-      public void run() {
-        throw error;
-      }
-    });
-    errorThread.setUncaughtExceptionHandler(spyErrorHandler);
-    assertSame(spyErrorHandler, errorThread.getUncaughtExceptionHandler());
-    errorThread.start();
-    errorThread.join();
-    verify(spyErrorHandler).uncaughtException(errorThread, error);
+    try {
+      final YarnUncaughtExceptionHandler spyErrorHandler = spy(exHandler);
+      final java.lang.Error error = new java.lang.Error("test-error");
+      final Thread errorThread = new Thread(new Runnable() {
+        @Override
+        public void run() {
+          throw error;
+        }
+      });
+      errorThread.setUncaughtExceptionHandler(spyErrorHandler);
+      assertSame(spyErrorHandler, errorThread.getUncaughtExceptionHandler());
+      errorThread.start();
+      errorThread.join();
+      verify(spyErrorHandler).uncaughtException(errorThread, error);
+    } finally {
+      ExitUtil.enableSystemExit();
+      ExitUtil.resetFirstExitException();
+    }
   }
 
   /**


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org