Skip to content

Commit

Permalink
Refactor goroutine execution with gowrapper and add error handling fo…
Browse files Browse the repository at this point in the history
…r panics
  • Loading branch information
cesarfda committed Dec 11, 2024
1 parent 1764b2f commit 97ae325
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions ee/desktop/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/kolide/launcher/ee/desktop/user/client"
"github.com/kolide/launcher/ee/desktop/user/menu"
"github.com/kolide/launcher/ee/desktop/user/notify"
"github.com/kolide/launcher/ee/gowrapper"
"github.com/kolide/launcher/ee/presencedetection"
"github.com/kolide/launcher/ee/ui/assets"
"github.com/kolide/launcher/pkg/backoff"
Expand Down Expand Up @@ -184,14 +185,19 @@ func New(k types.Knapsack, messenger runnerserver.Messenger, opts ...desktopUser
}

runner.runnerServer = rs
go func() {
gowrapper.Go(context.TODO(), runner.slogger, func() {
if err := runner.runnerServer.Serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
runner.slogger.Log(context.TODO(), slog.LevelError,
"running monitor server",
"err", err,
)
}
}()
}, func(err any) {
runner.slogger.Log(context.TODO(), slog.LevelError,
"exiting after monitor server panic",
"err", err,
)
})

if runtime.GOOS == "darwin" {
runner.osVersion, err = osversion()
Expand Down Expand Up @@ -309,10 +315,15 @@ func (r *DesktopUsersProcessesRunner) DetectPresence(reason string, interval tim
// killDesktopProcesses kills any existing desktop processes
func (r *DesktopUsersProcessesRunner) killDesktopProcesses(ctx context.Context) {
wgDone := make(chan struct{})
go func() {
gowrapper.Go(context.TODO(), r.slogger, func() {
defer close(wgDone)
r.procsWg.Wait()
}()
}, func(err any) {
r.slogger.Log(context.TODO(), slog.LevelError,
"exiting after wait group panic",
"err", err,
)
})

shutdownRequestCount := 0
for uid, proc := range r.uidProcs {
Expand Down Expand Up @@ -785,7 +796,7 @@ func (r *DesktopUsersProcessesRunner) addProcessTrackingRecordForUser(uid string
// The wait group is needed to prevent races.
func (r *DesktopUsersProcessesRunner) waitOnProcessAsync(uid string, proc *os.Process) {
r.procsWg.Add(1)
go func(username string, proc *os.Process) {
gowrapper.Go(context.TODO(), r.slogger, func() {
defer r.procsWg.Done()
// waiting here gives the parent a chance to clean up
state, err := proc.Wait()
Expand All @@ -798,7 +809,14 @@ func (r *DesktopUsersProcessesRunner) waitOnProcessAsync(uid string, proc *os.Pr
"state", state,
)
}
}(uid, proc)
}, func(err any) {
r.slogger.Log(context.TODO(), slog.LevelError,
"exiting after process wait panic",
"err", err,
"uid", uid,
"pid", proc.Pid,
)
})
}

// determineExecutablePath returns DesktopUsersProcessesRunner.executablePath if it is set,
Expand Down

0 comments on commit 97ae325

Please sign in to comment.