Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GODT-2500): Recover in deferred function. #334

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions async/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package async

import (
"context"

"github.com/ProtonMail/gluon/logging"
)

func GoAnnotated(ctx context.Context, panicHandler PanicHandler, fn func(context.Context), labelMap ...logging.Labels) {
go func() {
defer HandlePanic(panicHandler)
logging.DoAnnotated(ctx, fn, labelMap...)
}()
}
18 changes: 14 additions & 4 deletions async/panic_handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package async

type PanicHandler interface {
HandlePanic()
HandlePanic(interface{})
}

type NoopPanicHandler struct{}

func (n NoopPanicHandler) HandlePanic() {}
func (n NoopPanicHandler) HandlePanic(r interface{}) {}

func HandlePanic(panicHandler PanicHandler) {
if panicHandler != nil {
panicHandler.HandlePanic()
if panicHandler == nil {
return
}

if _, ok := panicHandler.(NoopPanicHandler); ok {
return
}

if _, ok := panicHandler.(*NoopPanicHandler); ok {
return
}

panicHandler.HandlePanic(recover())
}
48 changes: 48 additions & 0 deletions async/panic_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package async

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

type recoverHandler struct{}

func (h recoverHandler) HandlePanic(r interface{}) {
fmt.Println("recoverHandler", r)
}

func TestPanicHandler(t *testing.T) {
require.NotPanics(t, func() {
defer HandlePanic(recoverHandler{})
panic("there")
})

require.PanicsWithValue(t, "where", func() {
defer HandlePanic(NoopPanicHandler{})
panic("where")
})

require.PanicsWithValue(t, "everywhere", func() {
defer HandlePanic(nil)
panic("everywhere")
})

require.NotPanics(t, func() {
defer HandlePanic(recoverHandler{})
panic(nil)
})

require.NotPanics(t, func() {
defer HandlePanic(recoverHandler{})
})

require.NotPanics(t, func() {
defer HandlePanic(NoopPanicHandler{})
})

require.NotPanics(t, func() {
defer HandlePanic(&NoopPanicHandler{})
})
}
4 changes: 1 addition & 3 deletions async/queued_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package async
import (
"context"
"sync"

"github.com/ProtonMail/gluon/logging"
)

// QueuedChannel represents a channel on which queued items can be published without having to worry if the reader
Expand All @@ -30,7 +28,7 @@ func NewQueuedChannel[T any](chanBufferSize, queueCapacity int, panicHandler Pan
queue.closed.store(false)

// Start the queue consumer.
logging.GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
defer close(queue.ch)

for {
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/update_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newUpdateInjector(connector connector.Connector, userID string, panicHandle

injector.forwardWG.Add(1)

logging.GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
async.GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
injector.forward(ctx, connector.GetUpdates())
}, logging.Labels{
"Action": "Forwarding updates",
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func newUser(
user.updateWG.Add(1)

// nolint:contextcheck
logging.GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
async.GoAnnotated(context.Background(), panicHandler, func(ctx context.Context) {
defer user.updateWG.Done()

updateCh := user.updateInjector.GetUpdates()
Expand Down
3 changes: 2 additions & 1 deletion internal/session/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"

"github.com/ProtonMail/gluon/async"
"github.com/ProtonMail/gluon/imap/command"
"github.com/ProtonMail/gluon/internal/response"
"github.com/ProtonMail/gluon/logging"
Expand All @@ -21,7 +22,7 @@ type commandResult struct {
func (s *Session) startCommandReader(ctx context.Context) <-chan commandResult {
cmdCh := make(chan commandResult)

logging.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
async.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
defer close(cmdCh)

tlsHeaders := [][]byte{
Expand Down
3 changes: 2 additions & 1 deletion internal/session/handle_idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/ProtonMail/gluon/async"
"github.com/ProtonMail/gluon/imap/command"
"github.com/ProtonMail/gluon/internal/response"
"github.com/ProtonMail/gluon/logging"
Expand All @@ -22,7 +23,7 @@ func (s *Session) handleIdle(ctx context.Context, tag string, _ *command.Idle, c
}

return s.state.Idle(ctx, func(pending []response.Response, resCh chan response.Response) error {
logging.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
async.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
if s.idleBulkTime != 0 {
sendResponsesInBulks(s, resCh, s.idleBulkTime)
} else {
Expand Down
18 changes: 0 additions & 18 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ const (
LineKey = "line"
)

type PanicHandler interface {
HandlePanic()
}

func GoAnnotated(ctx context.Context, panicHandler PanicHandler, fn func(context.Context), labelMap ...Labels) {
pprofDo(ctx, toLabelSet(labelMap...), func(ctx context.Context) {
go func() {
defer func() {
if panicHandler != nil {
panicHandler.HandlePanic()
}
}()

fn(ctx)
}()
})
}

func DoAnnotated(ctx context.Context, fn func(context.Context), labelMap ...Labels) {
pprofDo(ctx, toLabelSet(labelMap...), fn)
}
Expand Down
8 changes: 2 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (s *Server) getNextID() int {
func (s *Server) newEventCh(ctx context.Context) chan events.Event {
eventCh := make(chan events.Event)

logging.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
async.GoAnnotated(ctx, s.panicHandler, func(ctx context.Context) {
for event := range eventCh {
s.publish(event)
}
Expand Down Expand Up @@ -364,11 +364,7 @@ func newConnCh(l net.Listener, panicHandler async.PanicHandler) <-chan net.Conn
connCh := make(chan net.Conn)

go func() {
defer func() {
if panicHandler != nil {
panicHandler.HandlePanic()
}
}()
defer async.HandlePanic(panicHandler)

defer close(connCh)

Expand Down
4 changes: 2 additions & 2 deletions tests/full_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestReceptionOnIdle(t *testing.T) {
wg.Add(2)

// idling.
logging.GoAnnotated(context.Background(), async.NoopPanicHandler{}, func(ctx context.Context) {
async.GoAnnotated(context.Background(), async.NoopPanicHandler{}, func(ctx context.Context) {
defer wg.Done()
done <- c.Idle(stop, nil)
}, logging.Labels{
Expand All @@ -121,7 +121,7 @@ func TestReceptionOnIdle(t *testing.T) {
})

// receiving messages from another client.
logging.GoAnnotated(context.Background(), async.NoopPanicHandler{}, func(ctx context.Context) {
async.GoAnnotated(context.Background(), async.NoopPanicHandler{}, func(ctx context.Context) {
defer wg.Done()

cli := sess.newClient()
Expand Down