Skip to content
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

Concurrent INSERT statements hanging #526

Open
tpickett66 opened this issue Apr 11, 2024 · 2 comments
Open

Concurrent INSERT statements hanging #526

tpickett66 opened this issue Apr 11, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@tpickett66
Copy link

What version of Chai are you using?

v0.16.0

What did you do?

I'm trying to build up a listing of similar data from several sources concurrently. When I got everything wired up I noticed my program wasn't making progress; after much debugging I realized things were hanging at db.Exec() while trying to insert into the database. The runtime isn't reporting a deadlock.

Below is an example that reproduces the behavior I'm seeing.

package main

import (
	"fmt"
	"os"
	"sync"

	"github.com/chaisql/chai"
)

const iterations int = 10
const concurrency int = 2

func main() {
	db, err := chai.Open(":memory:")
	if err != nil {
		panic(err)
	}
	err = db.Exec("CREATE TABLE vals (id text not null)")
	if err != nil {
		panic(err)
	}

	var wg sync.WaitGroup
	for i := 1; i <= concurrency; i++ {
		worker := i
		wg.Add(1)
		go func() {
			defer wg.Done()
			insertThings(worker, db)
		}()
	}

	wg.Wait()
}

func insertThings(workerNum int, db *chai.DB) {
	for j := 1; j < iterations; j++ {
		val := fmt.Sprintf("%d-%d", workerNum, j)
		fmt.Fprintf(os.Stdout, "Inserting %s.\n", val)

		err := db.Exec("INSERT INTO vals (id) VALUES (?)", val)
		if err != nil {
			panic(err)
		}
		fmt.Fprintf(os.Stdout, "inserted.\n", val)
	}
}

What did you expect to see?

I expected to see about 40 lines of output with each "worker" announcing an insert then success for the numbers 1-10.

What did you see instead?

I get 2-4 lines of output then an indefinite pause.

Example Output:

$ go run main.go
Inserting 2-1.
Inserting 1-1.
@tpickett66 tpickett66 added the bug Something isn't working label Apr 11, 2024
@tpickett66
Copy link
Author

I just tried again using the latest commit and a separate Connection for each worker, same result.

@kksh3ll
Copy link

kksh3ll commented May 21, 2024

Is anybody here ,hello,hello ~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants