Repository navigation
fix(storage): close request database connections - #171
Conversation
@greptile review |
[Medium impact] The PR appears safe to merge; no blocking issues were found. SummaryThe PR closes Peewee connections after each request, including failed requests, and closes the initialization connection before serving traffic.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Create datastore] --> B[Close initialization connection]
B --> C[Create API and register routes]
C --> D[Receive request]
D --> E[Database work opens a thread-local connection]
E --> F{Request outcome}
F --> G[Success]
F --> H[Error]
G --> I[Request teardown closes the connection]
H --> I
Reviews (3) · Last reviewed commit: "style: match CI formatter for database r..." · Reviewed by Greptile |
@greptile review |
5ea124f to
4d9443b
Compare
|
Thanks @0xbrayo. Read through the diff: closing the init connection (after aw-core's WAL setup) and releasing the Peewee thread-local connection in |
🤖 AI code reviewThis PR adds teardown handling in AWFlask to close Peewee database connections after each request, and closes the initialization connection before serving traffic. It also adds a new test file with three SQLite concurrency regression tests and updates the Makefile test target to include the new test file. Safe to merge — no P0/P1 findingsConfidence 5/5 ✅ No findings in this PR's changed files. What follows is outside them — not about this change, and not scored. 1 out-of-scope finding (outside this PR's changed files)Each item here is an observation about the PR as a whole — informational only, not scored. The teardown_request handler closes the database connection unconditionally after every request, but the handler is registered only when the storage strategy is PeeweeStorage. However, the handler closes the connection even if the request did not open a connection in that thread. Peewee's connection is thread-local; the teardown handler runs in the same thread that handled the request, so it will close the connection that the request used. But the handler also runs for requests that did not touch the database at all (e.g., static file requests or the root route). In that case, database.is_closed() may be False if the connection was opened by a previous request in the same thread (since Flask may reuse threads from a pool). Closing the connection after a request that didn't use it is harmless because Peewee will lazily reopen it on next use. However, the bigger issue is that the teardown handler closes the connection even when the request is still using it? No, teardown runs after the response is returned, so the request is done. The real problem: the teardown handler closes the connection, but the connection might be shared with the main thread? No, it's thread-local. Actually, the issue is that the teardown handler is registered inside the init method, but it captures the Consensus: 2/3 passes agreed — pass 1 looked and disagreed Files changed (3) — the diff as I read it
Reviewed Maintainer commands
|
4d9443b to
ad560ec
Compare
@greptile review |
Closes #100.
Close each request thread's Peewee connection during Flask teardown, including when a request fails, and release the initialization connection before serving traffic. The required aw-core 0.5.18 already enables SQLite WAL; this completes the server-side connection lifecycle so abandoned connections and cursors do not accumulate across requests.
Add file-backed SQLite regressions for reads during a write transaction, writes during a read transaction, and connection cleanup after an exception. Run these through
make teston all CI platforms.Validation: 50 tests passed locally and on Windows, Linux, and macOS CI, including all three new regressions; Ruff, Black, mypy, and
git diff --checkpassed. All three new tests fail against the original server because request connections remain open. WAL already permits the overlapping operations on the current dependency.