fix: avoid coroutine leaks when the dialecter initialization fails.#6249
Merged
jinzhu merged 1 commit intogo-gorm:masterfrom Apr 21, 2023
onlyice:fix/resource-leak
Merged
fix: avoid coroutine leaks when the dialecter initialization fails.#6249jinzhu merged 1 commit intogo-gorm:masterfrom onlyice:fix/resource-leak
jinzhu merged 1 commit intogo-gorm:masterfrom
onlyice:fix/resource-leak
Conversation
Contributor
Author
|
After observing the results of unit tests, it is believed that the failed tests are not related to this PR. |
3 tasks
3 tasks
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 did this pull request do?
When calling
gorm.Open, there is the following code:config.Dialector.Initialize(db)typically callssql.OpenDBto create an*sql.DB, and starts an goroutinedb.connectionOpener(ctx).Next, a typical database driver will attempt to connect to the schema specified in the DSN. If an error occurs at this time (such as the specified schema not existing), it will cause
config.Dialector.Initialize(db)to return an error. However, after receiving this error, GORM does not attempt to close*sql.DB. This causes thedb.connectionOpener(ctx)goroutine to leak.In my scenario, I used the wrong schema and retried calling
gorm.Openmultiple times. By observing the dumped goroutine, I found that there were a large number ofdb.connectionOpener(ctx)goroutines. After applying this code fix, these goroutines are no longer leaked.This pull request try to close the
*sql.DBvariable if possible.User Case Description
Same as above.