Commit de617db
fix(cli): release terraform state lock when interrupting diff/deploy (#284)
### Related issue
Fixes #283
### Description
Interrupting `cdktn diff` or `cdktn deploy` (Ctrl-C) left terraform's
state lock held, so the next run failed with `Error acquiring the state
lock`.
The cause was that the CLI's abort handler tore terraform down instead
of letting it exit on its own. Terraform shares the CLI's process group,
so Ctrl-C already reaches it directly and starts its graceful shutdown
(which releases the lock) — but the CLI killed it before that shutdown
could finish. The fix is to stop tearing terraform down on abort and
instead wait for it to exit:
- **diff/plan**
([`@cdktn/commons/src/util.ts`](packages/@cdktn/commons/src/util.ts)):
`exec()` no longer forwards the abort to `child_process.spawn`'s
`signal` option, which killed the child with SIGTERM and rejected the
promise immediately (synthetic `AbortError`) before terraform had exited
— so terraform never ran its graceful shutdown and the lock leaked. It
now settles on the child's own `close`, so the run resolves only once
terraform has exited and released the lock.
- **deploy/destroy**
([`@cdktn/cli-core/src/lib/models/deploy-machine.ts`](packages/@cdktn/cli-core/src/lib/models/deploy-machine.ts)):
the xstate machine previously went straight to the final `stopped` state
on `STOP`, killing the pty and resolving the run before terraform had
finished shutting down. It now passes through an intermediate `stopping`
state that marks the run cancelled and waits for terraform's natural
`EXITED` before reaching `stopped`. The pty stays alive throughout, so
terraform exits on its own and the run only resolves afterwards.
**Why no extra signal is needed:** every abort trigger in the codebase
(`runCdktfProject`, `watch`) originates from
`process.on(SIGINT/SIGTERM/SIGQUIT)`, so terraform — sharing the process
group — always receives the signal directly. There is no abort path that
requires the CLI to deliver the signal itself.
**Verification:** confirmed after the fix that a single Ctrl-C produces
only `Interrupt received... Gracefully shutting down...` and terraform
exits cleanly on its own. The full lock-release path (interrupt a
locking operation, then re-run) still needs a final check against a
backend that locks state.
### Checklist
- [x] I have updated the PR title to match [CDKTN's style
guide](https://github.com/open-constructs/cdk-terrain/blob/main/CONTRIBUTING.md#pull-requests-1)
- [x] I have run the linter on my code locally
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the
[documentation](https://github.com/open-constructs/cdk-terrain-docs/tree/main/content)
if applicable
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works if applicable
- [x] New and existing unit tests pass locally with my changes
Co-authored-by: Simon Heather <simon.heather@yulife.com>1 parent 6d3657a commit de617db
3 files changed
Lines changed: 65 additions & 2 deletions
File tree
- packages/@cdktn
- cli-core/src
- lib/models
- test/models
- commons/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
78 | 82 | | |
79 | 83 | | |
80 | 84 | | |
| |||
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
233 | | - | |
| 237 | + | |
234 | 238 | | |
235 | 239 | | |
236 | 240 | | |
| |||
309 | 313 | | |
310 | 314 | | |
311 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
312 | 325 | | |
313 | 326 | | |
314 | 327 | | |
| |||
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
230 | 276 | | |
231 | 277 | | |
232 | 278 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
139 | 143 | | |
140 | | - | |
| 144 | + | |
141 | 145 | | |
142 | 146 | | |
143 | 147 | | |
| |||
0 commit comments