Skip to content

test(gateway): start the auth-removal Gateway under process custody like production - #207615

Merged
obviyus merged 3 commits into
mainfrom
fix/auth-removal-gateway-custody
Nov 28, 2026
Merged

obviyus merged 3 commits into
mainfrom
fix/auth-removal-gateway-custody

Conversation

@obviyus

@obviyus obviyus commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Problem

Since #207510 (440945f), all 5 cases of src/gateway/server-methods/models-auth-removal.integration.test.ts time out at 120 s on every scheduled CI run (46154601106, 46159819558). Bisect, one case, 3 runs per side: 440945f^ green 3/3; 440945f red, with Waiting for N background tasks to finish and then Test timed out in 120000ms.

How it hangs:

  • The test starts a Gateway in the Vitest process and runs a registered config set inside withConfigWriteLock.
  • A pending models.authLogout is Gateway root work, and it waits for that config lock.
  • config set runs runWithLocalStateOwner. Since fix(cli): keep one-shot databases open until admitted detached work settles #207510, its finally waits for root work to go idle (src/cli/local-state-owner.ts:457-461), so it waits for the logout, and the logout waits for the lock: a deadlock.

This deadlock cannot happen in a production Gateway process:

  • A production Gateway starts under process state custody (acquireGatewayLock in src/cli/gateway-cli/run-loop-startup.ts).
  • runWithLocalStateOwner then finds the hosted owner (captureGatewayStateOwner, src/cli/local-state-owner.ts:349-357) and runs the command through that branch. The branch returns before the wait.
  • Under Vitest, acquireGatewayLock returns null unless allowInTests is set (src/infra/gateway-lock.ts:424). So the test Gateway held no custody, and config set took offline custody beside a live Gateway. Production refuses or borrows in that state.

Solution

Test-only. No production lines change.

  • models-auth-removal.integration.test.ts: the test Gateway starts under process custody, as in production (acquireGatewayLock({ allowInTests: true })), and releases it after the Gateway closes.
  • local-state-owner.test.ts: a command run as the hosted owner never waits for Gateway work, whether that work was admitted before or after the command started. A blocked request releases its lock when the owner starts waiting, so a regression fails fast instead of timing out. Spies are now restored between tests, because the existing detached-work test left process.stderr.write spied.

Rejected alternatives:

  • Wait only for root work admitted after a baseline: it still deadlocks when the Gateway admits the request after the command starts.
  • Skip the wait when the process role is gateway: bare openclaw has that role and can run the local TUI inside runWithLocalStateOwner, so its quit path would lose the fix(cli): keep one-shot databases open until admitted detached work settles #207510 wait. The role is also sticky after a Gateway closes.

Impact

Evidence

  • models-auth-removal.integration.test.ts on this head: 5/5 green. On main it fails 5/5 in both scheduled runs above, and locally at 440945f.
  • local-state-owner.test.ts on this head: 5/5 green.
  • Mutation check: with the wait added to the hosted branch, both hosted-ordering cases fail and the other 3 pass.
  • pnpm check:changed: see the CI run on this head.

…ateway

- waitForLocalBackgroundWork returns at once when the process role is gateway. The Gateway owns its root work's lifetime, and its requests can be blocked on a lock the command holds: a registered `config set` waited forever on a pending models.authLogout that was waiting for the config write lock.
- Tests: a Gateway request admitted before or after the command starts; red on main, green here. Spies are restored between tests.
…ike production

Drop the role check: a production Gateway holds process custody, so runWithLocalStateOwner takes the hosted branch and never waits. Test both orderings under real hosted custody.
@clawsweeper

clawsweeper Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes size: S labels Nov 28, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 28, 2026
@obviyus
obviyus merged commit a3bd213 into main Nov 28, 2026
112 checks passed
@obviyus
obviyus deleted the fix/auth-removal-gateway-custody branch November 28, 2026 17:21
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 29, 2026
…ike production (openclaw#207615)

## Problem

Since openclaw#207510 (642acd5), all 5 cases of `src/gateway/server-methods/models-auth-removal.integration.test.ts` time out at 120 s on every scheduled CI run (46154601106, 46159819558). Bisect, one case, 3 runs per side: 642acd5^ green 3/3; 642acd5 red, with `Waiting for N background tasks to finish` and then `Test timed out in 120000ms`.

How it hangs:
- The test starts a Gateway in the Vitest process and runs a registered `config set` inside `withConfigWriteLock`.
- A pending `models.authLogout` is Gateway root work, and it waits for that config lock.
- `config set` runs `runWithLocalStateOwner`. Since openclaw#207510, its `finally` waits for root work to go idle (`src/cli/local-state-owner.ts:457-461`), so it waits for the logout, and the logout waits for the lock: a deadlock.

This deadlock cannot happen in a production Gateway process:
- A production Gateway starts under process state custody (`acquireGatewayLock` in `src/cli/gateway-cli/run-loop-startup.ts`).
- `runWithLocalStateOwner` then finds the hosted owner (`captureGatewayStateOwner`, `src/cli/local-state-owner.ts:349-357`) and runs the command through that branch. The branch returns before the wait.
- Under Vitest, `acquireGatewayLock` returns `null` unless `allowInTests` is set (`src/infra/gateway-lock.ts:424`). So the test Gateway held no custody, and `config set` took offline custody beside a live Gateway. Production refuses or borrows in that state.

## Solution

Test-only. No production lines change.
- `models-auth-removal.integration.test.ts`: the test Gateway starts under process custody, as in production (`acquireGatewayLock({ allowInTests: true })`), and releases it after the Gateway closes.
- `local-state-owner.test.ts`: a command run as the hosted owner never waits for Gateway work, whether that work was admitted before or after the command started. A blocked request releases its lock when the owner starts waiting, so a regression fails fast instead of timing out. Spies are now restored between tests, because the existing detached-work test left `process.stderr.write` spied.

Rejected alternatives:
- Wait only for root work admitted after a baseline: it still deadlocks when the Gateway admits the request after the command starts.
- Skip the wait when the process role is `gateway`: bare `openclaw` has that role and can run the local TUI inside `runWithLocalStateOwner`, so its quit path would lose the openclaw#207510 wait. The role is also sticky after a Gateway closes.

## Impact

- Users: nothing changes. One-shot commands still wait for detached work they admitted (the openclaw#207510 test is still green). In a process that hosts a Gateway, local commands keep running through the hosted owner, as before.
- CI: removes 5 deterministic 120 s timeouts from the `gateway-database-workers` shard.

## Evidence

- `models-auth-removal.integration.test.ts` on this head: 5/5 green. On main it fails 5/5 in both scheduled runs above, and locally at 642acd5.
- `local-state-owner.test.ts` on this head: 5/5 green.
- Mutation check: with the wait added to the hosted branch, both hosted-ordering cases fail and the other 3 pass.
- `pnpm check:changed`: see the CI run on this head.

Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 29, 2026
…ettles (#207510)

## Problem

A one-shot command (`openclaw agent --local`, `agent exec`, `templates add`) closes its databases as soon as its own turn returns. Work the command admitted that runs detached from the turn (subagent completion, announcements back to the parent, cleanup flows) is cut off: its next database write fails with `Database maintenance resource admission is closed`, and the result is lost without a visible error.

## Solution

One owner waits: the one-shot database owners wait for admitted root work to go idle before they close the databases.

- `waitForGatewayRootWorkIdle` (process/gateway-work-admission.ts): resolves when no root work outside the caller is active. It is event-driven (root-work release listeners, no timer). Long-lived subscriptions never count, by the existing `isActiveRootWork` rule, so a process that hosts no Gateway cannot wait on listeners or loops.
- `waitForLocalBackgroundWork` (cli/local-state-owner.ts) is the single call both one-shot owners use: `runWithLocalStateOwner` before it closes its resources, and `agent exec` before it cleans up its temporary state.
- While it waits, the CLI prints `Waiting for N background tasks to finish. Press Ctrl-C to stop.` Ctrl-C stops the wait: through the CLI signal gate, or through the command's own signal bridge for `agent --local` and `agent exec`.

## User impact

- One-shot commands that started detached work keep their databases open until it finishes, with one visible waiting line.
- Commands that start no detached work exit as before, with no wait.
- Gateway users: unchanged. The wait runs only in the one-shot owners.
- This is part A of a split. Part B (subagent spawn in one-shot `agent --local`) builds on it.

## Accepted edge cases

- **Ctrl-C** stops the wait, not the work. The databases then close under any work still running, which is what main does today for all detached work.
- **Failure retries on timers** hold no root work during the delay, so a one-shot process can exit before a retry runs. The work stays persisted as pending; the next Gateway start recovers it.

## Evidence

- New test `local-state-owner.test.ts` "keeps the command's databases open until detached work it admitted settles": red on main (the owner settles while the detached work is still held), green here.
- `gateway-work-admission.idle.test.ts`: no work or only a subscription returns at once; detached work is waited for and reported once; an interrupt stops the wait.
- Consumer tests of every changed module pass.

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 440945f)

Later same-path commits from the pinned scan (separate behavior unless named as folded/selected below): a3bd213 test(gateway): start the auth-removal Gateway under process custody like production (#207615).
Later PR-number mentions: `6ec3cbf5614c` fix(agents): run sessions_spawn children in one-shot agent --local (#207557), `a3bd2136eec2` test(gateway): start the auth-removal Gateway under process custody like production (#207615) Later commit-SHA mentions: `27036be84d68` test(ui): open worktree row details before following the owner link (#207547), `a3bd2136eec2` test(gateway): start the auth-removal Gateway under process custody like production (#207615)

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 29, 2026
…ike production (#207615)

Since #207510 (440945f), all 5 cases of `src/gateway/server-methods/models-auth-removal.integration.test.ts` time out at 120 s on every scheduled CI run (46154601106, 46159819558). Bisect, one case, 3 runs per side: 440945f^ green 3/3; 440945f red, with `Waiting for N background tasks to finish` and then `Test timed out in 120000ms`.

How it hangs:
- The test starts a Gateway in the Vitest process and runs a registered `config set` inside `withConfigWriteLock`.
- A pending `models.authLogout` is Gateway root work, and it waits for that config lock.
- `config set` runs `runWithLocalStateOwner`. Since #207510, its `finally` waits for root work to go idle (`src/cli/local-state-owner.ts:457-461`), so it waits for the logout, and the logout waits for the lock: a deadlock.

This deadlock cannot happen in a production Gateway process:
- A production Gateway starts under process state custody (`acquireGatewayLock` in `src/cli/gateway-cli/run-loop-startup.ts`).
- `runWithLocalStateOwner` then finds the hosted owner (`captureGatewayStateOwner`, `src/cli/local-state-owner.ts:349-357`) and runs the command through that branch. The branch returns before the wait.
- Under Vitest, `acquireGatewayLock` returns `null` unless `allowInTests` is set (`src/infra/gateway-lock.ts:424`). So the test Gateway held no custody, and `config set` took offline custody beside a live Gateway. Production refuses or borrows in that state.

Test-only. No production lines change.
- `models-auth-removal.integration.test.ts`: the test Gateway starts under process custody, as in production (`acquireGatewayLock({ allowInTests: true })`), and releases it after the Gateway closes.
- `local-state-owner.test.ts`: a command run as the hosted owner never waits for Gateway work, whether that work was admitted before or after the command started. A blocked request releases its lock when the owner starts waiting, so a regression fails fast instead of timing out. Spies are now restored between tests, because the existing detached-work test left `process.stderr.write` spied.

Rejected alternatives:
- Wait only for root work admitted after a baseline: it still deadlocks when the Gateway admits the request after the command starts.
- Skip the wait when the process role is `gateway`: bare `openclaw` has that role and can run the local TUI inside `runWithLocalStateOwner`, so its quit path would lose the #207510 wait. The role is also sticky after a Gateway closes.

- Users: nothing changes. One-shot commands still wait for detached work they admitted (the #207510 test is still green). In a process that hosts a Gateway, local commands keep running through the hosted owner, as before.
- CI: removes 5 deterministic 120 s timeouts from the `gateway-database-workers` shard.

- `models-auth-removal.integration.test.ts` on this head: 5/5 green. On main it fails 5/5 in both scheduled runs above, and locally at 440945f.
- `local-state-owner.test.ts` on this head: 5/5 green.
- Mutation check: with the wait added to the hosted branch, both hosted-ordering cases fail and the other 3 pass.
- `pnpm check:changed`: see the CI run on this head.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit a3bd213)

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. cli CLI command changes gateway Gateway runtime P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant