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

feat(core): Cancel runner task on timeout in external mode #12101

Merged

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Dec 9, 2024

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Dec 9, 2024
Copy link

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@ivov ivov marked this pull request as ready for review December 9, 2024 15:00
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Couple changes I'd like to see. Also, we should add N8N_RUNNERS_TASK_TIMEOUT to docker/images/n8n/n8n-task-runners.json

const vmResult = (await runInNewContext(
`globalThis.global = globalThis; module.exports = async function VmCodeWrapper() {${settings.code}\n}()`,
context,
)) as TaskResultData['result'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Although it doesn't cover all cases, we could pass the timeout param to the runInNewContext. That way it would get canceled eventually as well. Same for runForEachItem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Today I learned 🙏🏻

ce94ef5

packages/@n8n/task-runner/src/task-runner.ts Outdated Show resolved Hide resolved
context,
)) as INodeExecutionData | undefined;
let result = await new Promise<INodeExecutionData | undefined>((resolve, reject) => {
signal.addEventListener(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this runs in a loop, we are adding a new event listener on each iteration, and it could be a thousand of these. I think it would be good in this case to remove the event listener once we are done. Also then we don't get these warnings:

2024/12/10 10:09:30 ERROR [Runner] (node:28928) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. MaxListeners is 10. Use events.setMaxListeners() to increase limit

Copy link
Contributor Author

@ivov ivov Dec 10, 2024

Choose a reason for hiding this comment

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

Interesting, I thought this was covered by { once: true }

Edit: once removes the listener only if called.

8b75bdf

@ivov ivov requested a review from tomi December 10, 2024 09:42
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Looking good now, one small comment 👍

@@ -89,4 +89,65 @@ describe('TestRunner', () => {
).toThrowError(/Invalid URL/);
});
});

describe('taskCancelled', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

👌

@@ -298,6 +300,20 @@ export abstract class TaskRunner extends EventEmitter {
}
task.cancelled = true;

for (const [requestId, request] of this.dataRequests.entries()) {
if (request.taskId === taskId) {
request.reject(new ApplicationError('Task cancelled'));
Copy link
Contributor

Choose a reason for hiding this comment

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

We receive a cancellation reason in the message. We could include that here. Also could use some more fine grained error than generic ApplicationError

Copy link
Contributor Author

Choose a reason for hiding this comment

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


for (const [requestId, request] of this.nodeTypesRequests.entries()) {
if (request.taskId === taskId) {
request.reject(new ApplicationError('Task cancelled'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tomi tomi added the release/backport Changes that need to be backported to older releases. label Dec 10, 2024
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Thank you for addressing the feedback 💘

Copy link

cypress bot commented Dec 10, 2024

n8n    Run #8265

Run Properties:  status check passed Passed #8265  •  git commit ffaba80f79: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review cat-405-task-that-times-out-is-left-running-on-task-runner
Run status status check passed Passed #8265
Run duration 04m 28s
Commit git commit ffaba80f79: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Committer Iván Ovejero
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 480
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit addb4fa into master Dec 10, 2024
37 checks passed
@ivov ivov deleted the cat-405-task-that-times-out-is-left-running-on-task-runner branch December 10, 2024 11:50
@github-actions github-actions bot mentioned this pull request Dec 10, 2024
@github-actions github-actions bot mentioned this pull request Dec 10, 2024
@janober
Copy link
Member

janober commented Dec 10, 2024

Got released with [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team release/backport Changes that need to be backported to older releases. Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants