-
Notifications
You must be signed in to change notification settings - Fork 4.7k
wp-env: Fix MySQL startup race condition causing database connection errors #75046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
wp-env: Fix MySQL startup race condition causing database connection errors #75046
Conversation
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @paolopiaggio, @peterfabian. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @LiamSarsfield! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
WordPress containers were starting before MySQL was ready to accept connections, causing intermittent "Error establishing a database connection" errors, especially in CI environments. This change: - Adds a healthcheck to mysql and tests-mysql services using mariadb-admin ping with root credentials - Updates WordPress depends_on to use condition: service_healthy - Starts both MySQL services in parallel for faster initialization - Removes redundant retry logic now handled by Docker Compose
6938ca8 to
825d42b
Compare
- Switch from mariadb-admin ping to MariaDB's official healthcheck.sh script with --connect and --innodb_initialized flags - Use generous timeouts (60s start_period, 12 retries) for slow CI - Fix clean() to start appropriate MySQL service(s) based on environment (previously always started only 'mysql' even when cleaning 'tests') - Remove unused checkDatabaseConnection function - Update tests to verify healthcheck configuration
- Updated MySQL healthcheck to utilize MariaDB's official healthcheck.sh script with the MARIADB_AUTO_UPGRADE environment variable, ensuring compatibility for both new and existing installations. - Adjusted related comments and tests to reflect the new healthcheck configuration.
What?
Closes #62242
Adds a MySQL healthcheck to wp-env to prevent the race condition where WordPress containers start before MySQL is ready to accept connections.
Why?
WordPress containers were starting before MySQL was ready to accept connections, causing intermittent "Error establishing a database connection" errors. This is especially problematic in CI environments where timing variations are more common.
The root cause is that Docker Compose's
depends_onin array format only waits for the container to start, not for the service to be ready. MySQL can take several seconds after container start to initialize and accept connections.How?
Added MySQL healthcheck using MariaDB's official
healthcheck.shscript with--connectand--innodb_initializedflags. This verifies both TCP connectivity and that the InnoDB storage engine is fully initialized.Added
MARIADB_AUTO_UPGRADE=1environment variable to MySQL containers. This ensures the healthcheck user is automatically created for existing wp-env installations that don't have it. Without this,healthcheck.shwould fail on existing databases. This is the recommended approach from MariaDB documentation.Used generous timeouts for CI environments - 60s start_period, 10s timeout, 12 retries (5s interval) provides ~2 minutes total window for slow CI runners.
Updated
depends_onforwordpressandtests-wordpressservices to use the object format withcondition: service_healthy, so they wait for MySQL to be healthy before starting.Start both MySQL services in parallel - Changed
dockerCompose.upOne('mysql')todockerCompose.upMany(['mysql', 'tests-mysql'])to allow both MySQL services to initialize concurrently while sources download.Fixed
clean()reliability - The clean command now starts only the MySQL service(s) needed for the environment being cleaned (previously always started onlymysqleven when cleaningtests).Removed dead code - Removed the
checkDatabaseConnectionfunction and associated retry logic since Docker Compose healthchecks now handle MySQL readiness.Testing Instructions
Setup
Clone this branch:
git clone -b fix/HOG-482-wp-env-mysql-healthcheck https://github.com/LiamSarsfield/gutenberg.git cd gutenberg npm installLink the local wp-env globally (so
wp-envcommand uses this PR's code):cd packages/env npm linkNavigate to any directory with a
.wp-env.jsonfile (or any plugin/theme):cd /path/to/your/projectTest basic functionality
wp-env destroy # Start fresh (optional but recommended) wp-env startVerify WordPress loads without "Error establishing a database connection".
Verify the healthcheck is working
Test the clean command
Test with existing installation (verifies MARIADB_AUTO_UPGRADE)
Run unit tests
cd /path/to/gutenberg npm run test:unit -- packages/env/lib/test/All 47 tests pass.
Cleanup
When done testing, unlink the local wp-env:
Testing Instructions for Keyboard
Not applicable - this PR has no user interface changes.
Screenshots or screencast
Not applicable - this PR has no visual changes.