Repository navigation
[bugfix] Clear in-scope namespaces when an XQuery context is reset - #6752
Merged
duncdrum merged 1 commit intoSep 30, 2026
Merged
Conversation
A compiled query returned to the XQueryPool carried the in-scope namespace bindings of its last execution into the next one. Attribute namespace fixup during element construction consults the context's in-scope namespace maps. Declarations made outside an element constructor -- by the fixup itself, or by a caller configuring the context before execution -- go into the top-level maps, and XQueryContext.reset() cleared variables, closures, options and attributes but not these. On a pooled context they survived into whichever execution next borrowed it, and the fixup trusted them. That is the cause of the XQTS Constr-inscope-1 to -4 flips. The runner serializes every assert-xml result through one cached helper query, so the bindings one test left on a pooled instance decided how the next test's attributes were prefixed. Those four tests use the same two prefixes, foo and XXX, with the URIs swapped between them: after a Constr-inscope-2 or -4 result, the pooled context held foo -> parent1 and XXX -> parent2, and a Constr-inscope-1 or -3 result then came out as <new xmlns:foo="http://www.example.com" ... foo:attr1="attr1"/> -- attr1 in the wrong namespace -- because the stale XXX binding looked like a conflict and the stale foo looked like the prefix to reuse. The maps are reset only when globals are not kept. Compilation resets the context too, with keepGlobals, when the optimizer rewrites the tree and the query is analyzed again, and namespaces a caller declared before compiling must still resolve then. An earlier version of this change cleared them unconditionally, and XQTS json-to-xml-008 and -009 -- whose assertions use a caller-declared prefix inside an optimizable predicate -- failed with XPST0081. A test now pins that path. The maps are replaced rather than cleared: updateContext() shares them by reference with util:eval's inner context, and resetting that must not empty the outer query's bindings mid-evaluation. Reproduced with the CI-path runner: Constr-inscope-1 failed 4 of 4 runs of its test set and passed 6 of 6 in isolation. With this change it passes, and the set's failures are otherwise identical; fn-json-to-xml is unchanged from develop. Closes eXist-db#6704 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
📊 XQTS result comparisonComparison of this run against
Relative to 🟢 Newly passing tests (1)
Runtime: 436.5s (-36.65s vs |
This was referenced Sep 23, 2026
duncdrum
approved these changes
Sep 29, 2026
reinhapa
approved these changes
Sep 30, 2026
joewiz
added a commit
to joewiz/exist
that referenced
this pull request
Oct 7, 2026
Attribute namespace fixup decided whether an attribute's prefix conflicts, and which prefix to use instead, from the query context's in-scope namespace maps. Those maps are shared per-context state: they can hold bindings from an earlier execution of a pooled query (eXist-db#6704), and they miss bindings made on the element itself, such as a computed namespace constructor. Either way, an attribute could end up with a prefix the element binds to a different URI, which puts it in the wrong namespace. Decide from the element instead: from its name and the namespace nodes already attached to it. Keep a prefix the element does not bind, or binds to the same URI. On a conflict, reuse a prefix the element already binds to the attribute's URI, or else generate one it does not use. This fixes XQTS nscons-010, and it fixes Constr-inscope-1 to -4 independently of the context reset in eXist-db#6752. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.
[This PR was prompted by Joe, drafted by Claude Code, and reviewed by Joe.]
Closes #6704
Summary
The XQTS tests
Constr-inscope-1to-4flip between runs and have been putting false red on the conformance comparison of nearly every PR. The cause is not in the tests or the runner: a compiled query returned to theXQueryPoolcarried the in-scope namespace bindings of its last execution into the next one. This clears them on reset.What was wrong
Attribute namespace fixup during element construction (
DocumentBuilderReceiver.resolveAttributeQName) consults the context's in-scope namespace maps. Declarations made outside an element constructor — by the fixup itself, or by a caller configuring the context before execution — go into the top-level maps.XQueryContext.reset()cleared variables, closures, options and attributes, but not these. On a pooled context they survived into whichever execution next borrowed it, and the fixup trusted them.Why it showed up as these four tests
The XQTS runner serializes every
assert-xmlresult through one cached helper query, so a pooled instance of that query sees one test after another.Constr-inscope-1/-3and-2/-4use the same two prefixes,fooandXXX, with the URIs swapped between them. After a-2/-4result, the pooled context heldfoo → parent1, XXX → parent2. A-1/-3result serialized next came out asThe stale
XXXbinding looked like a conflict, and the stalefoolooked like the prefix to reuse, soattr1landed in the wrong namespace while the correct binding sat unused beside it. Which test had last used the borrowed instance decided the outcome — hence the flips in both directions.This is not specific to the runner. Any caller that pools compiled queries and constructs elements with copied, namespaced attributes could see a previous execution's bindings.
Evidence
Reproduced locally with the same runner CI uses, on the
prod-DirElemContent.namespacetest set:developConstr-inscope-1, the four tests aloneConstr-inscope-1, whole test setfn-json-to-xmlsetBisecting the set showed no single half of the other tests reproduces it — it needs tests from both, which is what a leak across a shared pooled instance predicts. An instrumented run showed the same pooled contexts (by identity) reused across threads and tests with their maps accumulating.
What Changed
XQueryContext.reset()keepGlobalsis false, i.e. at the end of a use. Replaced rather thanclear()ed:updateContext()shares these maps by reference withutil:eval's inner context, and resetting that must not empty the outer query's bindings mid-evaluation.PooledContextInScopeNamespacesTest(new)XQueryPoolthe way the runner uses it;reset()leaving nothing behind; and caller-declared namespaces surviving the re-analysis compilation performs after an optimizer rewrite.Why
keepGlobalsreset()has two callers with different needs. At the end of an execution it is cleanup, and the bindings must go. But compilation also callsreset(true)— inanalyzeAndOptimizeIfModulesChanged, when the optimizer has rewritten the tree and the query is analyzed again — and there the namespaces a caller declared before compiling must still resolve. That is the onlyreset(true)caller; every end-of-use reset isreset()/reset(false).For full context, an earlier version of this fix cleared the maps unconditionally, passed the full unit suite, and then failed XQTS
json-to-xml-008and-009withXPST0081 undeclared prefix 'j': their assertions use a prefix the runner declares on the context, inside an optimizable predicate (j:map[@key='glossary']), so they are exactly the compile-then-reanalyze case. The third test pins it.Testing
PooledContextInScopeNamespacesTest— 3/3; the first two red without the fix, the third red against the unconditional versionXQueryTest,XPathQueryTest,CoreTests,XQuery3Tests,org.exist.xquery.xquery3.*,org.exist.xquery.functions.util.*(includingutil:eval),org.exist.dom.memtree.*,OptimizerTest,OptimizerTests— 2,371 tests, 0 failuresdevelop: 0 newly failing; all fourConstr-inscopetests pass (28,845 → 28,847 passed). A single run cannot prove a flake gone — the local repeated runs above are the stronger evidence — but it no longer contradicts it anywhereNot in this PR
The fixup decides conflicts from the context's in-scope maps rather than from the namespaces actually declared on the element under construction. That is why stale state could mislead it at all. A follow-up PR changes it to check the element directly. That also fixes XQTS
nscons-010, whose computednamespace p {…}binding never reaches the context's maps. This PR is still worth having on its own: it stops any code from seeing a previous execution's bindings, not just the attribute fixup.🤖 Generated with Claude Code