mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-13 20:04:38 +02:00
Fix race condition on status in Relay.Publish method and failure to send
A race-condition exists between setting of the (unprotected) status and the callback which sets the status upon receiving an OK. The message is sent which can receive an OK in separate goroutine (setting status) prior to the status being set to 'sent.' The OK can be received prior to the status being set. This fix also sets the status to PublishStatusFailed if the WriteJSON call fails.
This commit is contained in:
committed by
fiatjaf_
parent
9b89a49e5e
commit
b077a41f83
3
relay.go
3
relay.go
@@ -305,10 +305,11 @@ func (r *Relay) Publish(ctx context.Context, event Event) (Status, error) {
|
||||
// publish event
|
||||
message := []any{"EVENT", event}
|
||||
debugLog("{%s} sending %v\n", r.URL, message)
|
||||
status = PublishStatusSent
|
||||
if err := r.Connection.WriteJSON(message); err != nil {
|
||||
status = PublishStatusFailed
|
||||
return status, err
|
||||
}
|
||||
status = PublishStatusSent
|
||||
|
||||
sub := r.PrepareSubscription(ctx)
|
||||
sub.SetLabel("publish-check")
|
||||
|
Reference in New Issue
Block a user