Repository navigation
Commit 4925039
fix(driver-sqlite-wasm): disconnect() waits for its final flush instead of the pool 5 s destroy timeout (#37450)
<!-- claude-projects-attribution: {"github_login":"os-bill"} -->
_Requested by **Bill** · [project
thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wg8ZBQe4UHadd2fGHGeWRW9V)_
repo:objectstack#5 · os-bill · session_01Utp6CbdHk3orAeensH6EzS
Fixes #37429
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:开发用的本地数据库在停止服务或启动被拒时,数据一定先写到磁盘再返回;正常情况下用户无感。
- 为什么做:合并队列里一个升级测试时好时坏,把无关的变更踢出队列;查明原因是本地数据库在关闭时最多只等五秒落盘。
- 出错会怎样:最坏是关闭服务时多等几秒;改动只在一个驱动里,可直接撤回。
- 要你做什么:无。
## What changes
- **Root cause** (full evidence on the card):
`SqliteWasmDriver.disconnect()` handed its final flush to the
connection's `close()`, which runs as the Knex pool's destroyer. tarn
waits at most `destroyTimeoutMillis` (5000 ms by default) for a
destroyer, then resolves anyway. So when a whole-image write and fsync
took longer than 5 s on a loaded runner, `disconnect()` returned with
the flush still in flight. A refused boot's rollback (the
datasource-admin plugin's `disconnectAll()`) therefore returned before
the additive schema sync was on disk, and step 1b of the dogfood test
read the v17 bytes. CI run 45751259665: rollback at 04:04:42.799,
eviction 5.005 s later, then 1b red.
- **Fix**: `disconnect()` now awaits `flush()` itself, with no timeout,
before it destroys the pool, and does so only when the pool holds a
connection. A flush that fails is thrown once the pool is destroyed
(try/finally).
- **Pin**: a new top-level `describe` in
`sqlite-wasm-driver-durability.test.ts`. A pool `destroyTimeoutMillis:
50` and a 400 ms fsync stand in for the loaded disk. The file must hold
the committed row as soon as `disconnect()` returns.
- **Rework round (L1 S1/S2, nits N1/N3)**:
- S1: a pin covers the published error path. When the final flush
rejects, `disconnect()` rejects with that same error, and the pool has
already been released (Knex's `client.pool` is gone).
- S2: `disconnect()` memoises the disconnect in flight. A second call
made while the first is destroying the pool now awaits the same promise,
instead of waiting out Knex's 60 s acquire timeout on a pool that is
being destroyed. The memo clears once that disconnect settles. A pin
covers it: the destroyer is held for 300 ms, the second call is made
after `pool.destroyed`, and it must settle in under 2 s.
- N1: the code comment now names what bounds the await.
- N3: the test's pool config is typed (Knex `PoolConfig`, no `as any`),
and the test asserts the pool's effective `destroyTimeoutMillis` is 50.
- N2: the patch of the worker-wide `FileHandle#sync` notes that it
relies on vitest's per-file isolation.
- The dogfood test is unchanged. There is no skip, retry, quarantine or
timeout change.
**Read first** (by risk):
`packages/drivers/driver-sqlite-wasm/src/sqlite-wasm-driver.ts`, the
`disconnect()` hunk (try/finally order; the `hasLiveConnection` guard
keeps a driver that never connected from opening the file). Then the
durability test hunk.
**Two-minute check**: `pnpm --filter @objectstack/driver-sqlite-wasm
exec vitest run src/sqlite-wasm-driver-durability.test.ts`. Expect 4
passed. Delete the `await this.flush()` line and it turns red with
`expected [] to deeply equal [ 'a1' ]`.
## Tests
<details><summary>Package suite, ablation, deterministic repro, 20
repeats</summary>
- `pnpm --filter @objectstack/driver-sqlite-wasm test`: Test Files 41
passed (41), Tests 769 passed (769). `typecheck` exit 0, and
`--listFiles` shows it covers the durability test file.
- Ablation through `scripts/ablation-replace.mjs`: the anchor `if
(this.hasLiveConnection()) await this.flush();` went from 1 hit to 0,
and the blob changed from 9a6e95c to e2c4a600. The run then failed: `1
failed | 3 passed`, `AssertionError: expected [] to deeply equal [ 'a1'
]`. Restore: the blob is back to 9a6e95c, equal to HEAD, and `git diff
HEAD` is empty. The test imports `../src/index.js`, so no build was
needed.
- Deterministic repro (a scratch probe, deleted and never committed):
the dogfood file with every `FileHandle.sync` in the worker delayed.
- 6000 ms, before the fix: rollback, eviction 5.008 s later, 1b reads 4
columns, `2 failed | 2 passed`. This matches the CI signature.
- 6000 ms, after the fix (driver `dist/` rebuilt, marker confirmed in
dist): eviction about 10.7 s after rollback (the flush was awaited), `4
passed`.
- 1500 ms, before the fix: green. Under the 5 s timeout, the defect
cannot show.
- Rework head: `pnpm --filter @objectstack/driver-sqlite-wasm test`
gives Test Files 41 passed (41), Tests 771 passed (771); `typecheck`
exit 0. Memo ablation (`if (!this.disconnecting) {` replaced by `if
(true) {`, anchor 1 hit to 0, blob 9df1279 to 24c78a23): the overlap
pin goes red with `Test timed out in 10000ms`, `1 failed | 5 passed`.
Restored: blob equals HEAD and `git diff HEAD` is empty.
- 20 repeated runs of
`test/organization-ownership-boot-refusal-order.dogfood.test.ts` on the
fixed tree (`pnpm --filter @objectstack/dogfood exec vitest run
--maxWorkers=2 FILE`, 20 separate invocations under the verify lock):
`SUMMARY pass=20 fail=0`, each `Tests 4 passed (4)`.
</details>
## Acceptance notes
- Reach beyond this test: any graceful stop of a file-backed sqlite-wasm
datasource whose final flush takes more than 5 s used to return before
the data was on disk. The sqlite-wasm driver is the default fallback for
`os dev`, and a process that exits straight after a stop loses those
writes. Covered by the same fix.
- Rollback vs shutdown asymmetry, tracked in #37462 (filed by the area
under #37429). A failed boot's `rollbackStartedPlugins()` destroys only
plugins whose `start()` succeeded, while `performShutdown()` destroys
every plugin with a `destroy`. `DefaultDatasourcePlugin` is not
init-only: it has `init`, `start` and `destroy`, so this rollback skips
it only when its `start()` has not completed. It did not cause #37429,
because the datasource-admin plugin's `destroy()` closes the default
pool.
- The await in `disconnect()` has no timeout of its own. A kernel's
graceful `shutdown()` caps it with `shutdownTimeout`; a refused boot's
rollback has no cap, by design, because returning before the data is on
disk is the defect this PR fixes.
## Verification
<details><summary>Local gates (derived with --commands, narrowed to this
diff)</summary>
- 62 of the 64 derived commands exited 0.
- NOT MEASURED, reason environmental (CI runs both):
- `node scripts/check-plugin-teardown-shape.mjs --self-test`: exit 3,
its pinned positive-control commit is unreachable from this shallow
clone.
- `pnpm check:dual-build-cjs-loads`: exit 3, PREREQUISITE NOT MET,
unrelated packages have no `dist/` here.
- `--ran` reconciliation: 64 derived, 62 run, the 2 above reported as
unrun.
- `node scripts/pm/overlap-check.mjs --branch=HEAD`: VERDICT clean.
- origin/main merged once before opening. The merge brought in
driver-sql schema-sync changes, so the driver suite and the dogfood file
were re-run on the merged head.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01Utp6CbdHk3orAeensH6EzS
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6CbdHk3orAeensH6EzS -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent a46efa8 commit 4925039
3 files changed
Lines changed: 172 additions & 2 deletions
File tree
- .changeset
- packages/drivers/driver-sqlite-wasm/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
159 | 178 | | |
160 | 179 | | |
161 | 180 | | |
| |||
164 | 183 | | |
165 | 184 | | |
166 | 185 | | |
167 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
168 | 216 | | |
169 | 217 | | |
170 | 218 | | |
| |||
0 commit comments