Skip to content

Disconnect every database connection when the test environment is torn down - #62471

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
GromNaN:fix/disconnect-database-connections-on-teardown
Nov 14, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
GromNaN:fix/disconnect-database-connections-on-teardown

Conversation

@GromNaN

@GromNaN GromNaN commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Test suites that boot one application per test keep the database connections of every application open until the cycle collector runs. The database manager and its connections form a reference cycle, and neither Application::flush() nor the test teardown closes the handles. A parallel suite of a few thousand tests can leave thousands of connections open at once, and the server refuses new ones.

RefreshDatabase and DatabaseTransactions already disconnect the connections they transact, but only for connectionsToTransact(), which defaults to the default connection. Every other connection stays open.

This disconnects every resolved connection of the application just before it is flushed, after the beforeApplicationDestroyed callbacks so they can still query.

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@GromNaN
GromNaN marked this pull request as ready for review November 13, 2026 20:58
@GromNaN
GromNaN force-pushed the fix/disconnect-database-connections-on-teardown branch from caf0a97 to c06c895 Compare November 13, 2026 20:59
@GromNaN GromNaN changed the title Disconnect database connections when the test environment is torn down Disconnect every database connection when the test environment is torn down Nov 13, 2026
@GromNaN

GromNaN commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@Outsidaz that might solve the 2nd issue you reported in mongodb/laravel-mongodb#3665 (comment)

@raghavji2050

Copy link
Copy Markdown
Contributor

Tested this on 13.x (c980cb5827) with the diff applied, on PHP 8.5.12.

InteractsWithTestCaseLifecycleTest passed (2 tests, 3 assertions). The sqlite in-memory connection is open before teardown and getRawPdo() is null afterwards, and a non-DatabaseManager db binding is left alone.

The placement also looks right: it runs after callBeforeApplicationDestroyedCallbacks() and the parallel testing teardown, and before $this->app->flush(), so callbacks can still query. Only the pre-existing PHPUnit warning: Failed to set "intl.default_locale=C.UTF-8".

@GromNaN

GromNaN commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

@raghavji2050 it would be helpful to test this patch on your real project if you use multiple database connections and experience issues.

@raghavji2050

Copy link
Copy Markdown
Contributor

I don't have a real project with multiple connections, so I tried it on a fresh app with sqlite, MySQL and Postgres. Without the patch, 34 MySQL and 34 Postgres connections stayed open until PHPUnit exited. With it they're closed after every test, and all 52 tests still pass, including RefreshDatabase and beforeApplicationDestroyed callbacks that query during teardown.

@taylorotwell
taylorotwell merged commit 4b06fcb into laravel:13.x Nov 14, 2026
62 checks passed
@GromNaN
GromNaN deleted the fix/disconnect-database-connections-on-teardown branch November 14, 2026 20:14
taylorotwell pushed a commit that referenced this pull request Nov 16, 2026
taylorotwell pushed a commit that referenced this pull request Nov 23, 2026
…wn (#62600)

Mocking the DB facade with DB::shouldReceive() swaps the db binding for
a Mockery mock of DatabaseManager. The teardown added in #62471 then
calls getConnections() on the mock, and tests fail with
BadMethodCallException although their own assertions pass.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants