Title: bug: agent hangs on shutdown when a cron script is running (deadlock in agentscripts.Runner.Close) · Issue #32461 · coder/coder · GitHub
Open Graph Title: bug: agent hangs on shutdown when a cron script is running (deadlock in agentscripts.Runner.Close) · Issue #32461 · coder/coder
X Title: bug: agent hangs on shutdown when a cron script is running (deadlock in agentscripts.Runner.Close) · Issue #32461 · coder/coder
Description: Is there an existing issue for this? I have searched the existing issues Current Behavior If a workspace stops while a coder_script with cron set is still running, the agent never finishes shutting down. On SIGTERM the agent process keep...
Open Graph Description: Is there an existing issue for this? I have searched the existing issues Current Behavior If a workspace stops while a coder_script with cron set is still running, the agent never finishes shutting...
X Description: Is there an existing issue for this? I have searched the existing issues Current Behavior If a workspace stops while a coder_script with cron set is still running, the agent never finishes shutting...
Opengraph URL: https://github.com/coder/coder/issues/32461
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bug: agent hangs on shutdown when a cron script is running (deadlock in agentscripts.Runner.Close)","articleBody":"### Is there an existing issue for this?\n\n- [x] I have searched the existing issues\n\n### Current Behavior\n\nIf a workspace stops while a `coder_script` with `cron` set is still running, the agent never finishes shutting down. On SIGTERM the agent process keeps running until it is killed. On Kubernetes the pod stays in `Terminating` for the full `terminationGracePeriodSeconds` and then gets SIGKILLed. Shutdowns that happen while no cron run is in progress finish normally.\n\nThe cause is a lock-ordering deadlock in `Runner.Close()` in [`agent/agentscripts/agentscripts.go`](https://github.com/coder/coder/blob/5f61a045ab6c50b93603b520fea62f2911ac232a/agent/agentscripts/agentscripts.go#L553-L575):\n\n1. `Close()` sets `closing`, releases `closeMutex`, and waits on `executeWait` with the mutex released. The comment there explains why: in-flight `Execute` calls report timings through `trackCommandGoroutine`, which takes `closeMutex`.\n2. It then takes `closeMutex` again with `defer Unlock()`, closes `r.closed`, cancels the cron context, and blocks on `\u003c-r.cron.Stop().Done()` ([L571](https://github.com/coder/coder/blob/5f61a045ab6c50b93603b520fea62f2911ac232a/agent/agentscripts/agentscripts.go#L571)) **while still holding the mutex**.\n3. `cron.Stop()` waits for running jobs. The cron script that is in flight is cancelled, and when its command returns, `run()` reports the timing through `r.trackCommandGoroutine(...)` ([L456](https://github.com/coder/coder/blob/5f61a045ab6c50b93603b520fea62f2911ac232a/agent/agentscripts/agentscripts.go#L456)), which blocks on `closeMutex`.\n\nThe cron job never returns, so `cron.Stop()` never finishes and `Close()` never returns.\n\n### Relevant Log Output\n\n```shell\n# goroutine dump of the unfixed code at the commit above, taken while Close() hangs (runtime frames trimmed)\ngoroutine 39 [chan receive]:\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).Close(...)\n\tagent/agentscripts/agentscripts.go:571\n\ngoroutine 90 [sync.Mutex.Lock]:\nsync.(*Mutex).Lock(...)\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).trackCommandGoroutine(...)\n\tagent/agentscripts/agentscripts.go:577\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).run.func3()\n\tagent/agentscripts/agentscripts.go:456\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).run(...)\n\tagent/agentscripts/agentscripts.go:547\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).trackRun(...)\n\tagent/agentscripts/agentscripts.go:323\ngithub.com/coder/coder/v2/agent/agentscripts.(*Runner).Init.func1()\n\tagent/agentscripts/agentscripts.go:165\ngithub.com/robfig/cron/v3.FuncJob.Run(...)\ngithub.com/robfig/cron/v3.(*Cron).startJob.func1()\n```\n\n### Expected Behavior\n\n`Close()` cancels the in-flight cron run, waits for it, and returns. `trackCommandGoroutine` should see that the runner is closed and return an error instead of blocking, which is what already happens for in-flight `Execute` calls.\n\n### Steps to Reproduce\n\n1. Use a template with a `coder_script` that runs on a schedule and takes a few seconds, for example:\n ```hcl\n resource \"coder_script\" \"periodic\" {\n agent_id = coder_agent.main.id\n display_name = \"periodic\"\n cron = \"*/30 * * * * *\"\n script = \"sleep 20\"\n }\n ```\n2. Start the workspace and wait for the script to start a run.\n3. Stop the workspace (or send SIGTERM to the agent) while the run is in progress.\n4. The agent does not exit until it is force-killed.\n\nMinimal Go reproduction in `agent/agentscripts`: `Init` with one script whose `Cron` is `\"* * * * * *\"` and whose command sleeps, call `StartCron()`, wait for the script to start, then call `Close()`. `Close()` never returns.\n\n### Environment\n\n- Host OS: Linux (Kubernetes)\n- Coder version: v2.39.4; also reproduced on `main` at 5f61a045ab6c50b93603b520fea62f2911ac232a\n\n### Additional Context\n\nThe issue occurs consistently, I have tested this on the latest version\n\nI have a fix with a regression test ready and will open a PR shortly.\n","author":{"url":"https://github.com/simonepri","@type":"Person","name":"simonepri"},"datePublished":"2026-11-19T17:12:35Z","url":"https://github.com/coder/coder/issues/32461"}
| react-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4581edfa-2c62-7b31-004f-0dc38ce02248 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCAE:176DDE:1BDEAC:2572EA:6B043098 |
| html-safe-nonce | 446af3227cd67bd392f1790010537df78c7f6f4c6d3f392079d1c77d2f52af10 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0FFOjE3NkRERToxQkRFQUM6MjU3MkVBOjZCMDQzMDk4IiwidmlzaXRvcl9pZCI6IjY4NzM0ODU2NDMwNTQ1OTIxNTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 775924c74ede9772a221be18c0219e9f150867fb2bfb25a9f677d01562c5345e |
| hovercard-subject-tag | issue:6495038264 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/coder/coder/32461/issue_layout |
| twitter:image | https://opengraph.githubassets.com/71695a2af68df6bf4c33d238ce2fca3b59f201e55046d33e010f23bd48a2816c/coder/coder/issues/32461 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/71695a2af68df6bf4c33d238ce2fca3b59f201e55046d33e010f23bd48a2816c/coder/coder/issues/32461 |
| og:image:alt | Is there an existing issue for this? I have searched the existing issues Current Behavior If a workspace stops while a coder_script with cron set is still running, the agent never finishes shutting... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | simonepri |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/coder/coder git https://github.com/coder/coder.git |
| octolytics-dimension-user_id | 95932066 |
| octolytics-dimension-user_login | coder |
| octolytics-dimension-repository_id | 440752086 |
| octolytics-dimension-repository_nwo | coder/coder |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 440752086 |
| octolytics-dimension-repository_network_root_nwo | coder/coder |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width