hub icon indicating copy to clipboard operation
hub copied to clipboard

Fix panic on closed channel

Open andpago opened this issue 3 years ago • 5 comments

Fix panic in subscribers

When hub publishes messages, it first finds all matching subscribers and then calls sub.Set(m) on them:

// Publish will send an event to all the subscribers matching the event name.
func (h *Hub) Publish(m Message) {
	for k, v := range h.fields {
		m.Fields[k] = v
	}

	for _, sub := range h.matcher.Lookup(m.Topic()) {
		sub.Set(m)
	}
}

However, if Unsubscribe is called after the filtering but before the Set, the subscriber's underlying channel is closed and Set tries to write into a closed channel.

This PR adds channels and goroutines responsible for closing the channel.

andpago avatar Oct 19 '20 10:10 andpago

Hey, Nice catch! Thanks for the work on this Pull Request. I think I added this bug on PR #12 🙈

I fixed the CI process, now it should work.

leandro-lugaresi avatar Oct 23 '20 01:10 leandro-lugaresi

The modifications are approved. Thanks for the work here! I just fixed (I think) the GitHub actions to work on forks. Can you rebase or merge with the main branch?

leandro-lugaresi avatar Oct 26 '20 05:10 leandro-lugaresi

I had to add an RWMutex to stop go test from getting angry at the data race. Codecov reports a decrease in code coverage, but everything in subscriber.go should be covered (at least GoLand says so). I think this happens because to get a 100% coverage you need to run the tests many times

andpago avatar Oct 26 '20 07:10 andpago

About the codecov, you are right, We will ignore the patch. Fix the last comment and we are ready to merge

leandro-lugaresi avatar Oct 26 '20 13:10 leandro-lugaresi

Codecov Report

Merging #13 into main will decrease coverage by 0.59%. The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #13      +/-   ##
==========================================
- Coverage   87.09%   86.50%   -0.60%     
==========================================
  Files           4        4              
  Lines         310      326      +16     
==========================================
+ Hits          270      282      +12     
- Misses         30       33       +3     
- Partials       10       11       +1     
Flag Coverage Δ
#unittests 86.50% <77.77%> (-0.60%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
subscriber.go 89.47% <77.77%> (-10.53%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 99dcacb...de0df9d. Read the comment docs.

codecov-io avatar Oct 26 '20 14:10 codecov-io