Repository navigation
Fix nested Session.Tag scopes losing enclosing tags - #524
Merged
Merged
Conversation
With TaggingBehavior.LastTagOverrides, Session.Tag() replaced the session's tag list on every call, so disposing an inner scope left the session with no tags even though the outer scope was still open. Keep a single tag stack and apply LastTagOverrides when tags are read instead. TagScope.Dispose now truncates to its own slot instead of popping the last element, so double or out-of-order disposal can't remove an enclosing scope's tag, and disposing a default TagScope is a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SergeiPavlov
approved these changes
Sep 28, 2026
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.
Summary
TaggingBehavior.LastTagOverrides,Session.Tag()replaced the session's tag list on every call. Disposing an inner scope left the session with no tags while the outer scope was still open, so queries were emitted untagged.Sessionnow keeps one tag stack;LastTagOverridesis applied when tags are read (Session.Tagsreturns only the top tag), which covers LINQ translation, prefetch and entity-set loading.TagScope.Disposetruncates to its own slot instead of popping the last element, so double or out-of-order disposal can't remove an enclosing scope's tag, and disposing adefault(TagScope)no longer throws.Test plan
TagTestcases, each underDefaultandLastTagOverrides, asserting bothsession.Tagsand rendered SQL: outer tag restored after inner dispose, both scopes open, three-level nesting, double dispose, outer-first dispose, default scope dispose. 7 failed before the fix.Xtensive.Orm.TestsTagTest: 35/35 pass (SQL Server).Xtensive.Orm.BulkOperations.TestsTagTest: 6/6 pass.🤖 Generated with Claude Code
Note
Medium Risk
Changes how session tags are stored and surfaced to query SQL; wrong behavior could mis-tag or drop observability comments but does not alter data or auth paths.
Overview
Fixes nested
Session.Tag()scopes so disposing an inner scope restores the outer tag instead of leaving the session untagged (notably underTaggingBehavior.LastTagOverrides, where eachTag()call used to replace the tag list).Sessionnow keeps a single tag stack for all behaviors;LastTagOverridesis applied only when tags are read viaSession.Tags(LINQ SQL comments, prefetch, compilation).TagScope.Disposetruncates the list from the scope’s insertion index instead of always popping the last entry, so double-dispose, outer-first dispose, anddefault(TagScope).Dispose()do not strip enclosing tags.Adds
TagTestcoverage for nested/disposal edge cases under bothDefaultandLastTagOverrides, with helpersBuildDomainandRenderBookQuery.Reviewed by Cursor Bugbot for commit f44961b. Bugbot is set up for automated code reviews on this repo. Configure here.