Skip to content

Commit 316eeac

Browse files
committed
Defensive code to make sure we don't have multiple kick Chat Handlers
1 parent 9e06288 commit 316eeac

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

pkg/streamcontrol/kick/chat_handler_obsolete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type ChatHandlerOBSOLETE struct {
2828
client ChatClientOBSOLETE
2929
cancelFunc context.CancelFunc
3030
messagesOutChan chan streamcontrol.ChatMessage
31-
onClose func(context.Context)
31+
onClose func(context.Context, *ChatHandlerOBSOLETE)
3232
}
3333

3434
func (k *Kick) newChatHandlerOBSOLETE(
3535
ctx context.Context,
3636
channelSlug string,
37-
onClose func(context.Context),
37+
onClose func(context.Context, *ChatHandlerOBSOLETE),
3838
) (*ChatHandlerOBSOLETE, error) {
3939
reverseEngClient, err := kickcom.New()
4040
if err != nil {
@@ -51,7 +51,7 @@ func NewChatHandlerOBSOLETE(
5151
ctx context.Context,
5252
chatClient ChatClientOBSOLETE,
5353
channelID uint64,
54-
onClose func(context.Context),
54+
onClose func(context.Context, *ChatHandlerOBSOLETE),
5555
) (_ret *ChatHandlerOBSOLETE, _err error) {
5656
logger.Debugf(ctx, "NewChatHandlerOBSOLETE(ctx, client, %d, %p)", channelID, onClose)
5757
defer func() {
@@ -71,7 +71,7 @@ func NewChatHandlerOBSOLETE(
7171
observability.Go(ctx, func(ctx context.Context) {
7272
if onClose != nil {
7373
defer func() {
74-
onClose(ctx)
74+
onClose(ctx, h)
7575
}()
7676
}
7777
defer func() {

pkg/streamcontrol/kick/kick.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,16 @@ func (k *Kick) initChatHandler(
188188

189189
func (k *Kick) onChatHandlerClose(
190190
ctx context.Context,
191+
h *ChatHandlerOBSOLETE,
191192
) {
192193
if !k.ChatHandlerLocker.Lock(ctx) {
193194
return
194195
}
195196
defer k.ChatHandlerLocker.Unlock()
197+
if h != k.ChatHandler {
198+
logger.Errorf(ctx, "chat handler was already replaced")
199+
return
200+
}
196201
select {
197202
case <-ctx.Done():
198203
return

0 commit comments

Comments
 (0)