Repository navigation
Cap the prepared statement cache of the database pools - #79
Merged
Merged
Conversation
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.
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
Code coverage overviewLanguages: TypeScript TypeScript / code-coverage/vitest
View coverage of the 2 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()inlib/images.tswritesLIMITandOFFSETinto 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.tsholdsMAX_PREPARED_STATEMENTS = 128. It sits in its own module so the giveaway pool can use it without counting as a shop database module intests/routeGuards.test.ts.lib/db.tsandlib/giveawayDb.tspass it asmaxPreparedStatements. 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.tschecks 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 passnpx tsc --noEmitand ESLint on the changed files are clean