Skip to content

fix(gateway): shutdown reports unresolved SecretRef warnings when channel credentials use exec/file refs - #112717

Merged
fuller-stack-dev merged 12 commits into
openclaw:mainfrom
BharZInstein:fix/shutdown-secrets-after-channel-stop
Jul 28, 2026
Merged

fuller-stack-dev merged 12 commits into
openclaw:mainfrom
BharZInstein:fix/shutdown-secrets-after-channel-stop

Conversation

@BharZInstein

@BharZInstein BharZInstein commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #112681

What Problem This Solves

Fixes an issue where every gateway shutdown logs an unresolved SecretRef warning and finishes with completed in Nms with warnings: channel/<id> when a channel credential (for example channels.telegram.botToken) is configured as an exec or file SecretRef. The same ref resolves fine at startup and at runtime, so nothing actually failed, but any alerting that greps gateway logs for WARN around restarts fires on every single restart.

Why This Change Was Made

The close prelude cleared the secrets runtime snapshot before the channel stop steps ran, and that clear also drops the pinned runtime config. When the channels step later called getRuntimeConfig(), it lazily re-pinned the raw config file, so stopChannel -> plugin.config.resolveAccount(cfg, id) saw the raw SecretRef and the strict read threw UnresolvedSecretInputError. That throw is what shows up as the shutdown warning, and it also skips the rest of the per-account stop path: the graceful stop wait, the task/abort bookkeeping, the stopped-runtime state write, and the plugin's stopAccount hook (Telegram uses it to release its polling lease, keyed by the token it can no longer read).

The scrub is moved into the close handler's finally block: it still always runs, including when a close step fails, but only after every step that can read credentials. runGatewayClosePrelude loses the parameter since it has exactly one caller and nothing else needs the early clear.

User Impact

Operators who moved channel credentials out of plaintext into SecretRefs (the hardening path from https://docs.openclaw.ai/gateway/secrets) get a clean shutdown completed cleanly in Nms again, restart alerting stops false-positiving, and channel stop hooks run with the resolved credential during shutdown instead of being skipped.

Evidence

Reproduced from source on Linux with an isolated OPENCLAW_STATE_DIR, channels.telegram.botToken set to an exec SecretRef, and the channel connected and polling (getMe/getUpdates answered by a local stub so the run needs no real bot token). Then SIGTERM.

Before (main, 32d4323049):

2026-07-22T18:11:35 [shutdown] channel/telegram: channels.telegram.botToken: unresolved SecretRef "exec:keychain:tg-token". Resolve this command against an active gateway runtime snapshot before reading it.
2026-07-22T18:11:35 [shutdown] completed in 125ms with warnings: channel/telegram

After (this branch, same setup):

2026-07-22T18:11:07 [shutdown] completed cleanly in 400ms

The early clear was pinned down by instrumenting clearRuntimeConfigSnapshot with a stack trace during a reproduction run:

at clearRuntimeConfigSnapshot (dist/runtime-snapshot-*.js)
at Object.clearSecretsRuntimeSnapshot (dist/runtime-state-*.js)
at runGatewayClosePrelude (dist/server-close.runtime.js)
at async runClosePrelude (dist/server.impl-*.js)
at async Object.close (dist/server.impl-*.js)

Validation:

  • node scripts/run-vitest.mjs src/gateway/server-close.test.ts - 47 passed on final merge head 75a3dbf9d5861f6d7f2c8a3bdcb186c3168dfd7e, including the ordering and failure-cleanup regressions.
  • Current-main changed gates passed for core, core tests, and docs: formatting, attribution, boundary guards, core/core-test typechecks, lint, import cycles, and database-first guards.
  • node --import tsx scripts/native-app-i18n.ts verify passed after current-main integration.
  • Final branch autoreview passed with no accepted or actionable findings.

Shutdown cleared the secrets runtime snapshot in the close prelude, which
also drops the pinned runtime config. The channels close step then lazily
re-pins the raw config, so stopChannel's resolveAccount hit unresolved
SecretRefs and every shutdown of a gateway with an exec/file SecretRef
credential logged an unresolved-SecretRef WARN and completed with
warnings. The failed resolveAccount also skipped the per-account stop
bookkeeping (graceful wait, task/abort cleanup, stopped-runtime state)
and the plugin stopAccount hook.

Move the scrub into the close handler's finally block so it still always
runs, but only after every step that can read credentials.

Fixes openclaw#112681
@BharZInstein
BharZInstein marked this pull request as ready for review July 22, 2026 18:12
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Jul 22, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Jul 25, 2026
@clawsweeper

clawsweeper Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 28, 2026, 3:27 AM ET / 07:27 UTC.

ClawSweeper review

What this changes

The PR moves gateway SecretRef snapshot cleanup until after channel and plugin teardown and adds regression tests for teardown ordering and failure cleanup.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep this PR open for normal maintainer review. The patch preserves resolved SecretRefs through channel and plugin teardown, then scrubs them in a finally block; the supplied live before/after shutdown evidence and focused tests support the fix, and no discrete regression is visible in the reviewed diff. GitHub currently marks the branch behind main, so merge-time refresh remains appropriate.

Priority: P2
Reviewed head: 75a3dbf9d5861f6d7f2c8a3bdcb186c3168dfd7e

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, well-scoped repair with direct after-fix runtime evidence and regression coverage; only normal base/check refresh remains.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR includes redacted live terminal output from an isolated Linux SIGTERM reproduction showing the unresolved-SecretRef shutdown warning before the change and a clean shutdown after it; focused regression coverage supplements that proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR includes redacted live terminal output from an isolated Linux SIGTERM reproduction showing the unresolved-SecretRef shutdown warning before the change and a clean shutdown after it; focused regression coverage supplements that proof.
Evidence reviewed 5 items Teardown ordering: The branch removes secret-snapshot clearing from the early close prelude and invokes it from the close handler's finally block after the teardown sequence, preserving credential resolution during stop hooks while retaining unconditional cleanup.
Lifecycle ownership wiring: The lifecycle now passes the clear function to the close handler rather than the prelude, matching the changed ownership boundary for shutdown cleanup.
Regression coverage: The added tests assert that channel teardown precedes snapshot clearing and that the snapshot is still cleared when a channel stop reports a warning.
Findings None None.
Security None None.

How this fits together

Gateway shutdown runs a prelude followed by channel, plugin, and service teardown. Channel stop handlers can still resolve account credentials, so a resolved runtime configuration snapshot must survive until those steps have completed and then be scrubbed.

flowchart LR
  A[Gateway shutdown request] --> B[Close prelude]
  B --> C[Channel teardown]
  C --> D[Plugin and service teardown]
  D --> E[Resolve account credentials]
  E --> F[Clear secret snapshot]
  F --> G[Shutdown result]
Loading

Before merge

  • Resolve merge risk (P1) - GitHub reports the branch as behind main; refresh the merge result and let required checks complete on the exact landing head before merging.
  • Complete next step (P2) - No actionable repair finding remains; this needs ordinary maintainer review after the branch and required checks refresh.
Agent review details

Security

None.

PR surface

Source +6, Tests +39. Total +45 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 9 3 +6
Tests 1 39 0 +39
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 48 3 +45

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #112681
Summary: This PR is a concrete candidate fix for the reported shutdown SecretRef regression.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep credential-resolution state alive through every gateway teardown owner, scrub it exactly once in final cleanup, and merge only after the branch is refreshed against current main and its required checks are green.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep credential-resolution state alive through every gateway teardown owner, scrub it exactly once in final cleanup, and merge only after the branch is refreshed against current main and its required checks are green.

Do we have a high-confidence way to reproduce the issue?

Yes in source and contributor evidence: an exec or file SecretRef remains resolvable during startup but is needed again by channel teardown after the old prelude cleared its runtime snapshot. The supplied isolated-state SIGTERM run reports the failure before the patch and a clean shutdown after it.

Is this the best way to solve the issue?

Yes. Moving the existing scrub to final cleanup is narrower than making each channel cache credentials or suppressing shutdown warnings, and it preserves cleanup even when teardown reports a failure.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d2d6108d2c17.

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • remove rating: 🦞 diamond lobster: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.

Label justifications:

  • P2: This is a bounded gateway shutdown regression that creates recurring false warnings and can skip channel cleanup, without evidence of an emergency-wide outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR includes redacted live terminal output from an isolated Linux SIGTERM reproduction showing the unresolved-SecretRef shutdown warning before the change and a clean shutdown after it; focused regression coverage supplements that proof.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR includes redacted live terminal output from an isolated Linux SIGTERM reproduction showing the unresolved-SecretRef shutdown warning before the change and a clean shutdown after it; focused regression coverage supplements that proof.

Evidence

What I checked:

  • Teardown ordering: The branch removes secret-snapshot clearing from the early close prelude and invokes it from the close handler's finally block after the teardown sequence, preserving credential resolution during stop hooks while retaining unconditional cleanup. (src/gateway/server-close.ts:1042, 75a3dbf9d586)
  • Lifecycle ownership wiring: The lifecycle now passes the clear function to the close handler rather than the prelude, matching the changed ownership boundary for shutdown cleanup. (src/gateway/server-lifecycle.ts:406, 75a3dbf9d586)
  • Regression coverage: The added tests assert that channel teardown precedes snapshot clearing and that the snapshot is still cleared when a channel stop reports a warning. (src/gateway/server-close.test.ts:281, 75a3dbf9d586)
  • Real behavior proof: The PR body provides a Linux isolated-state SIGTERM reproduction with an exec SecretRef: the reported pre-fix shutdown warning is replaced by a clean shutdown after the branch change. The PR is also labeled proof: sufficient. (75a3dbf9d586)
  • Review continuity: The latest completed ClawSweeper cycle at 08c1c51bf8803e8a67e21b6f265ab25df4b52e80 recorded no findings; the supplied follow-up context reports focused gateway proof and changed-surface checks clean after current-main integration. (08c1c51bf880)

Likely related people:

  • fuller-stack-dev: They authored the current-lifecycle adaptation and several subsequent main-integration commits on this PR, and are assigned to its current review. (role: recent gateway integration contributor; confidence: medium; commits: 96510ac7b048, 75a3dbf9d586; files: src/gateway/server-close.ts, src/gateway/server-lifecycle.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (3 earlier review cycles)
  • reviewed 2026-07-25T01:37:01.237Z sha 4af7e9e :: needs maintainer review before merge. :: none
  • reviewed 2026-07-28T05:32:45.214Z sha 96510ac :: needs maintainer review before merge. :: none
  • reviewed 2026-07-28T07:16:03.391Z sha 08c1c51 :: needs maintainer review before merge. :: none

@fuller-stack-dev fuller-stack-dev self-assigned this Jul 28, 2026
Adapt SecretRef shutdown cleanup to the current gateway lifecycle split.
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jul 28, 2026
@fuller-stack-dev

Copy link
Copy Markdown
Member

@clawsweeper re-review

Current head b41aaa30770255a592831a4ffcfd0ec3ba310555 contains current-main integration through 85f03ac6379 and only the four reviewed gateway/changelog files differ from main. The earlier compact tooling failure was the missing-ripgrep test-harness issue fixed by merged #114958 (faa7b2fe1132), which is now in this head. Focused gateway proof passes 47/47; the complete changed-surface gate and branch autoreview are clean. The latest one-commit main drift is code-mode/docs/UI only and does not touch the gateway shutdown or SecretRef surfaces.

@clawsweeper

clawsweeper Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jul 28, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. labels Jul 28, 2026
@fuller-stack-dev

Copy link
Copy Markdown
Member

Maintainer merge note: the branch was refreshed through 0bc6f7525aec, then exact head 75a3dbf9d5861f6d7f2c8a3bdcb186c3168dfd7e completed the full CI gate successfully after the transient agents-embedded no-output shard passed on rerun. main advanced again while CI ran. I am not restarting that moving-target cycle: the intervening commits do not touch src/gateway/server-close.ts, src/gateway/server-lifecycle.ts, or the runtime snapshot path; the only SecretRef-adjacent drift is additive secret-file SDK exports. scripts/pr merge-verify 112717 passes against current main, with no hard check failures. The release-owned changelog entry identified in review was removed.

@fuller-stack-dev
fuller-stack-dev merged commit 7072ba5 into openclaw:main Jul 28, 2026
196 of 200 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 29, 2026
…nnel credentials use exec/file refs (openclaw#112717)

* fix(gateway): clear secrets runtime snapshot only after channel teardown

Shutdown cleared the secrets runtime snapshot in the close prelude, which
also drops the pinned runtime config. The channels close step then lazily
re-pins the raw config, so stopChannel's resolveAccount hit unresolved
SecretRefs and every shutdown of a gateway with an exec/file SecretRef
credential logged an unresolved-SecretRef WARN and completed with
warnings. The failed resolveAccount also skipped the per-account stop
bookkeeping (graceful wait, task/abort cleanup, stopped-runtime state)
and the plugin stopAccount hook.

Move the scrub into the close handler's finally block so it still always
runs, but only after every step that can read credentials.

Fixes openclaw#112681

* chore: drop release-owned changelog entry

---------

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

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

2 participants