-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[BUG] Tenant ID Not Propagating Correctly in Multi-Tenancy Setup When Triggering Workflows #6129
Comments
I was able to debug down to the creation of the scope, but it now looks like the ITenantAccessor wasn't initialized before the scope creation and therefore does not have a Tenant set before or after the scope creation. This may be further upstream than just the scope creation. Could the actor be running in a different service scope from the message received event? |
I think the issue is with the DefaultTenantContextInitializer and how AsyncLocal works on the TenantAccessor. I ran a quick test taking out the AsyncLocal from the TenantAccessor just to see if it worked and messages are now containing the Tenant ID. I think this might be an issue with how AsyncLocal works against nested async methods and since it sets it under a nested async, this is causing it to lose "scope" after InitializeAsync and so it gets reset to NULL. |
Following this up with an additional test. I took the lines that were in the DefaultTenantContextInitializer out of that class and used the ITenantFinder and ITenantAccessor directly in the receive middleware. Results show that that this is working correctly with the Tenant ID being passed around. This leads me to conclude that the InitializeAsync is having an issue with the AsyncLocal since it would naturally want to reset the variable once outside of the InitializeAsync. |
I fixed it by creating a tenant scope (which is essentially a wrapper around a child service scope) and temporarily configuring ProtoActor's DI service provider with the service provider from said child scope. The same technique is applied to the multitenant ASP.NET middleware component, where the HttpContext.RequestServices object is temporarily replaced with a tenant scoped provider for the duration of the next middleware. Seems to work well. Another option I considered was propagating the tenant from one service scope to the next by providing a custom IServiceScopeFactory, but the current fix seems to be enough. Let's see if we can do something similar for MT. |
When setting up
Elsa.Server.Web
with MultiTenancy support, workflows are successfully created and stored in the database with the correct Tenant ID populated. The API and UI correctly pass the Tenant ID during workflow creation. However, an issue arises when manually triggering a workflow from the UI.The problem appears to occur within the ProtoActor runtime. Specifically, the Tenant ID is not propagated correctly during the call to
CreateAndRun
. As a result, the Tenant ID isNULL
in the SQL command executed by Entity Framework, which leads to an error indicating that the workflow definition cannot be found.Analysis suggests that a new
IServiceScope
may be created during execution byIServiceScopeFactory
, causing theTenantAccessor
to reset and lose the Tenant ID context. Without the Tenant ID, the database query cannot locate the appropriate workflow definition for the tenant.This issue is observed after integrating the new MultiTenancy packages (currently in preview). Previously, the same workflow execution process functioned as expected.
Error Details
The specific error encountered is as follows:
Steps to Reproduce
Elsa.Server.Web
with MultiTenancy support.Expected Behavior
The Tenant ID should propagate through the Actor message headers to ensure the correct tenant's workflow instance is located and executed.
Actual Behavior
The Tenant ID is not propagated correctly, resulting in the following:
NULL
Tenant ID.Possible Cause
The issue may stem from a new
IServiceScope
being created during theCreateAndRun
call, resetting theTenantAccessor
and losing the Tenant ID context. Consequently, the Tenant ID becomesNULL
when the database query is executed.Suggested Next Steps
IServiceScopeFactory
or any internal scope creation during workflow execution is improperly resetting theTenantAccessor
.The text was updated successfully, but these errors were encountered: