Skip to content

Deadlock on sqlite with PrepareStmt = true #783

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func init() {
log.Printf("failed to connect database, got error %v\n", err)
}

sqlDB.SetMaxOpenConns(1)

RunMigrations()
if DB.Dialector.Name() == "sqlite" {
DB.Exec("PRAGMA foreign_keys = ON")
Expand Down Expand Up @@ -69,7 +71,9 @@ func OpenTestConnection() (db *gorm.DB, err error) {
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
default:
log.Println("testing sqlite3...")
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{
PrepareStmt: true, // set this to false and the test passes
})
}

if debug := os.Getenv("DEBUG"); debug == "true" {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ require (
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gorm.io/datatypes v1.2.4 // indirect
gorm.io/hints v1.1.2 // indirect
gorm.io/plugin/dbresolver v1.5.3 // indirect
)

replace gorm.io/gorm => ./gorm
63 changes: 60 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"context"
"sync"
"testing"
"time"
)

const concurrentReads = 40

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver
Expand All @@ -13,8 +18,60 @@ func TestGORM(t *testing.T) {

DB.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
testRunSuccessful := false
wgSuccess := sync.WaitGroup{}
wgSuccess.Add(concurrentReads)

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

start := make(chan struct{})

for i := 0; i < concurrentReads/2; i++ {
go func() {
t.Logf("Entered routine 1-%d", i)
var result User

<-start
transaction := DB.Begin()
if err := transaction.First(&result, "id = ? ", 1).Error; err != nil {
transaction.Rollback()
return
}
transaction.Commit()
t.Log("Got User from routine 1")
wgSuccess.Done()
}()
}

for i := 0; i < concurrentReads/2; i++ {
go func() {
t.Logf("Entered routine 2-%d", i)
var result User

<-start
if err := DB.First(&result, "id = ? ", 1).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
return
}
t.Log("Got User from routine 2")
wgSuccess.Done()
}()
}

time.Sleep(200 * time.Millisecond)
close(start)
t.Log("Started routines")

go func() {
wgSuccess.Wait()
testRunSuccessful = true
}()

<-ctx.Done()
if !testRunSuccessful {
t.Fatalf("Test failed")
}

t.Logf("Test completed")
}
Loading