Skip to content

Fix nested Session.Tag scopes losing enclosing tags - #524

Merged
snaumenko-st merged 1 commit into
master-servicetitanfrom
fix-nested-session-tag-scopes
Sep 28, 2026
Merged

snaumenko-st merged 1 commit into
master-servicetitanfrom
fix-nested-session-tag-scopes

Conversation

@snaumenko-st

@snaumenko-st snaumenko-st commented Sep 28, 2026 •

Copy link
Copy Markdown

Summary

  • With 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.
  • Session now keeps one tag stack; LastTagOverrides is applied when tags are read (Session.Tags returns only the top tag), which covers LINQ translation, prefetch and entity-set loading.
  • TagScope.Dispose 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) no longer throws.

Test plan

  • New TagTest cases, each under Default and LastTagOverrides, asserting both session.Tags and 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.Tests TagTest: 35/35 pass (SQL Server).
  • Xtensive.Orm.BulkOperations.Tests TagTest: 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 under TaggingBehavior.LastTagOverrides, where each Tag() call used to replace the tag list).

Session now keeps a single tag stack for all behaviors; LastTagOverrides is applied only when tags are read via Session.Tags (LINQ SQL comments, prefetch, compilation). TagScope.Dispose truncates the list from the scope’s insertion index instead of always popping the last entry, so double-dispose, outer-first dispose, and default(TagScope).Dispose() do not strip enclosing tags.

Adds TagTest coverage for nested/disposal edge cases under both Default and LastTagOverrides, with helpers BuildDomain and RenderBookQuery.

Reviewed by Cursor Bugbot for commit f44961b. Bugbot is set up for automated code reviews on this repo. Configure here.

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>
@snaumenko-st
snaumenko-st merged commit f37356b into master-servicetitan Sep 28, 2026
37 checks passed
@snaumenko-st
snaumenko-st deleted the fix-nested-session-tag-scopes branch September 28, 2026 18:30
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.

2 participants