[bugfix] fix possible mutex lockup during streaming code (#2633)

* rewrite Stream{} to use much less mutex locking, update related code

* use new context for the stream context

* ensure stream gets closed on return of writeTo / readFrom WSConn()

* ensure stream write timeout gets cancelled

* remove embedded context type from Stream{}, reformat log messages for consistency

* use c.Request.Context() for context passed into Stream().Open()

* only return 1 boolean, fix tests to expect multiple stream types in messages

* changes to ping logic

* further improved ping logic

* don't export unused function types, update message sending to only include relevant stream type

* ensure stream gets closed 🤦

* update to error log on failed json marshal (instead of panic)

* inverse websocket read error checking to _ignore_ expected close errors
This commit is contained in:
kim 2024-02-20 18:07:49 +00:00 committed by GitHub
commit 291e180990
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 535 additions and 451 deletions

View file

@ -116,23 +116,20 @@ func (suite *FromClientAPITestSuite) checkStreamed(
expectPayload string,
expectEventType string,
) {
var msg *stream.Message
streamLoop:
for {
select {
case msg = <-str.Messages:
break streamLoop // Got it.
case <-time.After(5 * time.Second):
break streamLoop // Didn't get it.
}
// Set a 5s timeout on context.
ctx := context.Background()
ctx, cncl := context.WithTimeout(ctx, time.Second*5)
defer cncl()
msg, ok := str.Recv(ctx)
if expectMessage && !ok {
suite.FailNow("expected a message but message was not received")
}
if expectMessage && msg == nil {
suite.FailNow("expected a message but message was nil")
}
if !expectMessage && msg != nil {
suite.FailNow("expected no message but message was not nil")
if !expectMessage && ok {
suite.FailNow("expected no message but message was received")
}
if expectPayload != "" && msg.Payload != expectPayload {

View file

@ -130,14 +130,9 @@ func (suite *FromFediAPITestSuite) TestProcessReplyMention() {
suite.Equal(replyingStatus.ID, notif.StatusID)
suite.False(*notif.Read)
// the notification should be streamed
var msg *stream.Message
select {
case msg = <-wssStream.Messages:
// fine
case <-time.After(5 * time.Second):
suite.FailNow("no message from wssStream")
}
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
msg, ok := wssStream.Recv(ctx)
suite.True(ok)
suite.Equal(stream.EventTypeNotification, msg.Event)
suite.NotEmpty(msg.Payload)
@ -203,14 +198,10 @@ func (suite *FromFediAPITestSuite) TestProcessFave() {
suite.Equal(fave.StatusID, notif.StatusID)
suite.False(*notif.Read)
// 2. a notification should be streamed
var msg *stream.Message
select {
case msg = <-wssStream.Messages:
// fine
case <-time.After(5 * time.Second):
suite.FailNow("no message from wssStream")
}
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
msg, ok := wssStream.Recv(ctx)
suite.True(ok)
suite.Equal(stream.EventTypeNotification, msg.Event)
suite.NotEmpty(msg.Payload)
suite.EqualValues([]string{stream.TimelineNotifications}, msg.Stream)
@ -277,7 +268,9 @@ func (suite *FromFediAPITestSuite) TestProcessFaveWithDifferentReceivingAccount(
suite.False(*notif.Read)
// 2. no notification should be streamed to the account that received the fave message, because they weren't the target
suite.Empty(wssStream.Messages)
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
_, ok := wssStream.Recv(ctx)
suite.False(ok)
}
func (suite *FromFediAPITestSuite) TestProcessAccountDelete() {
@ -405,14 +398,10 @@ func (suite *FromFediAPITestSuite) TestProcessFollowRequestLocked() {
})
suite.NoError(err)
// a notification should be streamed
var msg *stream.Message
select {
case msg = <-wssStream.Messages:
// fine
case <-time.After(5 * time.Second):
suite.FailNow("no message from wssStream")
}
ctx, _ = context.WithTimeout(ctx, time.Second*5)
msg, ok := wssStream.Recv(context.Background())
suite.True(ok)
suite.Equal(stream.EventTypeNotification, msg.Event)
suite.NotEmpty(msg.Payload)
suite.EqualValues([]string{stream.TimelineHome}, msg.Stream)
@ -423,7 +412,7 @@ func (suite *FromFediAPITestSuite) TestProcessFollowRequestLocked() {
suite.Equal(originAccount.ID, notif.Account.ID)
// no messages should have been sent out, since we didn't need to federate an accept
suite.Empty(suite.httpClient.SentMessages)
suite.Empty(&suite.httpClient.SentMessages)
}
func (suite *FromFediAPITestSuite) TestProcessFollowRequestUnlocked() {
@ -503,14 +492,10 @@ func (suite *FromFediAPITestSuite) TestProcessFollowRequestUnlocked() {
suite.Equal(originAccount.URI, accept.To)
suite.Equal("Accept", accept.Type)
// a notification should be streamed
var msg *stream.Message
select {
case msg = <-wssStream.Messages:
// fine
case <-time.After(5 * time.Second):
suite.FailNow("no message from wssStream")
}
ctx, _ = context.WithTimeout(ctx, time.Second*5)
msg, ok := wssStream.Recv(context.Background())
suite.True(ok)
suite.Equal(stream.EventTypeNotification, msg.Event)
suite.NotEmpty(msg.Payload)
suite.EqualValues([]string{stream.TimelineHome}, msg.Stream)

View file

@ -394,10 +394,7 @@ func (s *surface) notify(
if err != nil {
return gtserror.Newf("error converting notification to api representation: %w", err)
}
if err := s.stream.Notify(apiNotif, targetAccount); err != nil {
return gtserror.Newf("error streaming notification to account: %w", err)
}
s.stream.Notify(ctx, targetAccount, apiNotif)
return nil
}

View file

@ -348,11 +348,7 @@ func (s *surface) timelineStatus(
err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err)
return true, err
}
if err := s.stream.Update(apiStatus, account, []string{streamType}); err != nil {
err = gtserror.Newf("error streaming update for status %s: %w", status.ID, err)
return true, err
}
s.stream.Update(ctx, account, apiStatus, streamType)
return true, nil
}
@ -363,12 +359,11 @@ func (s *surface) deleteStatusFromTimelines(ctx context.Context, statusID string
if err := s.state.Timelines.Home.WipeItemFromAllTimelines(ctx, statusID); err != nil {
return err
}
if err := s.state.Timelines.List.WipeItemFromAllTimelines(ctx, statusID); err != nil {
return err
}
return s.stream.Delete(statusID)
s.stream.Delete(ctx, statusID)
return nil
}
// invalidateStatusFromTimelines does cache invalidation on the given status by
@ -555,11 +550,6 @@ func (s *surface) timelineStreamStatusUpdate(
err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err)
return err
}
if err := s.stream.StatusUpdate(apiStatus, account, []string{streamType}); err != nil {
err = gtserror.Newf("error streaming update for status %s: %w", status.ID, err)
return err
}
s.stream.StatusUpdate(ctx, account, apiStatus, streamType)
return nil
}