Commit 8edd54b
fix(resource): atomic single-shot deprovision on DELETE — close double-deprovision TOCTOU
Design-review follow-up to #229. The idempotent-DELETE fix used a
`resource.Status == "deleted"` PRE-READ before SoftDeleteResource +
deprovision — a check-then-act TOCTOU: two concurrent DELETEs both read
status='active', both pass the pre-read, and BOTH fire the destructive
provisioner Deprovision / storage Deprovision RPC against the same backend.
That is the truehomie-db DROP incident class (an active customer DB dropped
via a path that re-ran teardown).
Fix: make the soft-delete the authoritative atomic guard. New
models.SoftDeleteResourceIfActive does `UPDATE ... SET status='deleted'
WHERE id=$1 AND status != 'deleted'` and returns RowsAffected>0. The handler
deprovisions ONLY when this call won the transition (deleted==true); any
racer or sequential retry gets deleted==false → idempotent
{"ok":true,"already_deleted":true} with NO second deprovision. Removes the
redundant pre-read (the rows-affected check subsumes it and is race-safe).
SoftDeleteResource is kept unchanged for the ~18 provision-rollback callers
that always operate on a just-created row.
Tests: models.TestSoftDeleteResourceIfActive_Branches (sqlmock: won / no-op /
error — 100% of the new fn). The existing TestResourceDelete_Idempotent_
DoubleDelete now exercises the rows-affected !deleted branch via a sequential
retry (200 already_deleted, no re-deprovision). All changed handler lines
covered. Verified vs real Postgres+Redis.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 893bed9 commit 8edd54b
3 files changed
Lines changed: 74 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
231 | 232 | | |
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
235 | 242 | | |
236 | 243 | | |
237 | 244 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 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 | + | |
237 | 265 | | |
238 | 266 | | |
239 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
457 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
458 | 460 | | |
459 | 461 | | |
460 | 462 | | |
| |||
465 | 467 | | |
466 | 468 | | |
467 | 469 | | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
468 | 492 | | |
469 | 493 | | |
470 | 494 | | |
| |||
676 | 700 | | |
677 | 701 | | |
678 | 702 | | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
683 | 708 | | |
684 | 709 | | |
685 | 710 | | |
| |||
0 commit comments