René's URL Explorer Experiment


Title: Bug: Agent loses authorization before `run_on_stop` script completes · Issue #19467 · coder/coder · GitHub

Open Graph Title: Bug: Agent loses authorization before `run_on_stop` script completes · Issue #19467 · coder/coder

X Title: Bug: Agent loses authorization before `run_on_stop` script completes · Issue #19467 · coder/coder

Description: Summary The Coder agent is losing authorization and API connectivity before run_on_stop scripts complete execution, violating the documented behavior and preventing proper workspace shutdown operations. Expected Behavior According to the...

Open Graph Description: Summary The Coder agent is losing authorization and API connectivity before run_on_stop scripts complete execution, violating the documented behavior and preventing proper workspace shutdown operat...

X Description: Summary The Coder agent is losing authorization and API connectivity before run_on_stop scripts complete execution, violating the documented behavior and preventing proper workspace shutdown operat...

Opengraph URL: https://github.com/coder/coder/issues/19467

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: Agent loses authorization before `run_on_stop` script completes","articleBody":"\n## Summary\n\nThe Coder agent is losing authorization and API connectivity before `run_on_stop` scripts complete execution, violating the documented behavior and preventing proper workspace shutdown operations.\n\n## Expected Behavior\n\nAccording to the [Terraform provider documentation](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script):\n\n> **run_on_stop** (Boolean) This option defines whether or not the script should run when the agent stops. **The script should exit when it is done to signal that the workspace can be stopped.**\n\nThe agent would be expected to maintain full functionality (including external auth access and API connectivity) until the `run_on_stop` script completes and signals that the workspace can be stopped.\n\nThe alternative is to change the wording in the terraform article to 'should run after the agent stops'.\n\n## Actual Behavior\n\nThe agent loses authorization and API connectivity during `run_on_stop` script execution, causing:\n\n1. `coder external-auth access-token` commands to fail with \"Workspace agent not authorized\"\n2. API connectivity tests to fail\n3. Git operations requiring authentication to fail\n\nThis reduces the ability of a shutdown script to perform actions.\n\n## Evidence\n\nThe example is from a shutdown script which is created and should perform GIT commands on shutdown.\n\n### Logs from `run_on_stop` script execution:\n\n```\n[2025-08-21 12:27:14] === On-stop demo script started ===\n[2025-08-21 12:27:14] System information:\n[2025-08-21 12:27:14] Hostname: coder-workspace-pod\n[2025-08-21 12:27:14] Uptime:  12:27:14 up  4:31,  0 user,  load average: 0.19, 0.32, 0.33\n[2025-08-21 12:27:14] Disk usage: overlay          61G   42G   17G  72% /\n[2025-08-21 12:27:14] Workspace information:\n[2025-08-21 12:27:14] Workspace name: test-workspace\n[2025-08-21 12:27:14] Workspace owner: user\n[2025-08-21 12:27:14] Workspace ID: [workspace-id]\n[2025-08-21 12:27:14] Coder API connectivity: FAILED  ← API already failing\n[2025-08-21 12:27:14] Looking for Git repositories in /home/coder/src/server...\n[2025-08-21 12:27:14] Found Git repository at: /home/coder/src/server/repo\n[2025-08-21 12:27:14] Git status:\n M TEST9.TXT\n?? TEST10.TXT\n[2025-08-21 12:27:14] Current branch: main\n[2025-08-21 12:27:14] All branches: * main\n  remotes/origin/HEAD -> origin/main\n  remotes/origin/main\n[2025-08-21 12:27:14] Remote branches:   origin/HEAD -> origin/main\n  origin/main\n[2025-08-21 12:27:14] Remote URL: https://github.com/user/repo\n[2025-08-21 12:27:14] Last commit: abc123 Manual commit\n[2025-08-21 12:27:14] Upstream branch: origin/main\n[2025-08-21 12:27:14] Repository has full history\n[2025-08-21 12:27:14] Created workspace status file\n[2025-08-21 12:27:14] Found uncommitted changes, creating commit...\n[main def456] Auto-save on workspace stop - Thu Aug 21 12:27:14 PM UTC 2025\n 3 files changed, 3 insertions(+), 1 deletion(-)\n create mode 100644 TEST10.TXT\n[2025-08-21 12:27:14] Attempting to get GitHub token directly...\n[2025-08-21 12:27:14] ✗ Failed to get GitHub token directly  ← External auth failing\n[2025-08-21 12:27:14] This may indicate that GitHub external auth is not configured or the agent cannot access it\n[2025-08-21 12:27:14] Attempting push without authentication...\nEncountered an error running \"coder\", see \"coder --help\" for more information\nerror: Trace=[get git token: ]\nWorkspace agent not authorized.  ← Agent unauthorized during script execution\nSuggestion: Try logging in using 'coder login'.\nerror: unable to read askpass response from '/tmp/coder.XXXXXX/coder'\nfatal: could not read Username for 'https://github.com': No such device or address\n[2025-08-21 12:27:15] Saved workspace metadata to /tmp/workspace-metadata.json\n[2025-08-21 12:27:15] Cleaning up temporary files...\n[2025-08-21 12:27:15] === On-stop demo script completed successfully ===\n[2025-08-21 12:27:15] Check /home/coder/shutdown.log for full execution details\n```\n\n### Key Issues Observed:\n\n1. **API Connectivity Fails**: `curl -s -H \"Coder-Session-Token: ${CODER_AGENT_TOKEN}\" \"${CODER_URL}/api/v2/workspaces\"` returns failure\n2. **External Auth Fails**: `coder external-auth access-token GH` returns \"Workspace agent not authorized\"\n3. **Script Continues**: The script continues to run and completes successfully, but without the expected agent capabilities\n\n## Impact\n\nThis bug prevents legitimate use cases such as:\n\n- Auto-committing and pushing code changes on workspace stop\n- Backing up workspace state to external services\n- Performing authenticated cleanup operations\n- Any operation requiring external auth or API access during shutdown\n\n## Similar Issues\n\n- https://github.com/coder/coder/issues/17108\n- https://github.com/coder/coder/issues/13387\n\n## Workaround\n\nWe've created a workaround that caches external auth tokens during workspace startup and uses them during shutdown:\n\n```hcl\nresource \"coder_script\" \"startup_auth\" {\n  agent_id     = coder_agent.main.id\n  display_name = \"Cache Auth Tokens\"\n  run_on_start = true\n  \n  script = \u003c\u003c-EOT\n    #!/bin/bash\n    mkdir -p ~/.cache/coder\n    # Cache GitHub token for use in shutdown script\n    if coder external-auth access-token GH > ~/.cache/coder/github_token 2>/dev/null; then\n        chmod 600 ~/.cache/coder/github_token\n        echo \"Successfully cached GitHub token\"\n    fi\n  EOT\n}\n\nresource \"coder_script\" \"shutdown_script\" {\n  agent_id     = coder_agent.main.id\n  display_name = \"Auto-save on Stop\"\n  run_on_stop  = true\n  \n  script = \u003c\u003c-EOT\n    #!/bin/bash\n    # Use cached token instead of direct external-auth access\n    if [[ -f ~/.cache/coder/github_token ]]; then\n        GITHUB_TOKEN=$(cat ~/.cache/coder/github_token)\n        # Use token for git operations...\n    fi\n  EOT\n}\n```\n\nHowever, this workaround has security implications:\n- Tokens are stored on disk in the workspace\n- Tokens may expire during long-running workspaces (24 hours)\n- It adds complexity and potential failure points\n\n## Environment\n\n- **Coder Version**: Dev based off 2.24.X-2.25.1\n- **Platform**: Kubernetes\n- **Template**: Using `coder_script` resource with `run_on_stop = true`\n- **External Auth**: GitHub configured and working during normal operation\n\n## Reproduction Steps\n\n1. Create a template with a `run_on_stop` script that uses `coder external-auth access-token`\n2. Create a workspace from the template\n3. Stop the workspace\n4. Observe that external auth fails during script execution\n\n## Expected Fix\n\nThe agent should maintain authorization and API connectivity until the `run_on_stop` script exits, as documented. The agent shutdown sequence should be:\n\n1. Trigger `run_on_stop` scripts\n2. Wait for all `run_on_stop` scripts to complete\n3. **Only then** revoke agent authorization and stop the agent\n4. Proceed with workspace shutdown\n\nThis would align with the documented behavior and enable proper workspace cleanup operations.","author":{"url":"https://github.com/bjornrobertsson","@type":"Person","name":"bjornrobertsson"},"datePublished":"2025-08-21T12:57:37Z","url":"https://github.com/coder/coder/issues/19467"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:c850bd6f-43cb-1bcf-68c2-15b3a4ca69f0
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA662:2454CC:29B6FBC:34F1D1C:6B01A711
html-safe-nonce3742ae55cc55f33961389c00db1dfd68f4271aedf893f01c7463e98780b9f114
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjYyOjI0NTRDQzoyOUI2RkJDOjM0RjFEMUM6NkIwMUE3MTEiLCJ2aXNpdG9yX2lkIjoiNjE3NzM5OTUxMTcwNzY1ODAwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac874d3f0437fdf9bdb7921a02d7c15c0bbf245c8690dcb56ab2cda9254c5bb929
hovercard-subject-tagissue:3341653560
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/coder/coder/19467/issue_layout
twitter:imagehttps://opengraph.githubassets.com/b654f9f6c75808393bee7294eddc5e749ae8b3625d9b11105cbdd57f6ae3888e/coder/coder/issues/19467
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/b654f9f6c75808393bee7294eddc5e749ae8b3625d9b11105cbdd57f6ae3888e/coder/coder/issues/19467
og:image:altSummary The Coder agent is losing authorization and API connectivity before run_on_stop scripts complete execution, violating the documented behavior and preventing proper workspace shutdown operat...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamebjornrobertsson
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/coder/coder git https://github.com/coder/coder.git
octolytics-dimension-user_id95932066
octolytics-dimension-user_logincoder
octolytics-dimension-repository_id440752086
octolytics-dimension-repository_nwocoder/coder
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id440752086
octolytics-dimension-repository_network_root_nwocoder/coder
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/coder/coder/issues/19467#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fcoder%2Fcoder%2Fissues%2F19467
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fcoder%2Fcoder%2Fissues%2F19467
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=coder%2Fcoder
Reloadhttps://github.com/coder/coder/issues/19467
Reloadhttps://github.com/coder/coder/issues/19467
Reloadhttps://github.com/coder/coder/issues/19467
coderhttps://github.com/coder
coderhttps://github.com/coder/coder
Codehttps://github.com/coder/coder
Issues320 (320)https://github.com/coder/coder/issues
Pull requests470 (470)https://github.com/coder/coder/pulls
Discussionshttps://github.com/coder/coder/discussions
Actionshttps://github.com/coder/coder/actions
Projectshttps://github.com/coder/coder/projects
Security and quality49 (49)https://github.com/coder/coder/security
Insightshttps://github.com/coder/coder/insights
https://github.com/coder/coder
New issuehttps://github.com/login?return_to=https://github.com/coder/coder/issues/19467
New issuehttps://github.com/login?return_to=https://github.com/coder/coder/issues/19467
Bughttps://github.com/coder/coder/issues?q=type:"Bug"
#21538https://github.com/coder/coder/pull/21538
Bug: Agent loses authorization before run_on_stop script completeshttps://github.com/coder/coder/issues/19467#top
#21538https://github.com/coder/coder/pull/21538
https://github.com/mafredri
https://github.com/bjornrobertsson
bjornrobertssonhttps://github.com/bjornrobertsson
on Aug 21, 2025https://github.com/coder/coder/issues/19467#issue-3341653560
Terraform provider documentationhttps://registry.terraform.io/providers/coder/coder/latest/docs/resources/script
investigate why run_on_stop doesn't work consistently #17108https://github.com/coder/coder/issues/17108
Support for running a script on workspace delete #13387https://github.com/coder/coder/issues/13387
bjornrobertssonhttps://github.com/bjornrobertsson
Bughttps://github.com/coder/coder/issues?q=type:"Bug"
on Aug 21, 2025https://github.com/coder/coder/issues/19467#event-19265894521
bjornrobertssonhttps://github.com/bjornrobertsson
customer-reportedDO NOT USE. Instead, add to the project and fill in "Customer".https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22customer-reported%22
customer-requestedDO NOT USE. Instead, add to the project and fill in "Customer".https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22customer-requested%22
on Aug 21, 2025https://github.com/coder/coder/issues/19467#event-19265894620
matifalihttps://github.com/matifali
s1Bugs that break core workflows. Only humans may set this.https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22s1%22
customer-reportedDO NOT USE. Instead, add to the project and fill in "Customer".https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22customer-reported%22
customer-requestedDO NOT USE. Instead, add to the project and fill in "Customer".https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22customer-requested%22
on Aug 21, 2025https://github.com/coder/coder/issues/19467#event-19266950609
mafredrihttps://github.com/mafredri
on Jan 16, 2026https://github.com/coder/coder/issues/19467#event-22095698814
mafredrihttps://github.com/mafredri
on Jan 21, 2026https://github.com/coder/coder/issues/19467#event-22187011984
feat(coderd): allow agent auth during workspace shutdownhttps://github.com/coder/coder/commit/85001baed62371144d6fc0d05fd16eb82656448a
https://docs.github.com/articles/closing-issues-via-commit-messages
85001bahttps://github.com/coder/coder/commit/85001baed62371144d6fc0d05fd16eb82656448a
mafredrihttps://github.com/mafredri
completedhttps://github.com/coder/coder/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
97e8a5bhttps://github.com/coder/coder/commit/97e8a5b093971e26bfe24659e256fa086dd4f56c
on Jan 21, 2026https://github.com/coder/coder/issues/19467#event-22188012207
pullhttps://github.com/apps/pull
on Jan 21, 2026https://github.com/coder/coder/issues/19467#event-22188526693
fix(coderd): allow agent auth during workspace shutdown (coder#21538)https://github.com/PterX/coder/commit/97e8a5b093971e26bfe24659e256fa086dd4f56c
coder#21538https://github.com/coder/coder/pull/21538
https://docs.github.com/articles/closing-issues-via-commit-messages
97e8a5bhttps://github.com/PterX/coder/commit/97e8a5b093971e26bfe24659e256fa086dd4f56c
https://github.com/mafredri
mafredrihttps://github.com/mafredri
on Jan 22, 2026https://github.com/coder/coder/issues/19467#issuecomment-3784042526
#21538https://github.com/coder/coder/pull/21538
#6175https://github.com/coder/coder/issues/6175
linearhttps://github.com/apps/linear
s1Bugs that break core workflows. Only humans may set this.https://github.com/coder/coder/issues?q=state%3Aopen%20label%3A%22s1%22
on Feb 13, 2026https://github.com/coder/coder/issues/19467#event-22775568422
Sign up for freehttps://github.com/signup?return_to=https://github.com/coder/coder/issues/19467
Sign in to commenthttps://github.com/login?return_to=https://github.com/coder/coder/issues/19467
mafredrihttps://github.com/mafredri
Bughttps://github.com/coder/coder/issues?q=type:"Bug"
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.