Skip to content

Commit

Permalink
addon: Fix logger crash when node is shutting down (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigasp authored May 22, 2024
1 parent 78afc5a commit b2f821f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
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

0 comments on commit b2f821f

Please sign in to comment.