Commit 3f95cd4
Serialize local sqlite initialization under a file lock
With WEB_CONCURRENCY=2, both gunicorn workers import the app
concurrently on a fresh instance; both could pass the sqlite
exists-check and race initialize() — the losing worker died at boot on
'UNIQUE constraint failed: policy.id' (observed as 503 bursts during
the first real Cloud Run scale-out, Stage 5 LB validation), and a
worker could also observe a created-but-unseeded database and skip
initialization entirely, serving with empty reference tables. An
exclusive fcntl lock around the exists-check + initialize means exactly
one process initializes while the others wait, then see the complete
file and skip.
Empirical verification (6 concurrent fresh boots x 3 rounds): unfixed
code failed every round (boot crashes and row counts of 0/2/5 across
workers); fixed code passes with all workers seeing exactly 5 seed
rows.
Fixes #3746
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 56d8c69 commit 3f95cd4
2 files changed
Lines changed: 16 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
109 | | - | |
110 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
111 | 124 | | |
112 | 125 | | |
113 | 126 | | |
| |||
0 commit comments