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

addon: Fix logger crash when node is shutting down #768

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 9 additions & 8 deletions iModelJsNodeAddon/JsLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SEVERITY JsLogger::JsLevelToSeverity(Napi::Number jsLevel)
/** Synchronize the native std::map on this logger with the "_categoryFilter" map on the JavaScript logger object. */
void JsLogger::SyncLogLevels()
{
BeMutexHolder lock(m_severitiesMutex);
BeMutexHolder lock(m_mutex);

// first sync the default severity level for categories not specified
m_defaultSeverity = JsLevelToSeverity(m_loggerObj.Get("minLevel").As<Napi::Number>());
Expand Down Expand Up @@ -73,7 +73,7 @@ bool JsLogger::canUseJavaScript()
}

/** Given a category and severity level, return true if it should be logged.
* @note Must be called with deferredMutex held
* @note Must be called with mutex held
*/
bool JsLogger::isEnabled(Utf8CP catIn, SEVERITY sev)
{
Expand All @@ -96,6 +96,7 @@ bool JsLogger::isEnabled(Utf8CP catIn, SEVERITY sev)

void JsLogger::Cleanup()
{
BeMutexHolder lock(m_mutex);
m_loggerObj.Reset();
if (m_processLogsOnMainThread)
m_processLogsOnMainThread.Release();
Expand All @@ -109,8 +110,11 @@ void JsLogger::LogMessage(Utf8CP category, SEVERITY sev, Utf8CP msg)
if (canUseJavaScript())
{
logToJs(category, sev, msg);
return;
}
else if (m_processLogsOnMainThread)

BeMutexHolder lock(m_mutex);
if (m_processLogsOnMainThread)
{
m_processLogsOnMainThread.NonBlockingCall([this, category = Utf8String(category), sev, msg = Utf8String(msg)](Napi::Env, Napi::Function)
{
Expand All @@ -126,15 +130,11 @@ void JsLogger::LogMessage(Utf8CP category, SEVERITY sev, Utf8CP msg)
}
});
}
else
{
// js logger is not set
}
}

bool JsLogger::IsSeverityEnabled(Utf8CP category, SEVERITY sev)
{
BeMutexHolder lock(m_severitiesMutex);
BeMutexHolder lock(m_mutex);
return isEnabled(category, sev);
}

Expand All @@ -145,6 +145,7 @@ Napi::Value JsLogger::GetJsLogger()

void JsLogger::SetJsLogger(Napi::CallbackInfo const& info)
{
BeMutexHolder lock(m_mutex);
Cleanup();
m_processLogsOnMainThread = Napi::ThreadSafeFunction::New(info.Env(), Napi::Function::New(info.Env(), [](Napi::CallbackInfo const&){}), "JsLogger", 0, 1);
m_processLogsOnMainThread.Unref(info.Env());
Expand Down
5 changes: 3 additions & 2 deletions iModelJsNodeAddon/JsLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ struct LoggedMessage {
*/
struct JsLogger : NativeLogging::Logger {
private:
/** to synchronize access to `m_defaultSeverity` and `m_categoryFilter`. */
BeMutex m_severitiesMutex;
/** to synchronize access severities and JS objects */
BeMutex m_mutex;

/** the default severity for categories not specified in `m_categoryFilter` */
SEVERITY m_defaultSeverity = LOG_ERROR;
/** cached set of severity levels. Can be cleared from JS by `NativeLibrary.clearLogLevelCache` */
Expand Down