Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(core): Reduce risk of race condition during workflow activation loop #13186

Merged
merged 10 commits into from
Feb 13, 2025

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Feb 11, 2025

Summary

Since the first commit, to activate workflows we retrieve all workflows persisted as active and loop over them to activate their webhooks, triggers and pollers, registering them as active in memory if having triggers or pollers. Later we added also populating the webhook cache if having webhooks.

Since workflow activation happens after we stand up the server, we have a race condition between user edits to a workflow's active state and the activation loop. As a result, a user-deactivated workflow can end up active when later processed by the loop. The window for this to happen is longer the more workflows the instance has to activate.

To reduce (but not eliminate) this risk, this PR skips activation of workflows that were deactivated between the start of the loop and the exact time when they are about to be activated. To eliminate this risk, we'd have to move away from the bulk activation loop entirely, which will be a larger effort.

Related Linear tickets, Github issues, and Community forum posts

https://linear.app/n8n/issue/CAT-637

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@@ -90,7 +90,6 @@ export class ActiveWorkflowManager {
await this.addActiveWorkflows('init');

await this.externalHooks.run('activeWorkflows.initialized');
await this.webhookService.populateCache();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are already populating the cache immediately after adding the webhooks for every activated workflow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to this PR, but noticed this: Combined with #13191, we actually now query all webhooks multiple time and add them to the cache multiple times. Shouldn't we only add that one specific WFs webhooks in the addWebhooks?

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Feb 11, 2025
Copy link

codecov bot commented Feb 11, 2025

Codecov Report

Attention: Patch coverage is 85.71429% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/cli/src/active-workflow-manager.ts 83.33% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link

cypress bot commented Feb 13, 2025

n8n    Run #9260

Run Properties:  status check passed Passed #9260  •  git commit 0fba6a4fe5: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review cat-637
Run status status check passed Passed #9260
Run duration 04m 37s
Commit git commit 0fba6a4fe5: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Committer Tomi Turtiainen
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 5
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 436
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@tomi tomi merged commit 64c5b6e into master Feb 13, 2025
37 of 42 checks passed
@tomi tomi deleted the cat-637 branch February 13, 2025 15:10
@github-actions github-actions bot mentioned this pull request Feb 17, 2025
@janober
Copy link
Member

janober commented Feb 20, 2025

Got released with n8n@1.80.1

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants