Use writeRetry in case of exception during writing to the log

This commit is contained in:
Azat Khuzhin 2021-06-02 08:01:09 +03:00
parent 8ec874dc33
commit 3d9218dbfa

View File

@ -11,6 +11,7 @@
#include <Common/DNSResolver.h> #include <Common/DNSResolver.h>
#include <common/getThreadId.h> #include <common/getThreadId.h>
#include <Common/SensitiveDataMasker.h> #include <Common/SensitiveDataMasker.h>
#include <Common/IO.h>
namespace DB namespace DB
{ {
@ -43,10 +44,6 @@ void OwnSplitChannel::tryLogSplit(const Poco::Message & msg)
{ {
logSplit(msg); logSplit(msg);
} }
catch (...)
{
MemoryTracker::LockExceptionInThread lock_memory_tracker(VariableContext::Global);
/// It is better to catch the errors here in order to avoid /// It is better to catch the errors here in order to avoid
/// breaking some functionality because of unexpected "File not /// breaking some functionality because of unexpected "File not
/// found" (or similar) error. /// found" (or similar) error.
@ -57,20 +54,21 @@ void OwnSplitChannel::tryLogSplit(const Poco::Message & msg)
/// Also note, that we cannot log the exception here, since this /// Also note, that we cannot log the exception here, since this
/// will lead to recursion, using regular tryLogCurrentException(). /// will lead to recursion, using regular tryLogCurrentException().
/// but let's log it into the stderr at least. /// but let's log it into the stderr at least.
std::string message = "Cannot add message to the log:\n";
message += msg.getText();
message += "\n";
message += getCurrentExceptionMessage(true);
try
{
WriteBufferFromFileDescriptor out(STDERR_FILENO, 4096);
writeBinary(message, out);
out.finalize();
}
catch (...) catch (...)
{ {
/// Nothing can be done. Ignore the exception. MemoryTracker::LockExceptionInThread lock_memory_tracker(VariableContext::Global);
const std::string & exception_message = getCurrentExceptionMessage(true);
const std::string & message = msg.getText();
if (!writeRetry(STDERR_FILENO, "Cannot add message to the log: ") ||
!writeRetry(STDERR_FILENO, message.data(), message.size()) ||
!writeRetry(STDERR_FILENO, "\n") ||
!writeRetry(STDERR_FILENO, exception_message.data(), exception_message.size()) ||
!writeRetry(STDERR_FILENO, "\n")
)
{
/// Nothing can be done. Ignore the error.
} }
} }
} }