Fix race in ConnectWithTLS
where relay.Connection
can be set to nil
concurrently with writes, causing panics.
#183
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I am running
go-nostr v0.49.7
and recently got multiple times this panic:The function
ConnectWithTLS
is basically equivalent to the one in thego-nostr v0.51.10
, and that's why I am opening this PR. I have to say I was NOT able to reproduce the issue, but looking at the code there is a clear candidate.It's clear that here we have a race condition. Imagine
r.connectionContext
gets cancelled at the same timewriteRequest
is received from the channel.Then, (at least) two conditions are true in the
select
statement, one of which will randomly fire. Let's say it fires the channel, then we have a race condition.The first goroutine will set to
nil
theConnection
the relay is using withWriteMessage
.This PR should solve the issue.
I also removed the if in the
because it doesn't solve the problem:
r.Connection
might be non-nil when evaluated in theif
, and immediately after could be set tonil
by the first go-routine.