Commit Graph

13 Commits

Author SHA1 Message Date
alexcastgonz
81b617459b fix(db): bump client token and device last-used timestamps without a prior read (#5384)
* fix(db): bump client token and device last-used timestamps without a prior read

update_last_used() and update_last_seen_debounced() read the row with
session.get() and then updated it in the same transaction. When two requests
with the same Client API Token ran concurrently and both passed the debounce
check, the second UPDATE hit MariaDB error 1020 (Record has changed since last
read), so a read-only GET returned HTTP 500. Make each a single conditional
UPDATE that only touches the row when the debounce window has passed.

Fixes #5379

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* test(db): code-review fixes for last-used race tests

Reuse the executed_statements fixture, scope race listeners to
sync_engine, add the device race test, drop an unused fixture, and
apply trunk fmt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(db): simplify pass on last-used race fix

Share the race hook as tests/db_race_stubs.run_before_first, look tokens
up by id in tests, trim duplicated docstrings, and state the handler
comment as current behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(db): humanizer pass on last-used handler comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(db): run MariaDB and MySQL sessions at READ COMMITTED

MariaDB 11.6+ turns on innodb_snapshot_isolation, so at REPEATABLE READ any
transaction that reads a row and then writes it fails with error 1020
("Record has changed since last read") when another transaction committed
to that row in between. READ COMMITTED, PostgreSQL's default, takes a fresh
snapshot per statement and removes the whole class of failures.

A server logging binlog_format=STATEMENT refuses InnoDB writes below
REPEATABLE READ, so its sessions keep the default and log a warning once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(auth): skip last-used and last-active writes inside the debounce window

Client-token auth bumped the token, the device and the user on every
request, the user with an undebounced UPDATE plus a re-SELECT. The token's
loaded last_used_at now gates the token and device bumps, and
set_last_active checks the loaded last_active and bumps through a
debounced conditional UPDATE, so a request inside the window writes
nothing and takes no row lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-10-29 12:23:31 -04:00
Sam Dornan
3b4e230b7c Upgrade SQLAlchemy to 2.1 (#5007) 2026-10-01 19:17:30 -04:00
Georges-Antoine Assi
656c85bc46 refactor(tasks): simplify pass on the task catalog split
Loads every model where the engine is built rather than per job, so only
processes that touch the ORM pay for it. Resolves task paths with RQ's
import_attribute, references the scan failure callback by path, hoists
the recommendations builder's registry import now that the registry
loads no task modules, and gates push-pull on its spec.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 11:06:27 -04:00
Sam Dornan
3750d6ff70 chore(mypy): require full annotations outside tests, type the setup responses (#4795) 2026-09-26 15:31:00 -04:00
Sam Dornan
9a19d94ec5 fix(backend): type DML row counts from Session.execute for mypy (#4718) 2026-09-22 18:24:12 -04:00
Georges-Antoine Assi
21d9a7f3c6 refactor(db): give the pool recycle sentinel one home
An operator's `0` means "off", but SQLAlchemy gates recycling on
`_recycle > -1`, so a literal 0 recycles on every checkout: a fresh
connect and auth handshake per pool checkout. Mapping it to -1 is
therefore load-bearing, but doing it at the engine left the value
defined in `config` and corrected in `base_handler`, so a second
consumer could reasonably pass the raw constant and reintroduce the
footgun. Fold the mapping into the constant.

Also drop the `wait_timeout` figure the comment attributed to MariaDB.
Its default is 28800s; 600s is a value some hosts lower it to, not a
MariaDB one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-10 10:48:54 -04:00
Georges-Antoine Assi
6b2ca96397 fix(db): recycle pooled connections before the server drops them
The engine sets `pool_pre_ping` but no `pool_recycle`, so a connection
the database closed for being idle is still handed out, and the ping on
that half-closed socket blocks the worker until TCP gives up: tens of
seconds to minutes of an unresponsive API for every request that draws
it, while a fresh connection to the same server answers instantly.

Default to 300s, under the lowest `wait_timeout` MariaDB is commonly
configured with, and make it settable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-09 20:25:36 -04:00
Michael Manganiello
e4e3928d1b misc: Apply import sorting 2025-09-04 11:17:00 -03:00
Georges-Antoine Assi
8ed107336f only print queries once 2025-08-21 22:34:07 -04:00
Georges-Antoine Assi
03e2bac425 fix ordering rows in table view 2025-03-23 19:35:53 -04:00
Georges-Antoine Assi
0607323005 log SQL queries and exec time in dev mode 2025-03-22 09:49:13 -04:00
Michael Manganiello
a85c84a7d4 misc: Use single SQLAlchemy engine and session maker
As recommended by SQLAlchemy [1], this change makes a single
instantiation of the database engine and session maker, instead of one
entity per handler.

It also uses the provided `URL` constructor to better define the
database URL structure.

[1] https://docs.sqlalchemy.org/en/20/core/connections.html#basic-usage
2024-08-21 09:56:28 -03:00
Georges-Antoine Assi
9ad28f3294 more refactoring again 2024-05-05 16:32:40 -04:00