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

connection shutdown, channel close data race #196

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ services:
env:
- AMQP_URL=amqp://guest:guest@127.0.0.1:5672/ GOMAXPROCS=2

script: go test -v -tags integration ./...
script: go test -v -race -tags integration ./...
15 changes: 11 additions & 4 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func (me *Channel) open() error {
// Performs a request/response call for when the message is not NoWait and is
// specified as Synchronous.
func (me *Channel) call(req message, res ...message) error {
if err := me.send(me, req); err != nil {
me.m.Lock()
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a separate mutex for sending stuff, me.sendM. I think we should use it here.

Copy link
Contributor Author

@imkira imkira Nov 24, 2016

Choose a reason for hiding this comment

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

The original code is very misleading.
Please note that m.send starts as sendOpen and then switches to sendClosed:

https://github.com/imkira/amqp/blob/0563adcdb2a14308873fce26b14d3637dc2ac685/channel.go#L85
https://github.com/imkira/amqp/blob/0563adcdb2a14308873fce26b14d3637dc2ac685/channel.go#L104

Please, also note that each of those 2 functions use the mutex you specified for actually sending data.

What I am doing here is to protect a race condition that could happen if we used me.send directly.
That would collide with the following modification:

https://github.com/imkira/amqp/blob/0563adcdb2a14308873fce26b14d3637dc2ac685/channel.go#L104

send := me.send
me.m.Unlock()

if err := send(me, req); err != nil {
return err
}

Expand Down Expand Up @@ -273,22 +277,26 @@ func (me *Channel) dispatch(msg message) {
}

case *basicAck:
me.m.Lock()
if me.confirming {
if m.Multiple {
me.confirms.Multiple(Confirmation{m.DeliveryTag, true})
} else {
me.confirms.One(Confirmation{m.DeliveryTag, true})
}
}
me.m.Unlock()

case *basicNack:
me.m.Lock()
if me.confirming {
if m.Multiple {
me.confirms.Multiple(Confirmation{m.DeliveryTag, false})
} else {
me.confirms.One(Confirmation{m.DeliveryTag, false})
}
}
me.m.Unlock()

case *basicDeliver:
me.consumers.send(m.ConsumerTag, newDelivery(me, m))
Expand Down Expand Up @@ -1476,17 +1484,16 @@ exception could occur if the server does not support this method.

*/
func (me *Channel) Confirm(noWait bool) error {
me.m.Lock()
defer me.m.Unlock()

if err := me.call(
&confirmSelect{Nowait: noWait},
&confirmSelectOk{},
); err != nil {
return err
}

me.m.Lock()
me.confirming = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@imkira imkira Nov 29, 2016

Choose a reason for hiding this comment

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

Thanks for pointing out. I fixed it in c6a5ab1

me.m.Unlock()

return nil
}
Expand Down
Loading