From 32fc2015543c41557a364d45213ca6c710b478bd Mon Sep 17 00:00:00 2001 From: Zhiheng Lin Date: Fri, 21 Apr 2023 22:17:21 +0800 Subject: [PATCH] fix: avoid coroutine leaks when the dialecter initialization fails. (#6249) Co-authored-by: Kevin Lin --- gorm.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gorm.go b/gorm.go index 4402a2df..07a913fc 100644 --- a/gorm.go +++ b/gorm.go @@ -179,6 +179,12 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { if config.Dialector != nil { err = config.Dialector.Initialize(db) + + if err != nil { + if db, err := db.DB(); err == nil { + _ = db.Close() + } + } } preparedStmt := &PreparedStmtDB{