Browse Source

Refactor and unify exit codes
ec1d0c473e709e8aad76b4e190a75ad4125a9ce2

babenko 7 months ago
parent
commit
23b7c61b38

+ 6 - 0
library/cpp/yt/memory/chunked_memory_pool-inl.h

@@ -6,6 +6,8 @@
 
 #include "serialize.h"
 
+#include <library/cpp/yt/system/exit.h>
+
 #include <library/cpp/yt/malloc/malloc.h>
 
 #include <util/system/align.h>
@@ -30,6 +32,10 @@ TDerived* TAllocationHolder::Allocate(size_t size, TRefCountedTypeCookie cookie)
     auto requestedSize = sizeof(TDerived) + size;
     auto* ptr = ::malloc(requestedSize);
 
+    if (!ptr) {
+        AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
+    }
+
 #ifndef _win_
     auto allocatedSize = ::malloc_usable_size(ptr);
     if (allocatedSize) {

+ 1 - 0
library/cpp/yt/memory/ya.make

@@ -21,6 +21,7 @@ PEERDIR(
     library/cpp/yt/assert
     library/cpp/yt/misc
     library/cpp/yt/malloc
+    library/cpp/yt/system
 )
 
 CHECK_DEPENDENT_DIRS(

+ 14 - 0
library/cpp/yt/system/exit.cpp

@@ -0,0 +1,14 @@
+#include "exit.h"
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+void AbortProcess(int exitCode)
+{
+    _exit(exitCode);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT

+ 22 - 0
library/cpp/yt/system/exit.h

@@ -0,0 +1,22 @@
+#pragma once
+
+#include <library/cpp/yt/misc/enum.h>
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+DEFINE_ENUM(EProcessExitCode,
+    ((OK)                  (0))
+    ((ArgumentsError)      (1))
+    ((GenericError)        (2))
+    ((IOError)             (3))
+    ((OutOfMemory)         (9))
+);
+
+//! Invokes _exit to abort the process immediately without calling any cleanup code.
+[[noreturn]] void AbortProcess(int exitCode);
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT

+ 1 - 0
library/cpp/yt/system/ya.make

@@ -3,6 +3,7 @@ LIBRARY()
 INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc)
 
 SRCS(
+    exit.cpp
     thread_id.cpp
 )
 

+ 12 - 2
yt/yt/core/compression/zstd.cpp

@@ -6,6 +6,8 @@
 #include <yt/yt/core/misc/blob.h>
 #include <yt/yt/core/misc/finally.h>
 
+#include <library/cpp/yt/system/exit.h>
+
 #include <library/cpp/yt/memory/chunked_memory_pool.h>
 
 #include <contrib/libs/zstd/lib/zstd_errors.h>
@@ -29,8 +31,16 @@ struct TZstdCompressBufferTag
 
 void VerifyError(size_t result)
 {
-    YT_LOG_FATAL_IF(ZSTD_isError(result),
-        "Zstd compression failed (Error: %v)",
+    if (!ZSTD_isError(result)) {
+        return;
+    }
+
+    if (result == ZSTD_error_memory_allocation) {
+        YT_LOG_ERROR("Zstd compression failed with memory allocation error; terminating");
+        AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
+    }
+
+    YT_LOG_FATAL("Zstd compression failed (Error: %v)",
         ZSTD_getErrorName(result));
 }
 

+ 3 - 1
yt/yt/core/concurrency/execution_stack.cpp

@@ -18,6 +18,8 @@
 
 #include <library/cpp/yt/misc/tls.h>
 
+#include <library/cpp/yt/system/exit.h>
+
 #include <util/system/sanitizers.h>
 
 namespace NYT::NConcurrency {
@@ -88,7 +90,7 @@ TExecutionStack::TExecutionStack(size_t size)
     auto checkOom = [] {
         if (LastSystemError() == ENOMEM) {
             fprintf(stderr, "Out-of-memory condition detected while allocating execution stack; terminating\n");
-            _exit(9);
+            AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
         }
     };
 

+ 2 - 2
yt/yt/core/logging/log_writer_detail.cpp

@@ -3,6 +3,7 @@
 #include "log.h"
 #include "formatter.h"
 
+#include <library/cpp/yt/system/exit.h>
 #include <library/cpp/yt/system/handle_eintr.h>
 
 namespace NYT::NLogging {
@@ -153,8 +154,7 @@ void TStreamLogWriterBase::OnException(const std::exception& ex)
         /*lpNumberOfBytesWritten*/ nullptr,
         /*lpOverlapped*/ nullptr);
 #endif
-    _exit(100);
-    YT_ABORT();
+    AbortProcess(ToUnderlying(EProcessExitCode::IOError));
 }
 
 void TStreamLogWriterBase::ResetCurrentSegment(i64 size)

+ 3 - 1
yt/yt/core/misc/crash_handler.cpp

@@ -10,6 +10,8 @@
 
 #include <yt/yt/library/undumpable/undumpable.h>
 
+#include <library/cpp/yt/system/exit.h>
+
 #include <library/cpp/yt/assert/assert.h>
 
 #include <library/cpp/yt/string/raw_formatter.h>
@@ -463,7 +465,7 @@ void DumpSigcontext(void* uc)
 void CrashTimeoutHandler(int /*signal*/)
 {
     WriteToStderr("*** Process hung during crash ***\n");
-    _exit(1);
+    AbortProcess(ToUnderlying(EProcessExitCode::GenericError));
 }
 
 void DumpUndumpableBlocksInfo()

+ 2 - 1
yt/yt/core/misc/fs.cpp

@@ -8,6 +8,7 @@
 #include <yt/yt/core/actions/invoker_util.h>
 
 #include <library/cpp/yt/system/handle_eintr.h>
+#include <library/cpp/yt/system/exit.h>
 
 #include <util/folder/dirut.h>
 #include <util/folder/iterator.h>
@@ -826,7 +827,7 @@ void WrapIOErrors(std::function<void()> func)
         switch (status) {
             case ENOMEM:
                 fprintf(stderr, "Out-of-memory condition detected during I/O operation; terminating\n");
-                _exit(9);
+                AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
                 break;
 
             case EIO:

Some files were not shown because too many files changed in this diff