Skip to content

fix: preserve tenant context for background alteration jobs - #7962

Merged
sfmskywalker merged 4 commits into
elsa-workflows:mainfrom
DenDeline:bugfix/7961-alteration-job-tenant-context
Sep 7, 2026
Merged

sfmskywalker merged 4 commits into
elsa-workflows:mainfrom
DenDeline:bugfix/7961-alteration-job-tenant-context

Conversation

@DenDeline

@DenDeline DenDeline commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Purpose

Preserve the dispatch-time tenant for alteration jobs queued through BackgroundAlterationJobDispatcher, so a plan submitted under a named tenant is executed under that tenant instead of the background worker's ambient tenant.

Scope

  • Bug fix (behavior change)

Description

Problem

BackgroundAlterationJobDispatcher captured only the job ID and later created a plain DI scope. DefaultTenantAccessor keeps the current tenant in an AsyncLocal, which does not flow into the mediator's background worker, so the queued job ran under the worker's tenant. With tenant-aware persistence the job lookup returned null, the callback failed, and the job stayed Pending while the plan stayed Running.

Solution

  • Capture ITenantAccessor.Tenant synchronously in DispatchAsync and pass it into the queued callback.
  • Execute the job inside an ITenantScopeFactory scope created for that tenant (await using), resolving IAlterationJobRunner from the tenant scope. The scope restores the previous tenant on disposal, including when the runner throws. CreateScope(null) keeps the default-tenant path working.
  • The dispatcher's constructor now takes IJobQueue, ITenantAccessor, and ITenantScopeFactory in place of IServiceScopeFactory. The service is DI-registered and constructed only through AlterationsFeature; both tenant services are registered by MultitenancyFeature, a declared dependency of WorkflowsFeature.

Tests

BackgroundAlterationJobDispatcherTests (integration) covers: the queued callback runs under the dispatch-time tenant even after the dispatch scope ended and while a different worker tenant is active, and the worker tenant is restored afterward; the same restoration when the runner throws; two jobs for different tenants executing concurrently each observe their own tenant; and dispatch with no tenant pushed executes the runner under the default tenant.

Verification

  • dotnet test test/integration/Elsa.Alterations.IntegrationTests/... --filter FullyQualifiedName~BackgroundAlterationJobDispatcherTests -p:CollectCoverage=false: 4 passed.
  • dotnet build src/modules/Elsa.Alterations/Elsa.Alterations.csproj: 0 errors, 0 warnings.
  • Branch is merged with current main; the pr workflow runs on the merged head.

Greptile's earlier finding about unconditional tenant-service dependencies is answered on the thread: the services are always registered where alterations can run, and a no-op fallback would reintroduce the defect.

Fixes #7961

🤖 Generated with Claude Code

Capture the current tenant when dispatching a background alteration job
and restore it while executing the queued callback.

Add regression coverage verifying that the dispatch-time tenant is used
and the worker's previous tenant context is restored.

Fixes elsa-workflows#7961
@sfmskywalker

Copy link
Copy Markdown
Member

@greptile

@greptile-apps

greptile-apps Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This change preserves the tenant present when an alteration job is dispatched and restores that tenant context while the queued job runs. The dispatcher now also unconditionally depends on tenant services, so Alterations registrations that do not enable multitenancy must provide compatible tenant dependencies.

Confidence Score: 4/5

Do not merge without ensuring that Alterations remains usable in installations where multitenancy is not registered.

There is one independent P2, non-security finding. Under the scoring rules, a nonempty finding set containing only P2 findings receives a score of 4.

Files Needing Attention: src/modules/Elsa.Alterations/Services/BackgroundAlterationJobDispatcher.cs

Prompt To Fix All With AI
### Issue 1
src/modules/Elsa.Alterations/Services/BackgroundAlterationJobDispatcher.cs:12-14
**Optional tenant services required**

If a host or direct integration registers Alterations without the optional multitenancy feature, resolving `BackgroundAlterationJobDispatcher` now requires two unavailable tenant services. This prevents the dispatcher from being constructed; gate its registration with multitenancy or support absent/no-op tenant dependencies.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "Fix tenant context propagation for alter..." | Re-trigger Greptile

@sfmskywalker sfmskywalker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please review Greptile's comment.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

sfmskywalker and others added 2 commits September 7, 2026 04:51
…ant isolation

Add coverage for the spec lines that were previously untested: the
worker's tenant context is restored after execution even when the
job runner throws, and concurrent jobs dispatched under different
tenants do not exchange tenant context. Extract the shared service
provider / job queue / recording runner arrange logic into
constructor-initialized fields so the three tests stay DRY.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… test setup

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@sfmskywalker
sfmskywalker dismissed their stale review September 7, 2026 02:57

The Greptile finding this review pointed to has been answered and resolved on the thread; current head carries the merged main plus added regression coverage.

@sfmskywalker
sfmskywalker merged commit 54c8fda into elsa-workflows:main Sep 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BackgroundAlterationJobDispatcher loses tenant context for /alterations/submit jobs

2 participants