Skip to content

Cap the prepared statement cache of the database pools - #79

Merged
Musiker15 merged 1 commit into
mainfrom
fix/cap-prepared-statements
Nov 29, 2026
Merged

Musiker15 merged 1 commit into
mainfrom
fix/cap-prepared-statements

Conversation

@Musiker15

Copy link
Copy Markdown
Member

What happened

On 24 Nov the MariaDB server ran out of prepared statements. From 01:05 until the scheduled reboot at 05:30 every database client on the server failed with ER_MAX_PREPARED_STMT_COUNT_REACHED: the shop, the giveaway bot, msk-paste, msk-shortener, and the paste, shortener and stripe-reconcile cron jobs.

MariaDB allows 16382 prepared statements for the whole server (max_prepared_stmt_count). mysql2 keeps up to 16000 per connection unless told otherwise, and every distinct SQL text is its own statement. listImages() in lib/images.ts writes LIMIT and OFFSET into the SQL text, so each gallery page number made a new statement on each of the ten pool connections. The OpenSpider crawl from 21 to 24 Nov requested about 24,000 gallery pages with ?page=N, which was enough to fill the limit.

The change

  • lib/dbLimits.ts holds MAX_PREPARED_STATEMENTS = 128. It sits in its own module so the giveaway pool can use it without counting as a shop database module in tests/routeGuards.test.ts.
  • lib/db.ts and lib/giveawayDb.ts pass it as maxPreparedStatements. When the cache is full, mysql2 closes the least recently used statement, so the shop now holds at most 1920 statements instead of up to 240,000.
  • tests/dbPoolConfig.test.ts checks that both pools set the option and stay within a budget of 2000 together. It fails without the change.

mysql2 3.24.6 is the latest release, and the 16000 default is intended behaviour, so there is no upstream fix to pick up.

Checks

  • npx vitest run: 96 files, 1527 tests pass
  • npx tsc --noEmit and ESLint on the changed files are clean

mysql2 keeps up to 16000 prepared statements per connection by default,
while MariaDB allows 16382 for the whole server. Every distinct SQL text
is its own statement, and the gallery writes LIMIT and OFFSET into the
text, so each page number made a new one on each of the ten connections.
The OpenSpider crawl from 21 to 24 Nov filled the server's limit at 01:05
on the 24th. From then on every database client on the server failed with
ER_MAX_PREPARED_STMT_COUNT_REACHED until the scheduled reboot at 05:30,
including the paste and shortener cleanup jobs and stripe-reconcile.

Both pools now keep at most 128 statements per connection. mysql2 closes
the least recently used one when it needs room, so the shop holds at most
1920 instead of up to 240000. A new test checks the budget of both pools.
@github-actions

Copy link
Copy Markdown

⚠️ Deprecation Warning: The deny-licenses option is deprecated for possible removal in the next major release. For more information, see issue 997.

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: TypeScript

TypeScript / code-coverage/vitest

main edc702f fix/cap-prepared-sta... ad27d60 +/-
73% 73% 0%
View coverage of the 2 most impacted files.
File main edc702f fix/cap-prepared-sta... ad27d60 +/-
lib/giveawayDb.ts 0% 50% +50%
lib/dbLimits.ts 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

@Musiker15
Musiker15 merged commit ce0ab7e into main Nov 29, 2026
11 checks passed
@Musiker15
Musiker15 deleted the fix/cap-prepared-statements branch November 29, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant