feat(GODT-2500): Add panic handlers everywhere.

This commit is contained in:
Jakub
2023-03-22 17:18:17 +01:00
parent 9f59e61b14
commit ec92c918cd
42 changed files with 283 additions and 130 deletions

View File

@ -191,6 +191,12 @@ func NewService(
return s, nil
}
func (s *Service) handlePanic() {
if s.panicHandler != nil {
s.panicHandler.HandlePanic()
}
}
func (s *Service) initAutostart() {
s.firstTimeAutostart.Do(func() {
shouldAutostartBeOn := s.bridge.GetAutostart()
@ -207,11 +213,14 @@ func (s *Service) Loop() error {
if s.parentPID < 0 {
s.log.Info("Not monitoring parent PID")
} else {
go s.monitorParentPID()
go func() {
defer s.handlePanic()
s.monitorParentPID()
}()
}
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
s.watchEvents()
}()
@ -221,6 +230,8 @@ func (s *Service) Loop() error {
defer close(doneCh)
go func() {
defer s.handlePanic()
select {
case <-s.quitCh:
s.log.Info("Stopping gRPC server")
@ -564,6 +575,8 @@ func (s *Service) monitorParentPID() {
s.log.Info("Parent process does not exist anymore. Initiating shutdown")
// quit will write to the parentPIDDoneCh, so we launch a goroutine.
go func() {
defer s.handlePanic()
if err := s.quit(); err != nil {
logrus.WithError(err).Error("Error on quit")
}

View File

@ -114,6 +114,8 @@ func (s *Service) Quit(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empt
func (s *Service) quit() error {
// Windows is notably slow at Quitting. We do it in a goroutine to speed things up a bit.
go func() {
defer s.handlePanic()
if s.parentPID >= 0 {
s.parentPIDDoneCh <- struct{}{}
}
@ -221,7 +223,8 @@ func (s *Service) TriggerReset(ctx context.Context, _ *emptypb.Empty) (*emptypb.
s.log.Debug("TriggerReset")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
s.triggerReset()
}()
return &emptypb.Empty{}, nil
@ -316,6 +319,8 @@ func (s *Service) ReportBug(ctx context.Context, report *ReportBugRequest) (*emp
}).Debug("ReportBug")
go func() {
defer s.handlePanic()
defer func() { _ = s.SendEvent(NewReportBugFinishedEvent()) }()
if err := s.bridge.ReportBug(
@ -343,7 +348,7 @@ func (s *Service) ExportTLSCertificates(_ context.Context, folderPath *wrappersp
s.log.WithField("folderPath", folderPath).Info("ExportTLSCertificates")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
cert, key := s.bridge.GetBridgeTLSCert()
@ -379,7 +384,7 @@ func (s *Service) Login(ctx context.Context, login *LoginRequest) (*emptypb.Empt
s.log.WithField("username", login.Username).Debug("Login")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
password, err := base64Decode(login.Password)
if err != nil {
@ -435,7 +440,7 @@ func (s *Service) Login2FA(ctx context.Context, login *LoginRequest) (*emptypb.E
s.log.WithField("username", login.Username).Debug("Login2FA")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
if s.auth.UID == "" || s.authClient == nil {
s.log.Errorf("Login 2FA: authethication incomplete %s %p", s.auth.UID, s.authClient)
@ -480,7 +485,7 @@ func (s *Service) Login2Passwords(ctx context.Context, login *LoginRequest) (*em
s.log.WithField("username", login.Username).Debug("Login2Passwords")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
password, err := base64Decode(login.Password)
if err != nil {
@ -502,7 +507,7 @@ func (s *Service) LoginAbort(ctx context.Context, loginAbort *LoginAbortRequest)
s.log.WithField("username", loginAbort.Username).Debug("LoginAbort")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
s.loginAbort()
}()
@ -514,7 +519,7 @@ func (s *Service) CheckUpdate(context.Context, *emptypb.Empty) (*emptypb.Empty,
s.log.Debug("CheckUpdate")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
updateCh, done := s.bridge.GetEvents(
events.UpdateAvailable{},
@ -546,7 +551,7 @@ func (s *Service) InstallUpdate(ctx context.Context, _ *emptypb.Empty) (*emptypb
s.log.Debug("InstallUpdate")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
safe.RLock(func() {
s.bridge.InstallUpdate(s.target)
@ -587,6 +592,8 @@ func (s *Service) SetDiskCachePath(ctx context.Context, newPath *wrapperspb.Stri
s.log.WithField("path", newPath.Value).Debug("setDiskCachePath")
go func() {
defer s.handlePanic()
defer func() {
_ = s.SendEvent(NewDiskCachePathChangeFinishedEvent())
}()
@ -652,7 +659,7 @@ func (s *Service) SetMailServerSettings(_ context.Context, settings *ImapSmtpSet
Debug("SetConnectionMode")
go func() {
defer s.panicHandler.HandlePanic()
defer s.handlePanic()
defer func() { _ = s.SendEvent(NewChangeMailServerSettingsFinishedEvent()) }()