eventsource icon indicating copy to clipboard operation
eventsource copied to clipboard

Wait for stream writes to stop before closing channels

Open pboyd opened this issue 5 years ago • 3 comments

I was getting some test failures under the race detector after calling stream.Close. The goroutine that processes events was writing to the channel that Close closed. It looks like the same problem as #39, so at the very least here's a test case to reproduce that.

I attempted to fix it by using a sync.WaitGroup to keep stream.Close from closing the channel until the other goroutine stopped (stream()). But by itself that just caused a deadlock, because nothing triggered stream() to stop. So I also had to cancel the outbound request.

The non-deprecated way to cancel an HTTP request is by canceling the context, so that's what I did. That was added in Go 1.7, which is perhaps a problem. I can rewrite it to use Request.Cancel if that's more palatable.

pboyd avatar Jun 25 '19 02:06 pboyd

I made one small tweak to this. It was calling stream.retryRestartStream after Close was called. retryRestartStream sleeps before it checks if the connection was closed, so that, combined with the wait in Close, had the effect of making Close take 3s (or whatever stream.retry happens to be).

pboyd avatar Jun 28 '19 13:06 pboyd

Bump..

perpetual-hydrofoil avatar Mar 03 '20 23:03 perpetual-hydrofoil

Ping...

floodkoff avatar Aug 06 '20 14:08 floodkoff