Skip to content

fix: handle race condition when logging to removed logger (fixes #318224)#318229

Merged
vs-code-engineering[bot] merged 1 commit into
mainfrom
fix/logger-race-condition-318224-4cec6099d56b06af
May 25, 2026
Merged

fix: handle race condition when logging to removed logger (fixes #318224)#318229
vs-code-engineering[bot] merged 1 commit into
mainfrom
fix/logger-race-condition-318224-4cec6099d56b06af

Conversation

@vs-code-engineering
Copy link
Copy Markdown
Contributor

Summary

The error Create the logger before logging is thrown in LoggerChannel.log() when a renderer process sends an IPC log message for a logger that has already been removed from the loggers map. This is a race condition introduced by commit 1fc96f74 which added eager logger cleanup on deregistration — IPC messages already in the pipeline arrive after the logger entry is deleted.

Hits: ~113k across 1600+ users on 1.122.0-insider builds since 2026-05-22.

Fixes #318224
Recommended reviewer: @SimonSiefke

Culprit Commit

1fc96f74 — "fix: memory leak in LoggerChannel (#315875)" by @SimonSiefke (2026-05-21)

This commit added an onDidChangeLoggers listener that deletes loggers from the ResourceMap when they are deregistered. The error first appeared the next day (2026-05-22), confirming causality.

Code Flow

sequenceDiagram
    participant Renderer
    participant IPC as IPC Pipeline
    participant LC as LoggerChannel
    participant LS as LoggerMainService

    Renderer->>IPC: log(file, messages)
    LS->>LC: onDidChangeLoggers({removed: [file]})
    LC->>LC: loggers.delete(file)
    IPC->>LC: call('log', [file, messages])
    LC->>LC: loggers.get(file) → undefined
    LC->>LC: throw Error('Create the logger before logging')
Loading

Affected Files

File Role
src/vs/platform/log/electron-main/logIpc.ts Error construction site; log() method throws when logger not found

Repro Steps

  1. Open VS Code Insiders (1.122.0+)
  2. A window/extension host deregisters a logger (e.g., during extension deactivation or window close)
  3. Log messages that were already queued in the IPC pipeline arrive at the main process
  4. LoggerChannel.log() throws because the logger was already removed from the map

This is a timing-dependent race that occurs on all platforms (Windows, Mac, Linux).

How the Fix Works

Chosen approach (src/vs/platform/log/electron-main/logIpc.ts:74):

Replaced the throw new Error('Create the logger before logging') with a guard clause that returns early when the logger is not found. This is the correct fix because the error is produced at this exact site — it is the error construction code, not a downstream crash site. The invalid state (missing logger) is produced by the onDidChangeLoggers cleanup handler in the same class (line 20-23) racing with in-flight IPC messages. Since we cannot prevent the renderer from sending messages that are already in the IPC pipeline, the appropriate fix is to gracefully discard stale log messages rather than throw.

After this change, logIpc.ts:74 cannot produce the error because when loggers.get(file) returns undefined, execution returns immediately instead of reaching the throw statement.

Alternatives considered:

  • Buffering messages and replaying after logger creation — overly complex for stale messages that should simply be dropped.
  • Reverting the memory leak fix — reintroduces the original memory leak from fix: memory leak in LoggerChannel #315875.

Recommended Owner

@SimonSiefke — authored the culprit commit 1fc96f74 that introduced the race condition.

Generated by errors-fix · ● 14.3M ·

)

The memory leak fix in 1fc96f7 added cleanup of loggers on deregistration.
This created a race condition where IPC log messages from renderer processes
arrive after the logger has been removed from the map, causing an unhandled
error. Replace the throw with a silent return since log messages for a
removed logger are stale and should be discarded.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 25, 2026 15:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot requested a review from Copilot May 25, 2026 15:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot marked this pull request as ready for review May 25, 2026 15:54
@vs-code-engineering vs-code-engineering Bot enabled auto-merge (squash) May 25, 2026 15:54
@vs-code-engineering vs-code-engineering Bot merged commit 3b30d92 into main May 25, 2026
25 checks passed
@vs-code-engineering vs-code-engineering Bot deleted the fix/logger-race-condition-318224-4cec6099d56b06af branch May 25, 2026 20:13
@vs-code-engineering vs-code-engineering Bot added this to the 1.123.0 milestone May 25, 2026
dileepyavan pushed a commit that referenced this pull request May 27, 2026
) (#318229)

The memory leak fix in 1fc96f7 added cleanup of loggers on deregistration.
This created a race condition where IPC log messages from renderer processes
arrive after the logger has been removed from the map, causing an unhandled
error. Replace the throw with a silent return since log messages for a
removed logger are stale and should be discarded.

Co-authored-by: vs-code-engineering[bot] <122617954+vs-code-engineering[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] unhandlederror-Create the logger before logging

3 participants