Skip to content

Commit e935c64

Browse files
committed
always log important errors
1 parent f9f59b7 commit e935c64

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Common/Exception.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <Common/logger_useful.h>
1818

1919
#include <algorithm>
20+
#include <atomic>
2021
#include <cstdlib>
2122
#include <cstring>
2223
#include <filesystem>
@@ -37,6 +38,12 @@ namespace ErrorCodes
3738
extern const int LOGICAL_ERROR;
3839
extern const int CANNOT_ALLOCATE_MEMORY;
3940
extern const int CANNOT_MREMAP;
41+
extern const int POTENTIALLY_BROKEN_DATA_PART;
42+
extern const int REPLICA_ALREADY_EXISTS;
43+
extern const int NOT_ENOUGH_SPACE;
44+
extern const int CORRUPTED_DATA;
45+
extern const int CHECKSUM_DOESNT_MATCH;
46+
extern const int CANNOT_WRITE_TO_FILE_DESCRIPTOR;
4047
}
4148

4249
void abortOnFailedAssertion(const String & description, void * const * trace, size_t trace_offset, size_t trace_size)
@@ -263,6 +270,22 @@ void Exception::clearThreadFramePointers()
263270
thread_frame_pointers.frame_pointers.clear();
264271
}
265272

273+
Exception::~Exception()
274+
{
275+
if (logged && logged->load(std::memory_order_relaxed))
276+
{
277+
const int error_code = code();
278+
const bool is_error_important = error_code == ErrorCodes::LOGICAL_ERROR
279+
|| error_code == ErrorCodes::POTENTIALLY_BROKEN_DATA_PART
280+
|| error_code == ErrorCodes::REPLICA_ALREADY_EXISTS
281+
|| error_code == ErrorCodes::NOT_ENOUGH_SPACE
282+
|| error_code == ErrorCodes::CORRUPTED_DATA
283+
|| error_code == ErrorCodes::CHECKSUM_DOESNT_MATCH
284+
|| error_code == ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR;
285+
tryLogException(getLogger("~Exception"), *this, "An important exception was likely ignored, here it is");
286+
}
287+
}
288+
266289
static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string & start_of_message, LogsLevel level)
267290
{
268291
if (!isLoggingEnabled())
@@ -290,6 +313,16 @@ static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string
290313
catch (...) // NOLINT(bugprone-empty-catch)
291314
{
292315
}
316+
317+
/// Mark the exception as logged.
318+
try
319+
{
320+
throw;
321+
}
322+
catch (Exception & e)
323+
{
324+
e.markAsLogged();
325+
}
293326
}
294327

295328
void tryLogCurrentException(const char * log_name, const std::string & start_of_message, LogsLevel level)

src/Common/Exception.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <Common/StackTrace.h>
77
#include <Core/LogsLevel.h>
88

9+
#include <atomic>
910
#include <cerrno>
1011
#include <exception>
1112
#include <vector>
@@ -62,6 +63,12 @@ class Exception : public Poco::Exception
6263
message_format_string_args = msg.format_string_args;
6364
}
6465

66+
~Exception() override;
67+
Exception(const Exception &) = default;
68+
Exception & operator=(const Exception &) = default;
69+
Exception(Exception &&) = default;
70+
Exception & operator=(Exception &&) = default;
71+
6572
/// Collect call stacks of all previous jobs' schedulings leading to this thread job's execution
6673
static thread_local bool enable_job_stack_trace;
6774
using ThreadFramePointersBase = std::vector<StackTrace::FramePointers>;
@@ -166,11 +173,20 @@ class Exception : public Poco::Exception
166173

167174
std::vector<std::string> getMessageFormatStringArgs() const { return message_format_string_args; }
168175

176+
void markAsLogged()
177+
{
178+
if (logged)
179+
{
180+
logged->store(true, std::memory_order_relaxed);
181+
}
182+
}
183+
169184
private:
170185
#ifndef STD_EXCEPTION_HAS_STACK_TRACE
171186
StackTrace trace;
172187
#endif
173188
bool remote = false;
189+
std::shared_ptr<std::atomic<bool>> logged = std::make_shared<std::atomic<bool>>(false);
174190

175191
/// Number of this error among other errors with the same code and the same `remote` flag since the program startup.
176192
size_t error_index = static_cast<size_t>(-1);

0 commit comments

Comments
 (0)