go-libp2p icon indicating copy to clipboard operation
go-libp2p copied to clipboard

eventbus: kill associated subscriptions when the subscriber closes the channel

Open raulk opened this issue 6 years ago • 4 comments

Use recover() to recover from the panic when sending on a closed channel.

raulk avatar Jun 19 '19 22:06 raulk

Oh, I was almost certain that that happens

magik6k avatar Jun 20 '19 06:06 magik6k

No, wait, we shouldn't allow closing channel when subs are running, it will lead to memory leaks when for whatever reason that becomes a habbit.

How about a sub option that would close the channel when canceling the sub?

magik6k avatar Jun 20 '19 06:06 magik6k

Yet another thing in Go that makes me nervous: channels don’t have an owner that’s responsible for closure :D Normally it’s the creator who controls the lifespan of a channel.

Closing on subscription cancel is dangerous with this design because a consumer may have registered the same channel twice for different subscriptions.

I guess we have to take the small chance of memory leak (we can document that if closing the channel, one must drain it to prevent leaks), or switch to the eventbus returning receive only channels, and owning those.

raulk avatar Jun 20 '19 07:06 raulk

switch to the eventbus returning receive only channels

There is no way to do that without manual casting in Go

Ideally channels had some form of refcounting, which they also can't have in Go, so alternatively we can:

  • Say that subscribe takes ownership of the channel, and is responsible for closing it
  • Create a special container for refcounting so subs with multiple channels still work

I guess it could improve the situation, but I have no idea how to make it not annoyingly verbose.

Or we can just leave things as they are, maybe implementig some more runtime validation, somehow..

magik6k avatar Jun 20 '19 09:06 magik6k