Commit 9f31074
committed
fix(storage): wrapper tx-handle forwarding + real-pool mutex bypass
Addresses Copilot review on PR #458. Five related fixes flagged after the
real-pool dedicated-client refactor exposed gaps in the wrapper layer
and a perf regression in the per-method mutex.
PostgresTabularStorage — mutex bypass on real `pg.Pool`:
The per-instance mutex was wrapping every public method, serializing
reads and writes that previously fanned across separate pool clients
in parallel. With the dedicated-client `withTransaction` path, the
mutex is no longer needed for tx isolation on real pool — `pool.connect()`
provides that. The mutex now short-circuits to a no-op when the handle
exposes `connect()`, and only serializes on the single-connection path
(PGlite / PGLitePool) where it is required to keep external traffic
out of the open transaction.
TelemetryTabularStorage.withTransaction — forward inner tx handle:
Previously called `inner.withTransaction(() => fn(this))`, throwing
away the inner's tx proxy and passing the outer wrapper to `fn`. With
the new createTxView proxy, `fn(this).put(...)` would re-enter the
inner's PUBLIC put through the mutex-acquiring path and deadlock on
single-connection backends, or run on the wrong connection on real
pool. Now constructs a fresh `TelemetryTabularStorage` wrapping the
inner's `tx` proxy and passes that to `fn`, so writes inside the
callback flow through the transaction-bound resources.
ScopedTabularStorage.withTransaction — forward + defer events:
Same forwarding fix as Telemetry. Additionally, since Scoped emits
events on its own local emitter (not via the inner's `put` events),
the inner's deferred-event flush does not cover Scoped's outer events.
The tx wrapper's emitter is replaced with a per-tx buffer; entries
are flushed to the parent's emitter only after `inner.withTransaction`
resolves successfully (rollbacks discard via promise rejection).
CachedTabularStorage.withTransaction — explicit override:
Previously inherited `BaseTabularStorage`'s no-op default, silently
losing rollback / atomicity for callers using the cached wrapper.
Now forwards to the durable store's `withTransaction` so writes inside
`fn` are atomic. The cache layer is bypassed for the transaction's
duration; documented post-tx staleness is repopulated on the first
read miss.
ITabularStorage.withTransaction JSDoc — bring docs in step:
Previous text claimed the API throws on a real `pg.Pool`, said the
storage instance is not isolated from concurrent callers, and stated
that `tx === this` for every backend — all out of date after the
recent refactor. Updated to describe: real-pool path uses `pool.connect()`;
concurrent calls are isolated (mutex on single-conn, dedicated client
on real pool); the `tx` handle is a Proxy distinct from `this`, and
callers MUST use it to avoid deadlock or wrong-connection writes.
767 storage tests passing locally.1 parent cad033c commit 9f31074
5 files changed
Lines changed: 119 additions & 27 deletions
File tree
- packages
- knowledge-base/src/knowledge-base
- postgres/src/storage
- storage/src/tabular
Lines changed: 36 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
| 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 | + | |
231 | 266 | | |
232 | 267 | | |
233 | 268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
657 | 657 | | |
658 | 658 | | |
659 | 659 | | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
664 | | - | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
665 | 669 | | |
666 | 670 | | |
667 | 671 | | |
668 | 672 | | |
669 | 673 | | |
670 | 674 | | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
671 | 678 | | |
| 679 | + | |
672 | 680 | | |
673 | 681 | | |
674 | 682 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
331 | 356 | | |
332 | 357 | | |
333 | 358 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
316 | 317 | | |
317 | 318 | | |
318 | 319 | | |
319 | 320 | | |
320 | 321 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
329 | 333 | | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
335 | 347 | | |
336 | 348 | | |
337 | 349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
140 | 146 | | |
141 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
142 | 154 | | |
143 | 155 | | |
144 | 156 | | |
| |||
0 commit comments