Skip to content

Commit

Permalink
Fix unblock mutex
Browse files Browse the repository at this point in the history
- if `receivedEvent.ID` not match `event.ID`, may trigger an error `fatal error: sync: unlock of unlocked mutex`.
- if context cancled, it does not needs mutex.
  • Loading branch information
waybackarchiver authored and fiatjaf committed Feb 5, 2023
1 parent 4aee139 commit ab2db2d
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
}

sub := r.Subscribe(ctx, Filters{Filter{IDs: []string{event.ID}}})
defer mu.Unlock()
for {
select {
case receivedEvent := <-sub.Events:
if receivedEvent.ID == event.ID {
// we got a success, so update our status and proceed to return
mu.Lock()
status = PublishStatusSucceeded
mu.Unlock()
return status
}
case <-ctx.Done():
Expand All @@ -250,7 +250,6 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
// e.g. if this happens because of the timeout then status will probably be "failed"
// but if it happens because okCallback was called then it might be "succeeded"
// do not return if okCallback is in process
mu.Lock()
return status
}
}
Expand Down

0 comments on commit ab2db2d

Please sign in to comment.