Skip to content

Commit

Permalink
fix(GODT-2526): Unlimited memory usage during fetch/search
Browse files Browse the repository at this point in the history
This bug was introduced when we switch for a queued channel to handle
the client's responses. This had the unfortunate side effect that if the
responses would arrive faster than they can be sent to client they can
pile up. When this is combined with fetch queries that read the message
literal, an unhealthy amount of RAM can be consumed by this.
  • Loading branch information
LBeernaertProton committed Mar 29, 2023
1 parent 574da2d commit 19b8f7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
15 changes: 2 additions & 13 deletions internal/session/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,14 @@ import (
"github.com/ProtonMail/gluon/internal/response"
"github.com/ProtonMail/gluon/internal/state"
"github.com/ProtonMail/gluon/logging"
"github.com/ProtonMail/gluon/queue"
)

func (s *Session) handleOther(
ctx context.Context,
tag string,
cmd command.Payload,
) <-chan response.Response {
resCh := make(chan response.Response)

outCh := queue.NewQueuedChannel[response.Response](0, 0)

go func() {
defer outCh.Close()

for res := range resCh {
outCh.Enqueue(res)
}
}()
resCh := make(chan response.Response, 8)

s.handleWG.Go(func() {
logging.DoAnnotated(state.NewStateContext(ctx, s.state), func(ctx context.Context) {
Expand All @@ -45,7 +34,7 @@ func (s *Session) handleOther(
})
})

return outCh.GetChannel()
return resCh
}

// handleCommand returns a response instance if a command needs to force an exit of the client.
Expand Down
10 changes: 9 additions & 1 deletion internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,16 @@ func (s *Session) serve(ctx context.Context) error {
}

default:
for res := range s.handleOther(withStartTime(ctx, time.Now()), res.command.Tag, cmd) {
respCh := s.handleOther(withStartTime(ctx, time.Now()), res.command.Tag, cmd)
for res := range respCh {
if err := res.Send(s); err != nil {
go func() {
for range respCh {
// Consume all invalid input on error that is still being produced by the ongoing
// command.
}
}()

return fmt.Errorf("failed to send response to client: %w", err)
}
}
Expand Down

0 comments on commit 19b8f7b

Please sign in to comment.