API operations (/api/plan, /api/apply, drift endpoints) are not drained on graceful shutdown
Community Note
- Please vote on this issue by adding a reaction to the original issue to help the community and maintainers prioritize this request. Searching for pre-existing feature requests helps us consolidate datapoints for identical requirements into a single place, thank you!
- Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
- If you are interested in working on this issue or have submitted a pull request, please leave a comment.
Overview of the Issue
On SIGTERM/SIGINT, Atlantis waits for in-progress operations to finish before exiting ("Received interrupt. Waiting for in-progress operations to complete"). That wait only covers work that comes in through VCS webhooks. Operations started through the HTTP API are not tracked by the drainer, so they get killed during shutdown:
POST /api/planPOST /api/applyPOST /api/drift/detectPOST /api/drift/remediate
How it happens (code links pinned to main @ 668fc54):
Server.StartcallswaitForDrain(), thenserver.Shutdownwith a 5s timeout, then returns, and the process exits.- The only callers of
Drainer.StartOp()areRunAutoplanCommandandRunCommentCommand. APIController.Plan,Apply,RemediateandDetectDriftrun the whole operation synchronously inside the HTTP handler, andAPIControllerhas no reference to theDrainer.
So when the server gets SIGTERM while an API plan/apply is running, the drainer sees 0 in-progress operations (or only the webhook ones) and finishes right away. server.Shutdown then waits at most 5s for the open API request, and the process exits. The terraform/tofu process dies mid-run, even if the orchestrator (e.g. Kubernetes terminationGracePeriodSeconds) would have allowed much more time. The effects:
- Orphaned remote state locks (e.g. S3/DynamoDB) that have to be
force-unlocked by hand. POST /api/applyandPOST /api/drift/remediate(with apply) can be cut off partway through an apply.- The API caller gets a dropped connection instead of a clean error.
This matters most for drift detection and remediation, which are designed to be driven through the API by scheduled jobs, and for any rolling restart or node drain that happens while those jobs run.
Expected behavior: API operations are drained like webhook operations. In-flight API requests finish before the server exits, and new API requests during shutdown get a clear error (e.g. 503 Service Unavailable), the same way webhook commands get the "Atlantis is shutting down" comment.
Proposed fix (happy to open a PR once this is accepted): add the Drainer to APIController (wired in server.go). In Plan, Apply, Remediate and DetectDrift, call StartOp() before any setup (working dir, locks) and return 503 if it returns false, then defer OpDone(). Add tests for the 503 during shutdown and for ShutdownBlocking() waiting on an in-flight API request.
Reproduction Steps
- Run Atlantis with the API enabled (
--api-secretset) and a repo whose plan takes a while (e.g. anull_resourcewith alocal-execsleep 120, or any slow real project). - Start a plan via the API:
curl -sS -X POST https://<atlantis>/api/plan \ -H "X-Atlantis-Token: $ATLANTIS_API_SECRET" -H 'Content-Type: application/json' \ -d '{"Repository":"<owner>/<repo>","Ref":"main","Type":"Github","Paths":[{"Directory":"<dir>","Workspace":"default"}]}' - While the plan is running, send SIGTERM to the Atlantis server (e.g.
kubectl delete podwith a longterminationGracePeriodSeconds, orkill -TERM <pid>). - Observe: the server logs "All in-progress operations complete, shutting down" right away (or after webhook ops only), exits within ~5s, the curl request fails with a dropped connection, and the terraform process is killed. With a remote backend, the state lock is left behind.
- For comparison, repeat with
atlantis plancommented on a PR: the server waits for the plan to finish before exiting.
Logs
Not captured from a controlled repro yet; this was found while investigating orphaned state locks and confirmed by reading the code. Based on Start() / waitForDrain(), the expected log sequence with only an API plan in flight is:
Received interrupt. Waiting for in-progress operations to complete
All in-progress operations complete, shutting downwith no "Waiting for in-progress operations to complete, current in-progress ops: N" lines, even though the API plan is still running.
Environment details
- Atlantis version: v0.48.0 (also present on
main@ 668fc54) - Deployment method: Kubernetes StatefulSet (plain manifests), custom image based on the official Alpine image
- If not running the latest Atlantis version have you tried to reproduce this issue on the latest version: v0.48.0 is the latest release; the code paths above are unchanged on
main - Atlantis flags (relevant):
--enable-drift-detection,--enable-drift-remediation,--api-secret
Any other information you can provide about the environment/deployment: AWS, OpenTofu via Terragrunt custom workflows, S3 backend with state locking. Drift detection/remediation is triggered by a scheduled job through /api/drift/detect and /api/drift/remediate.
Additional Context
- Related: #5949 (best-effort graceful termination of terraform processes on shutdown). That issue is about signalling the terraform processes; this one is about API operations not being waited on at all.
- Original clean-shutdown work: #953, #1052. Recent drainer change: #6850.
- ADR 0002 (API Enhancement and Drift Detection) doesn't cover shutdown behavior for API-driven operations.
Source: runatlantis/atlantis