Skip to content

Logger.Log: null check on _writer outside lock — race condition with Shutdown() #165

@Christophe-Rogiers

Description

@Christophe-Rogiers

Bug

In Logger.Log, the _writer null check is performed before acquiring the lock, but _writer is used inside the lock without re-checking:

if (_writer == null) return;  // Check outside lock
lock (_lock)
{
    _writer.WriteLine(logEntry);  // Could be null if Shutdown() ran concurrently
}

If Shutdown() is called between the null check and the lock acquisition, _writer becomes null, leading to a NullReferenceException.

Impact

Race condition during service shutdown that could cause an unhandled exception and mask the real shutdown reason in logs.

Suggested fix

Re-check _writer inside the lock:

lock (_lock)
{
    if (_writer == null) return;
    _writer.WriteLine(logEntry);
}

File

src/Servy.Core/Logging/Logger.cs — lines 232-234

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions