~emersion/soju-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
4 3

[PATCH soju v2] fix goroutine leak

Details
Message ID
<20240701094312.99524-1-ch@bitfehler.net>
DKIM signature
pass
Download raw message
Patch: +1 -0
As is, soju leaks goroutines on client disconnects, because the closure
started as goroutine in `newConn()` never finishes. It gets stuck in the
for loop annotated as "draining the outgoing channel", because the
outgoing channel is in fact never closed.

This commit fixes the issue by calling `dc.Close()` when an
`eventDownstreamDisconnected` is being handled, which closes the
connection and the channel, and lets all goroutines finish.
---
v2: doesn't panic on server shutdown

It took me a while to untangle the complexity of multiple clients being
connected to multiple networks, etc. I think this fix is much closer to
the root cause, but there is still some uncertainty on my part. Feedback
would be much appreciated.

 user.go | 1 +
 1 file changed, 1 insertion(+)

diff --git a/user.go b/user.go
index c3a3931..dcaf968 100644
--- a/user.go
+++ b/user.go
@@ -791,6 +791,7 @@ func (u *user) run() {
			})

			u.bumpDownstreamInteractionTime(ctx)
			dc.Close()
		case eventDownstreamMessage:
			msg, dc := e.msg, e.dc
			if dc.isClosed() {
-- 
2.45.2

[soju/patches/.build.yml] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<D2E3TA7D9MXL.R1OWVORWQH5F@fra01>
In-Reply-To
<20240701094312.99524-1-ch@bitfehler.net> (view parent)
DKIM signature
missing
Download raw message
soju/patches/.build.yml: SUCCESS in 1m7s

[fix goroutine leak][0] v2 from [Conrad Hoffmann][1]

[0]: https://lists.sr.ht/~emersion/soju-dev/patches/53616
[1]: ch@bitfehler.net

✓ #1264959 SUCCESS soju/patches/.build.yml https://builds.sr.ht/~emersion/job/1264959
Details
Message ID
<07mxdCDbldd80ttTRda71vDE_Dsb0gKjklsMvvn2eiJ4dfen4_-m5nsBTXjS_lt8xMrq7YzrGHgBUyGwFJRjC87KYhZPXb9h7kUioPP7QvI=@emersion.fr>
In-Reply-To
<20240701094312.99524-1-ch@bitfehler.net> (view parent)
DKIM signature
pass
Download raw message
I actually think your first patch was a good fix: it ensures that the
connection is properly closed in all cases.

The panic comes from a footgun I'm responsible for. The *conn pointer
returned by newConn is dereferenced in newDownstreamConn and
connectToUpstream (a premature optimization). As a result, the whole
struct is copied and the internal newConn goroutine works with a
different chunk of memory than downstreamConn and upstreamConn.

I've fixed that in 94ff7d49b116 ("Stop dereferencing *conn").
Details
Message ID
<e0b5bb19-4324-4207-bf47-4f85e72dd519@bitfehler.net>
In-Reply-To
<07mxdCDbldd80ttTRda71vDE_Dsb0gKjklsMvvn2eiJ4dfen4_-m5nsBTXjS_lt8xMrq7YzrGHgBUyGwFJRjC87KYhZPXb9h7kUioPP7QvI=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On 7/8/24 8:35 PM, Simon Ser wrote:
> I actually think your first patch was a good fix: it ensures that the
> connection is properly closed in all cases.
> 
> The panic comes from a footgun I'm responsible for. The *conn pointer
> returned by newConn is dereferenced in newDownstreamConn and
> connectToUpstream (a premature optimization). As a result, the whole
> struct is copied and the internal newConn goroutine works with a
> different chunk of memory than downstreamConn and upstreamConn.
> 
> I've fixed that in 94ff7d49b116 ("Stop dereferencing *conn").

Cool, thanks. That would have definitely taking me a long time to figure 
out :)

Does the first patch still apply? Or do you want me to open a PR on 
codeberg or something?

Cheers,
Conrad
Details
Message ID
<5C_vb9vS7D559NKJSEClEDz-e821rQUpTwICnGaSPrYtuS1gUrqPzQIYhJutyYpWa6tXv-fmxNMCGxwgXl8dDCOOOYifuIUtCBzOta7UR88=@emersion.fr>
In-Reply-To
<e0b5bb19-4324-4207-bf47-4f85e72dd519@bitfehler.net> (view parent)
DKIM signature
pass
Download raw message
On Monday, July 8th, 2024 at 22:36, Conrad Hoffmann <ch@bitfehler.net> wrote:

> Does the first patch still apply? Or do you want me to open a PR on
> codeberg or something?

Yup, I just wanted to hear back from you to make sure it all makes
sense. Pushed now, thanks!
Reply to thread Export thread (mbox)