Skip to content

A closing holder produces a 409 change-pending with a null request ID #496

Closed

Description

@ephico2real2

Part of #13. Found by Codex (reviewer 1) in the review of PR #492. Reviewer 2 confirmed it predates the PR, and rejected the proposed fix (an unbounded re-plan loop in one route).

F09 · Medium · A closing holder produces a 409 with a null request ID

cmapi/src/cmapi/core/db/repo.py:161–168, cmapi/src/cmapi/plugins/namespaces/router.py:263–267. The unique-index violation and the query for its holder run in different transactions. If the holder finishes between them, the response is 409 change-pending, requestId: null. There is no longer a pending request to display. The existing sequential 409 test keeps the holder open throughout. For namespace PATCH, repeat replay lookup and planning with the original headers after this specific race. If Git moved, the caller gets 412 with the current ETag. If a holder still exists, the ordinary named 409 goes straight back. Replanning also rechecks permission and the current configuration.

Fix

diff --git a/cmapi/src/cmapi/plugins/namespaces/router.py b/cmapi/src/cmapi/plugins/namespaces/router.py
--- a/cmapi/src/cmapi/plugins/namespaces/router.py
+++ b/cmapi/src/cmapi/plugins/namespaces/router.py
@@ -259,10 +259,16 @@
     key = _idem(key, dry_run)
-    replay = _replay(c, who, key, raw)
-    if replay:
-        return replay
-    with refusals(c, who, cluster_id, ns, "update", dry_run=dry_run):
-        plan = c.pipeline.plan_namespace(who, cluster_id, raw, action="update", if_match=etag)
-    if dry_run:
-        return JSONResponse(PlanView.model_validate(plan.view()).model_dump(by_alias=True), status_code=200)
-    return _submit(c, who, plan, key, raw, body.justification)
+    while True:
+        replay = _replay(c, who, key, raw)
+        if replay:
+            return replay
+        with refusals(c, who, cluster_id, ns, "update", dry_run=dry_run):
+            plan = c.pipeline.plan_namespace(who, cluster_id, raw, action="update", if_match=etag)
+        if dry_run:
+            return JSONResponse(PlanView.model_validate(plan.view()).model_dump(by_alias=True), status_code=200)
+        try:
+            return _submit(c, who, plan, key, raw, body.justification)
+        except Problem as exc:
+            if exc.code != "change-pending" or exc.extra.get("requestId"):
+                raise
+            # The index saw a holder that closed before the lookup. Check If-Match again.
 

Regression tests

diff --git a/cmapi/tests/core/test_review492_released_holder.py b/cmapi/tests/core/test_review492_released_holder.py
new file mode 100644
--- /dev/null
+++ b/cmapi/tests/core/test_review492_released_holder.py
@@ -0,0 +1,24 @@
+from contextlib import nullcontext
+from unittest.mock import Mock
+
+import pytest
+
+from cmapi.core.errors import Problem
+from cmapi.core.models import NamespacePatch
+from cmapi.plugins.namespaces import router as routes
+
+
+def test_patch_rechecks_etag_when_the_conflicting_holder_has_already_closed(monkeypatch):
+    ctx = Mock()
+    plan = ctx.pipeline.plan_namespace
+    plan.side_effect = [Mock(), Problem(412, "precondition-failed", "Git moved", extra={"etag": "new"})]
+    monkeypatch.setattr(routes, "_replay", lambda *args: None)
+    monkeypatch.setattr(routes, "refusals", lambda *args, **kwargs: nullcontext())
+    submit = Mock(side_effect=Problem.conflict("change-pending", "another request holds ns", requestId=None))
+    monkeypatch.setattr(routes, "_submit", submit)
+    with pytest.raises(Problem) as found:
+        routes.update_namespace("CRC", "ns", NamespacePatch(labels={"owner": "green"}), False, "key", "old", Mock(), ctx)
+    assert (found.value.status, found.value.extra) == (412, {"etag": "new"})
+    assert plan.call_count == 2
+    assert all(call.kwargs["if_match"] == "old" for call in plan.call_args_list)
+    submit.assert_called_once()
diff --git a/cmapi/tests/service/test_review492_holder_race.py b/cmapi/tests/service/test_review492_holder_race.py
new file mode 100644
--- /dev/null
+++ b/cmapi/tests/service/test_review492_holder_race.py
@@ -0,0 +1,22 @@
+from cmapi.core.db.repo import RequestRepo
+from test_namespace_contract_479 import design_config, design_world
+from test_namespace_patch_contract_483 import create, patch
+
+
+def test_holder_closed_between_index_conflict_and_lookup_does_not_return_anonymous_409(design_world, monkeypatch):
+    client, ctx, git, kube, validator, keys = design_world
+    create(design_world)
+    first = patch(design_world, {"labels": {"company.net/cl": "2"}}, dry=False)
+    assert first.status_code == 202
+    rid = first.json()["requestId"]
+    original = RequestRepo.open_for
+
+    def release_then_lookup(repo, cluster, namespace):
+        with ctx.db.session() as session:
+            RequestRepo(session).get(rid).status = "cancelled"
+        return original(repo, cluster, namespace)
+
+    monkeypatch.setattr(RequestRepo, "open_for", release_then_lookup)
+    second = patch(design_world, {"labels": {"company.net/lob": "retail"}}, dry=False)
+    assert second.status_code == 202, second.text
+    assert second.json()["requestId"] != rid

Command and observed output

PYTHONPATH=src python -m pytest -q --tb=short tests/core/test_review492_released_holder.py
HEAD (selected failure lines and pytest summary)
E   AssertionError: assert (409, {'requestId': None}) == (412, {'etag': 'new'})
1 failed in 1.51s
With fixes
1 passed in 1.44s

PYTHONPATH=src python -m pytest -q tests/service/test_review492_holder_race.py — needs PostgreSQL: not run.

Research mandate. Find a bounded fix that names the holder or answers a retryable problem. No unbounded loop.

Activity

  1. added
    bug
    status:open
    sev:low
    Backlog
    on Nov 20, 2026
  2. added
    In-Progress
    and removed
    status:open
    Backlog
    on Nov 20, 2026
  3. added 2 commits that reference this issue on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-Progressbugsev:low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions