Skip to content

Commit ad5fb5c

Browse files
authored
prevent concurrent writes to ctx maps (keybase#23634)
* prevent concurrent writes to ctx maps * Fix
1 parent 526b35b commit ad5fb5c

1 file changed

Lines changed: 44 additions & 17 deletions

File tree

‎go/chat/globals/context.go‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package globals
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/keybase/client/go/chat/types"
78
"github.com/keybase/client/go/libkb"
@@ -21,6 +22,7 @@ type localizerCancelableKeyTyp int
2122
type messageSkipsKeyTyp int
2223
type unboxModeKeyTyp int
2324
type emojiHarvesterKeyTyp int
25+
type ctxMutexKeyTyp int
2426

2527
var kfKey keyfinderKey
2628
var inKey identifyNotifierKey
@@ -33,6 +35,7 @@ var localizerCancelableKey localizerCancelableKeyTyp
3335
var messageSkipsKey messageSkipsKeyTyp
3436
var unboxModeKey unboxModeKeyTyp
3537
var emojiHarvesterKey emojiHarvesterKeyTyp
38+
var ctxMutexKey ctxMutexKeyTyp
3639

3740
type identModeData struct {
3841
mode keybase1.TLFIdentifyBehavior
@@ -91,30 +94,45 @@ func CtxModifyIdentifyNotifier(ctx context.Context, notifier types.IdentifyNotif
9194
}
9295

9396
func CtxAddRateLimit(ctx context.Context, rl []chat1.RateLimit) {
94-
val := ctx.Value(rlKey)
95-
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
96-
for _, r := range rl {
97-
existingRL[r.Name] = r
97+
val := ctx.Value(ctxMutexKey)
98+
if l, ok := val.(sync.RWMutex); ok {
99+
l.Lock()
100+
defer l.Unlock()
101+
val = ctx.Value(rlKey)
102+
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
103+
for _, r := range rl {
104+
existingRL[r.Name] = r
105+
}
98106
}
99107
}
100108
}
101109

102110
func CtxRateLimits(ctx context.Context) (res []chat1.RateLimit) {
103-
val := ctx.Value(rlKey)
104-
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
105-
for _, rl := range existingRL {
106-
res = append(res, rl)
111+
val := ctx.Value(ctxMutexKey)
112+
if l, ok := val.(sync.RWMutex); ok {
113+
l.RLock()
114+
defer l.RUnlock()
115+
val = ctx.Value(rlKey)
116+
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
117+
for _, rl := range existingRL {
118+
res = append(res, rl)
119+
}
107120
}
108121
}
109122
return res
110123
}
111124

112125
func CtxAddMessageCacheSkips(ctx context.Context, convID chat1.ConversationID, msgs []chat1.MessageUnboxed) {
113-
val := ctx.Value(messageSkipsKey)
114-
if existingSkips, ok := val.(map[chat1.ConvIDStr]MessageCacheSkip); ok {
115-
existingSkips[convID.ConvIDStr()] = MessageCacheSkip{
116-
ConvID: convID,
117-
Msgs: append(existingSkips[convID.ConvIDStr()].Msgs, msgs...),
126+
val := ctx.Value(ctxMutexKey)
127+
if l, ok := val.(sync.RWMutex); ok {
128+
l.Lock()
129+
defer l.Unlock()
130+
val = ctx.Value(messageSkipsKey)
131+
if existingSkips, ok := val.(map[chat1.ConvIDStr]MessageCacheSkip); ok {
132+
existingSkips[convID.ConvIDStr()] = MessageCacheSkip{
133+
ConvID: convID,
134+
Msgs: append(existingSkips[convID.ConvIDStr()].Msgs, msgs...),
135+
}
118136
}
119137
}
120138
}
@@ -125,10 +143,15 @@ type MessageCacheSkip struct {
125143
}
126144

127145
func CtxMessageCacheSkips(ctx context.Context) (res []MessageCacheSkip) {
128-
val := ctx.Value(messageSkipsKey)
129-
if existingSkips, ok := val.(map[chat1.ConvIDStr]MessageCacheSkip); ok {
130-
for _, skips := range existingSkips {
131-
res = append(res, skips)
146+
val := ctx.Value(ctxMutexKey)
147+
if l, ok := val.(sync.RWMutex); ok {
148+
l.RLock()
149+
defer l.RUnlock()
150+
val = ctx.Value(messageSkipsKey)
151+
if existingSkips, ok := val.(map[chat1.ConvIDStr]MessageCacheSkip); ok {
152+
for _, skips := range existingSkips {
153+
res = append(res, skips)
154+
}
132155
}
133156
}
134157
return res
@@ -238,6 +261,10 @@ func ChatCtx(ctx context.Context, g *Context, mode keybase1.TLFIdentifyBehavior,
238261
if _, ok := val.(types.UPAKFinder); !ok {
239262
res = context.WithValue(res, upKey, g.CtxFactory.NewUPAKFinder())
240263
}
264+
val = res.Value(ctxMutexKey)
265+
if _, ok := val.(sync.RWMutex); !ok {
266+
res = context.WithValue(res, ctxMutexKey, sync.RWMutex{})
267+
}
241268
val = res.Value(rlKey)
242269
if _, ok := val.(map[string]chat1.RateLimit); !ok {
243270
res = context.WithValue(res, rlKey, make(map[string]chat1.RateLimit))

0 commit comments

Comments
 (0)