Skip to content

refactor(filter): hold the state of one parse in a FilterParseScope - #222

Merged
pdevito3 merged 1 commit into
mainfrom
refactor/filter-parse-scope
Nov 20, 2026
Merged

pdevito3 merged 1 commit into
mainfrom
refactor/filter-parse-scope

Conversation

@pdevito3

Copy link
Copy Markdown
Owner

Why

The state of one ParseFilter call was an implicit state machine. Five [ThreadStatic] fields in two files held it, and ParseFilter saved, set, and restored each field by hand. This PR puts that state in one place and changes no behavior.

What changed

  • The new FilterParseScope holds the parameter flag, the nesting limit and depth, and the two flags of the v1.14.2 retry. One [ThreadStatic] holds the current scope, and Dispose gives back the outer scope.
  • ParseFilter opens the scope with using. The retry filter reads its own scope object, not the thread field.
  • The retry flags have names that say what they do: QueryNameHidUnknownIdentifier and RetryingForLegacyException.
  • FilterValue.Parameterize was an internal property with a setter. It is now a private getter that reads the scope.
  • The new test NestedParseTests pins that a parse inside a parse keeps the settings of the outer parse.

Blast Radius

The public API, the parse results, and the exception types do not change. FilterValue is internal, and InternalsVisibleTo names only QueryKit.UnitTests. Each parse now allocates one small object.

The change fixes no bug. Before this change, the outer retry filter could read the flag of a nested parse that failed. By analysis and by a manual run on both builds, that case gave the same exception, so this PR has no behavior change to call out.

Verification

  • dotnet test QueryKit.UnitTests/ passes 881 tests on net10.0 and net11.0. dotnet test QueryKit.IntegrationTests/ passes 396 tests on Postgres. The Release build has no warnings.
  • The new test passes on main too, because it pins behavior that already existed. A mutation that removes the restore in Dispose makes it fail with Expected name.HolderTypes {empty} to contain QueryKit.FilterValue1[System.String]`.
  • Six verify-querykit drives (parameterized values, nesting limit pass and fail, derived properties, a default filter) gave byte-identical JSON on main and on this branch, against memory and Postgres.
  • I did not run QueryKit.Net6Tests locally, because the .NET 6 runtime is not installed.

ParseFilter kept its per-parse state in five thread-static fields in two files. It saved, set, and restored each field by hand. The state now lives in one FilterParseScope object. A nested parse on the same thread gets its own scope, and the outer scope comes back when the nested parse ends.

The public API and the parse results do not change. A new test pins that a parse inside a parse keeps the settings of the outer parse.
@pdevito3

Copy link
Copy Markdown
Owner Author

review-gate: passed 949719e

The independent review found no high or medium findings. It found four low findings:

  1. The new test passes on main too. This is correct, because the PR is a refactor and the test pins behavior that already existed.
  2. The test could stop running a nested parse without failing. Fixed in 63aba56, which adds a call counter.
  3. A comment deletion removed the reason that [ThreadStatic] is safe. Fixed in 63aba56, which restores it in one line.
  4. No test covers a nested parse that throws. This stays open. The reviewer ran this case by hand on main and on the branch, and both gave the same exception type, message, and constructor call count.

@pdevito3
pdevito3 merged commit 80fb0df into main Nov 20, 2026
2 checks passed
@pdevito3
pdevito3 deleted the refactor/filter-parse-scope branch November 20, 2026 18:14
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.

1 participant