Skip to content

Commit

Permalink
node: remove potential deadlock on chan capacity of zero (cosmos#1116)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview
There was a potential deadlock for a channel capacity of zero. This code
removes that deadlock and adds a comment explaining the code more.

Related to cosmos#1069 

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [ ] New and updated code has appropriate documentation
- [ ] New and updated code has new and/or updated testing
- [ ] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [ ] Linked issues closed with keywords
  • Loading branch information
MSevey authored Jul 31, 2023
1 parent dbb3bb7 commit 4c903bc
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions node/full_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,14 +829,16 @@ func (c *FullClient) eventsRoutine(sub cmtypes.Subscription, subscriber string,
select {
case msg := <-sub.Out():
result := ctypes.ResultEvent{Query: q.String(), Data: msg.Data(), Events: msg.Events()}
if cap(outc) == 0 {
outc <- result
} else {
select {
case outc <- result:
default:
c.Logger.Error("wanted to publish ResultEvent, but out channel is full", "result", result, "query", result.Query)
}
select {
case outc <- result:
default:
// The default case can happen if the outc chan
// is full or if it was initialized incorrectly
// with a capacity of 0. Since this function has
// no control over re-initializing the outc
// chan, we do not block on a capacity of 0.
full := cap(outc) != 0
c.Logger.Error("wanted to publish ResultEvent, but out channel is full:", full, "result:", result, "query:", result.Query)
}
case <-sub.Cancelled():
if sub.Err() == cmpubsub.ErrUnsubscribed {
Expand Down

0 comments on commit 4c903bc

Please sign in to comment.