Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCORE-2234 Crash in dart due to debug output when app is being torn down #7985

Merged
merged 16 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* Using an empty KeyPath in C API would result in no filtering being done ([#7805](https://github.com/realm/realm-core/issues/7805))
* Fix crash during client app shutdown when Logger log level is set higher than Info. ([#7969](https://github.com/realm/realm-core/issues/7969), since v13.23.3)

### Breaking changes
* None.
Expand Down
18 changes: 12 additions & 6 deletions src/realm/util/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
namespace realm::util {

namespace {
auto& s_logger_mutex = *new std::mutex;
auto& s_default_logger_mutex = *new std::mutex;
std::shared_ptr<util::Logger> s_default_logger;
auto s_stderr_log_mutex = std::make_shared<std::mutex>();
} // anonymous namespace

size_t LogCategory::s_next_index = 0;
Expand Down Expand Up @@ -82,7 +83,7 @@ void LogCategory::set_default_level_threshold(Level level)
for (auto c : m_children) {
c->set_default_level_threshold(level);
}
std::lock_guard lock(s_logger_mutex);
std::lock_guard lock(s_default_logger_mutex);
if (s_default_logger)
set_default_level_threshold(s_default_logger.get());
}
Expand Down Expand Up @@ -110,13 +111,13 @@ void LogCategory::set_default_level_threshold(Logger* root) const

void Logger::set_default_logger(std::shared_ptr<util::Logger> logger) noexcept
{
std::lock_guard lock(s_logger_mutex);
std::lock_guard lock(s_default_logger_mutex);
s_default_logger = logger;
}

std::shared_ptr<util::Logger>& Logger::get_default_logger() noexcept
{
std::lock_guard lock(s_logger_mutex);
std::lock_guard lock(s_default_logger_mutex);
if (!s_default_logger) {
s_default_logger = std::make_shared<StderrLogger>();
}
Expand Down Expand Up @@ -175,10 +176,15 @@ const std::string_view Logger::level_to_string(Level level) noexcept
return "";
}

StderrLogger::StderrLogger() noexcept
: m_log_mutex(s_stderr_log_mutex)
{
}

void StderrLogger::do_log(const LogCategory& cat, Level level, const std::string& message)
{
static Mutex mutex;
LockGuard l(mutex);
REALM_ASSERT(m_log_mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When could this assert ever be false?

std::lock_guard l(*m_log_mutex.get());
// std::cerr is unbuffered, so no need to flush
std::cerr << cat.get_name() << " - " << get_level_prefix(level) << message << '\n'; // Throws
}
Expand Down
6 changes: 5 additions & 1 deletion src/realm/util/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <realm/util/file.hpp>

#include <cstring>
#include <mutex>
#include <ostream>
#include <string>
#include <utility>
Expand Down Expand Up @@ -344,7 +345,7 @@ std::basic_istream<C, T>& operator>>(std::basic_istream<C, T>&, Logger::Level&);
/// level threshold.
class StderrLogger : public Logger {
public:
StderrLogger() noexcept = default;
StderrLogger() noexcept;

StderrLogger(Level level) noexcept
: Logger()
Expand All @@ -354,6 +355,9 @@ class StderrLogger : public Logger {

protected:
void do_log(const LogCategory& category, Level, const std::string&) final;

private:
std::shared_ptr<std::mutex> m_log_mutex;
};


Expand Down
Loading