Skip to content

Commit

Permalink
Fixed enter member from internal presence map data race issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Nov 21, 2023
1 parent ace96a0 commit 6443077
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ably/proto_presence_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ func Test_internal_presencemap_RTP17(t *testing.T) {
presenceMsgCh = make(chan *ably.PresenceMessage, 16)

c, _ = ably.NewRealtime(
ably.WithToken("fake:token"),
ably.WithKey("Auth:Key"),
ably.WithAutoConnect(false),
ably.WithChannelRetryTimeout(channelRetryTimeout),
ably.WithRealtimeRequestTimeout(realtimeRequestTimeout),
Expand Down Expand Up @@ -1231,6 +1231,6 @@ func Test_internal_presencemap_RTP17(t *testing.T) {

// Enter from internal map
// ablytest.Soon.Recv(t, nil, out, t.Fatalf)
// ablytest.Instantly.Recv(t, nil, out, t.Fatalf)
// ablytest.Soon.Recv(t, nil, out, t.Fatalf)
})
}
11 changes: 9 additions & 2 deletions ably/realtime_presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,23 @@ func syncSerial(msg *protocolMessage) (noChannelSerial bool, syncSequenceId stri

func (pres *RealtimePresence) enterMembersFromInternalPresenceMap() {
pres.mtx.Lock()
internalMembers := pres.internalMembers
internalMembers := make([]*PresenceMessage, len(pres.internalMembers))
indexCounter := 0
for _, member := range pres.internalMembers {
internalMembers[indexCounter] = member
indexCounter = indexCounter + 1
}
pres.mtx.Unlock()
for _, member := range internalMembers {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
// RTP17g
err := pres.EnterClient(context.Background(), member.ClientID, member.Data)
err := pres.EnterClient(ctx, member.ClientID, member.Data)
// RTP17e
if err != nil {
pres.channel.log().Errorf("Error for internal member presence enter with id %v, clientId %v, err %v", member.ID, member.ClientID, err)
pres.channel.emitErrorUpdate(newError(91004, err), true)
}
cancel()
}
}

Expand Down

0 comments on commit 6443077

Please sign in to comment.