Skip to content

[bugfix] Clear in-scope namespaces when an XQuery context is reset - #6752

Merged
duncdrum merged 1 commit into
eXist-db:developfrom
joewiz:bugfix/6704-reset-inscope-namespaces
Sep 30, 2026
Merged

duncdrum merged 1 commit into
eXist-db:developfrom
joewiz:bugfix/6704-reset-inscope-namespaces

Conversation

@joewiz

@joewiz joewiz commented Sep 23, 2026

Copy link
Copy Markdown
Member

[This PR was prompted by Joe, drafted by Claude Code, and reviewed by Joe.]

Closes #6704

Summary

The XQTS tests Constr-inscope-1 to -4 flip 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 the XQueryPool carried 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-xml result through one cached helper query, so a pooled instance of that query sees one test after another. Constr-inscope-1/-3 and -2/-4 use the same two prefixes, foo and XXX, with the URIs swapped between them. After a -2/-4 result, the pooled context held foo → parent1, XXX → parent2. A -1/-3 result serialized next came out as

<new xmlns:foo="http://www.example.com" xmlns:XXX="http://www.example.com/parent1" foo:attr1="attr1"/>

The stale XXX binding looked like a conflict, and the stale foo looked like the prefix to reuse, so attr1 landed 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.namespace test set:

develop this PR
Constr-inscope-1, the four tests alone pass 6 of 6 —
Constr-inscope-1, whole test set fail 4 of 4 pass 4 of 4
other failures in the set the same 11 in every run the same 11 in every run
fn-json-to-xml set 17 failing 17 failing — the same 17

Bisecting 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

file change
XQueryContext.reset() Replaces the four in-scope namespace maps with fresh ones — only when keepGlobals is false, i.e. at the end of a use. Replaced rather than clear()ed: updateContext() shares these maps by reference with util:eval's inner context, and resetting that must not empty the outer query's bindings mid-evaluation.
PooledContextInScopeNamespacesTest (new) Three tests: the leak reproduced deterministically through XQueryPool the way the runner uses it; reset() leaving nothing behind; and caller-declared namespaces surviving the re-analysis compilation performs after an optimizer rewrite.

Why keepGlobals

reset() has two callers with different needs. At the end of an execution it is cleanup, and the bindings must go. But compilation also calls reset(true) — in analyzeAndOptimizeIfModulesChanged, 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 only reset(true) caller; every end-of-use reset is reset() / 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-008 and -009 with XPST0081 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 version
  • XQueryTest, XPathQueryTest, CoreTests, XQuery3Tests, org.exist.xquery.xquery3.*, org.exist.xquery.functions.util.* (including util:eval), org.exist.dom.memtree.*, OptimizerTest, OptimizerTests — 2,371 tests, 0 failures
  • Codacy PMD clean on the changed lines
  • Full XQTS on the fork vs develop: 0 newly failing; all four Constr-inscope tests 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 anywhere

Not 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 computed namespace 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

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]>
@joewiz
joewiz requested a review from a team as a code owner September 23, 2026 13:01
@github-actions

Copy link
Copy Markdown
Contributor

📊 XQTS result comparison

Comparison of this run against develop.

Metric develop this run Change
🟢 Passed 28,866 (90.73%) 28,867 (90.73%) +1 (+0.00 pp)
🟢 Failures 1,548 1,547 -1
➖ Errors 134 134 0
➖ Skipped 1,267 1,267 0
🧪 Total tests 31,815 31,815 0

Relative to develop: 1 newly passing, 0 newly failing, 0 new errors, 0 newly skipped — counting only tests recorded in both runs whose outcome changed.

🟢 Newly passing tests (1)
  • Constr-inscope-1 (was failing)

Runtime: 436.5s (-36.65s vs develop).

@duncdrum
duncdrum requested a review from a team September 29, 2026 11:30
@duncdrum
duncdrum merged commit 40bbb43 into eXist-db:develop Sep 30, 2026
9 of 10 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

xquery issue is related to xquery implementation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] XQTS Constr-inscope-1 to -4 flip nondeterministically, putting false red on every PR's conformance gate

3 participants