-
Notifications
You must be signed in to change notification settings - Fork 57.4k
fix(core): Guard event log parsing against unbounded memory growth #28594
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
Merged
guillaumejacquart
merged 5 commits into
master
from
iam-528-bug-event-log-parsing-on-startup-causes-oom-on-starter-plan-v1
Apr 21, 2026
+257
−40
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a72907
fix(core): Guard event log parsing against unbounded memory growth
guillaumejacquart c74ff17
fix(core): Per-file guard count, skip guard in 'all' mode, handle str…
cstuncsik c9ff84d
fix(core): Log stream read errors and shorten config comment
guillaumejacquart 67e4ada
chore(cli): Drop unused jest.MockedClass cast in ai-workflow-builder …
guillaumejacquart 4abbbc2
Revert "chore(cli): Drop unused jest.MockedClass cast in ai-workflow-…
guillaumejacquart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
.../cli/src/eventbus/message-event-bus-writer/__tests__/message-event-bus-log-writer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { Logger } from '@n8n/backend-common'; | ||
| import { GlobalConfig } from '@n8n/config'; | ||
| import { Container } from '@n8n/di'; | ||
| import { mock } from 'jest-mock-extended'; | ||
| import { EventMessageTypeNames } from 'n8n-workflow'; | ||
| import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| import type { EventMessageTypes } from '../../event-message-classes'; | ||
| import { MessageEventBusLogWriter } from '../message-event-bus-log-writer'; | ||
|
|
||
| jest.unmock('node:fs'); | ||
| jest.unmock('node:fs/promises'); | ||
|
|
||
| describe('MessageEventBusLogWriter.readLoggedMessagesFromFile', () => { | ||
| let tempDir: string; | ||
| let logger: ReturnType<typeof mock<Logger>>; | ||
| let writer: MessageEventBusLogWriter; | ||
|
|
||
| const makeWorkflowStartedLine = (id: string, executionId: string) => | ||
| JSON.stringify({ | ||
| __type: EventMessageTypeNames.workflow, | ||
| id, | ||
| ts: '2026-04-16T12:00:00.000Z', | ||
| eventName: 'n8n.workflow.started', | ||
| message: 'n8n.workflow.started', | ||
| payload: { executionId }, | ||
| }); | ||
|
|
||
| const makeConfirmLine = (id: string) => | ||
| JSON.stringify({ | ||
| __type: EventMessageTypeNames.confirm, | ||
| confirm: id, | ||
| ts: '2026-04-16T12:00:00.000Z', | ||
| source: { id: '', name: '' }, | ||
| }); | ||
|
|
||
| const writeLogFile = (fileName: string, lines: string[]): string => { | ||
| const path = join(tempDir, fileName); | ||
| writeFileSync(path, lines.join('\n') + '\n'); | ||
| return path; | ||
| }; | ||
|
|
||
| const setMaxMessagesPerParse = (maxMessagesPerParse: number) => { | ||
| const globalConfig = mock<GlobalConfig>({ | ||
| eventBus: { logWriter: { maxMessagesPerParse, keepLogCount: 3 } }, | ||
| }); | ||
| Container.set(GlobalConfig, globalConfig); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = mkdtempSync(join(tmpdir(), 'eventbus-log-writer-test-')); | ||
| logger = mock<Logger>(); | ||
| Container.set(Logger, logger); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(tempDir, { recursive: true, force: true }); | ||
| Container.reset(); | ||
| }); | ||
|
|
||
| it('aborts parsing and warns when the in-memory working set exceeds the configured max', async () => { | ||
| const maxMessagesPerParse = 5; | ||
| setMaxMessagesPerParse(maxMessagesPerParse); | ||
| writer = new MessageEventBusLogWriter(); | ||
|
|
||
| const lines: string[] = []; | ||
| for (let i = 0; i < 100; i++) { | ||
| lines.push(makeWorkflowStartedLine(`id-${i}`, `exec-${i}`)); | ||
| } | ||
| const logFile = writeLogFile('bloated.log', lines); | ||
|
|
||
| const results = { | ||
| loggedMessages: [] as EventMessageTypes[], | ||
| sentMessages: [] as EventMessageTypes[], | ||
| unfinishedExecutions: {} as Record<string, EventMessageTypes[]>, | ||
| }; | ||
|
|
||
| await writer.readLoggedMessagesFromFile(results, 'unsent', logFile); | ||
|
|
||
| expect(results.loggedMessages.length).toBeLessThan(100); | ||
| expect(results.loggedMessages.length).toBeLessThanOrEqual(maxMessagesPerParse + 1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('exceeded 5 in-memory messages during parse'), | ||
| ); | ||
| }); | ||
|
|
||
| it('uses per-file count so prior file accumulation does not abort the next file', async () => { | ||
| const maxMessagesPerParse = 5; | ||
| setMaxMessagesPerParse(maxMessagesPerParse); | ||
| writer = new MessageEventBusLogWriter(); | ||
|
|
||
| // File 1: 4 unconfirmed messages (below limit) | ||
| const lines1: string[] = []; | ||
| for (let i = 0; i < 4; i++) { | ||
| lines1.push(makeWorkflowStartedLine(`old-id-${i}`, `old-exec-${i}`)); | ||
| } | ||
| const logFile1 = writeLogFile('old.log', lines1); | ||
|
|
||
| // File 2: 4 unconfirmed messages (below limit per-file, but 8 total) | ||
| const lines2: string[] = []; | ||
| for (let i = 0; i < 4; i++) { | ||
| lines2.push(makeWorkflowStartedLine(`new-id-${i}`, `new-exec-${i}`)); | ||
| } | ||
| const logFile2 = writeLogFile('new.log', lines2); | ||
|
|
||
| const results = { | ||
| loggedMessages: [] as EventMessageTypes[], | ||
| sentMessages: [] as EventMessageTypes[], | ||
| unfinishedExecutions: {} as Record<string, EventMessageTypes[]>, | ||
| }; | ||
|
|
||
| await writer.readLoggedMessagesFromFile(results, 'unsent', logFile1); | ||
| await writer.readLoggedMessagesFromFile(results, 'unsent', logFile2); | ||
|
|
||
| // Both files should be fully parsed (8 total, each file under limit) | ||
| expect(results.loggedMessages).toHaveLength(8); | ||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not apply the guard in "all" mode since confirms do not prune', async () => { | ||
| const maxMessagesPerParse = 5; | ||
| setMaxMessagesPerParse(maxMessagesPerParse); | ||
| writer = new MessageEventBusLogWriter(); | ||
|
|
||
| const lines: string[] = []; | ||
| for (let i = 0; i < 20; i++) { | ||
| lines.push(makeWorkflowStartedLine(`id-${i}`, `exec-${i}`)); | ||
| } | ||
| const logFile = writeLogFile('all-mode.log', lines); | ||
|
|
||
| const results = { | ||
| loggedMessages: [] as EventMessageTypes[], | ||
| sentMessages: [] as EventMessageTypes[], | ||
| unfinishedExecutions: {} as Record<string, EventMessageTypes[]>, | ||
| }; | ||
|
|
||
| await writer.readLoggedMessagesFromFile(results, 'all', logFile); | ||
|
|
||
| expect(results.loggedMessages).toHaveLength(20); | ||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not abort when confirms prune the working set below the limit', async () => { | ||
| const maxMessagesPerParse = 5; | ||
| setMaxMessagesPerParse(maxMessagesPerParse); | ||
| writer = new MessageEventBusLogWriter(); | ||
|
|
||
| const lines: string[] = []; | ||
| for (let i = 0; i < 100; i++) { | ||
| const id = `id-${i}`; | ||
| lines.push(makeWorkflowStartedLine(id, `exec-${i}`)); | ||
| lines.push(makeConfirmLine(id)); | ||
| } | ||
| const logFile = writeLogFile('healthy.log', lines); | ||
|
|
||
| const results = { | ||
| loggedMessages: [] as EventMessageTypes[], | ||
| sentMessages: [] as EventMessageTypes[], | ||
| unfinishedExecutions: {} as Record<string, EventMessageTypes[]>, | ||
| }; | ||
|
|
||
| await writer.readLoggedMessagesFromFile(results, 'unsent', logFile); | ||
|
|
||
| expect(results.loggedMessages).toHaveLength(0); | ||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we swallow all errors and never throw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, errors are logged but not propagated. I think this makes sense, as we don't want this recovery process (furthermore a single log file reading) to hard fail the whole instance. It's a not critical recovery AFAIK (for instance, we've allowed ourselves to tamper with those files in cloud medic system to solve instances failures)