Reset DbSwitchEventListener with the container - #93
Open
gisostallenberg wants to merge 1 commit into
Open
Conversation
DbSwitchEventListener already implements ResetInterface, but its service
definition is written out explicitly without autoconfigure(), so the
ResetInterface autoconfiguration never applies and reset() is never called.
TenantContext, on the other hand, is tagged kernel.reset. The two therefore
desync in any long-running process:
1. Message/request 1 switches to tenant X — the listener remembers X and
TenantSwitchedEvent sets the context to X.
2. The container is reset between messages (messenger:consume) or between
requests (FrankenPHP worker mode) — the context is nulled, the listener
still holds X.
3. Message/request 2 switches to X again — the listener early-returns on
`currentTenantIdentifier === X` and never dispatches TenantSwitchedEvent,
so the context stays null while the connection is on X.
Anything asking "is a tenant active?" then reads null for the rest of that
worker's life, even though the tenant connection is live and correct.
Why the listener may be reset — and should be: its state is a pure memoisation
of the last switch, and reset() only drops that memory. The very next
SwitchDbEvent performs the switch it would otherwise have skipped, so the only
cost is repeating work that is idempotent by construction: resolve the tenant
config, clear the entity manager, point the connection at the same database.
Nothing that survives a container reset depends on the skipped path — and the
bundle already treats these two as one unit in TenantTestTrait::
resetTenantState(), which resets the listener and the context together.
Tagging the listener kernel.reset gives production the same pairing the test
helper has always had.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #93 +/- ##
=========================================
Coverage 90.95% 90.95%
Complexity 281 281
=========================================
Files 40 40
Lines 1128 1128
=========================================
Hits 1026 1026
Misses 102 102 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
DbSwitchEventListener already implements ResetInterface, but its service definition is written out explicitly without autoconfigure(), so the ResetInterface autoconfiguration never applies and reset() is never called. TenantContext, on the other hand, is tagged kernel.reset. The two therefore desync in any long-running process:
currentTenantIdentifier === Xand never dispatches TenantSwitchedEvent, so the context stays null while the connection is on X.Anything asking "is a tenant active?" then reads null for the rest of that worker's life, even though the tenant connection is live and correct.
Why the listener may be reset — and should be: its state is a pure memoisation of the last switch, and reset() only drops that memory. The very next SwitchDbEvent performs the switch it would otherwise have skipped, so the only cost is repeating work that is idempotent by construction: resolve the tenant config, clear the entity manager, point the connection at the same database. Nothing that survives a container reset depends on the skipped path — and the bundle already treats these two as one unit in TenantTestTrait:: resetTenantState(), which resets the listener and the context together. Tagging the listener kernel.reset gives production the same pairing the test helper has always had.