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

Fix rare deadlock on multiple concurrent conn.Close() calls. #227

Merged
Merged
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 connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func (me *Connection) shutdown(err *Error) {
if err != nil {
me.errors <- err
}
close(me.errors) // Since the channel is buffered, the waiting goroutine will recieve the result.

me.conn.Close()

Expand Down Expand Up @@ -618,7 +619,10 @@ func (me *Connection) call(req message, res ...message) error {
}

select {
case err := <-me.errors:
case err, ok := <-me.errors:
if !ok {
return ErrClosed
}
return err

case msg := <-me.rpc:
Expand Down
21 changes: 21 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package amqp

import (
"log"
"sync"
"testing"
)

Expand Down Expand Up @@ -34,3 +35,23 @@ func TestQueueDeclareOnAClosedConnectionFails(t *testing.T) {
log.Fatalf("queue.declare on a closed connection %s is expected to fail", conn)
}
}

func TestConcurrentClose(t *testing.T) {
conn, err := Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Could't connect to amqp server, err = %s", err)
}

wg := sync.WaitGroup{}
wg.Add(5)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I change this value to something higher than 5, I cannot get the suite to pass 20 times in a row, e.g.

for i in {1..20}; do go test -v -tags integration_test.go .\/...; done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I figured it out.

for i := 0; i < 5; i++ {
go func() {
err := conn.Close()
if err != nil && err != ErrClosed {
log.Fatalf("Expected nil or ErrClosed - got %#v, type is %T", err, err)
}
wg.Done()
}()
}
wg.Wait()
}