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

Call to Close() hangs #25

Closed
JordanP opened this issue Oct 18, 2022 · 3 comments
Closed

Call to Close() hangs #25

JordanP opened this issue Oct 18, 2022 · 3 comments

Comments

@JordanP
Copy link

JordanP commented Oct 18, 2022

package main

import (
	"context"
	"log"
	"os/signal"
	"syscall"

	"github.com/jackc/pgx/v5/pgxpool"
	"golang.org/x/sync/errgroup"
)

func main() {
	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	db, err := pgxpool.New(ctx, "postgresql://XXX")
	if err != nil {
		log.Panicln(err)
	}
	g, ctx := errgroup.WithContext(ctx)
	g.SetLimit(8)

	loop := func() {
		for {
			select {
			case <-ctx.Done():
				return
			default:
			}
			g.Go(func() error {
				log.Println("Going to db.Exec")
				_, err := db.Exec(ctx, "select pg_sleep(0.2);")
				return err
			})
		}
	}

	loop()
	db.Close()
}

When running this script with go run main.go, if I hit ctrl+C, the program will sometimes hang on the db.Close() and more especially on the p.destructWG.Wait() line. It's pretty reliable, though you may want to try something like 10 times (go run + ctrl+c).

I couldn't debug it more :( I am on Linux go 1.19.

Edit: I noticed that if I hit ctrl+c quite early after the program started, there's a higher chance to reproduce the deadlock.

@JordanP
Copy link
Author

JordanP commented Oct 19, 2022

Hummm I tested #24 and I couldn't repro the issue with the PR applied

@jackc
Copy link
Owner

jackc commented Oct 22, 2022

I wonder if there is something specific to Linux. I tried it about 30 times on go version go1.19.2 darwin/amd64 and was unable to reproduce the issue.

Hmm, no luck on go version go1.19.1 linux/amd64 either.

Not really sure what else to try. OTOH, #24 will probably be merged sometime soon, so maybe it won't matter.

@JordanP
Copy link
Author

JordanP commented Oct 23, 2022

Maybe it has to do with my PG DB being 300ms away from my laptop.

I am going to close this one, I hope #24 will make it, but no rush,.

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

No branches or pull requests

2 participants