Skip to content

Commit

Permalink
Fix where agent panics on nil event (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaitanyaKulkarni28 authored Aug 2, 2024
1 parent ca3272f commit d592f56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions google_guest_agent/events/sshtrustedca/sshtrustedca_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -78,7 +79,7 @@ func (mp *Watcher) setWaitingWrite(val bool) {

// Run listens to ssh_trusted_ca's pipe open calls and report back the event.
func (mp *Watcher) Run(ctx context.Context, evType string) (bool, interface{}, error) {
var canceled bool
var canceled atomic.Bool

for mp.isWaitingWrite() {
time.Sleep(10 * time.Millisecond)
Expand All @@ -95,7 +96,7 @@ func (mp *Watcher) Run(ctx context.Context, evType string) (bool, interface{}, e
case <-cancelContext:
break
case <-ctx.Done():
canceled = true
canceled.Store(true)

// Open the pipe as O_RDONLY to release the blocking open O_WRONLY.
pipeFile, err := os.OpenFile(mp.pipePath, os.O_RDONLY, 0644)
Expand Down Expand Up @@ -127,7 +128,7 @@ func (mp *Watcher) Run(ctx context.Context, evType string) (bool, interface{}, e

// Have we got a ctx.Done()? if so lets just return from here and unregister
// the watcher.
if canceled {
if canceled.Load() {
if err := pipeFile.Close(); err != nil {
logger.Errorf("Failed to close readonly pipe: %+v", err)
}
Expand Down
7 changes: 6 additions & 1 deletion google_guest_agent/sshca/sshca.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func writeFile(ctx context.Context, evType string, data interface{}, evData *eve
}

// Make sure we close the pipe after we've done writing to it.
pipeData := evData.Data.(*sshtrustedca.PipeData)
pipeData, ok := evData.Data.(*sshtrustedca.PipeData)
if !ok {
logger.Errorf("Received invalid event data (%+v), ignoring this event and un-subscribing %s", evData.Data, evType)
return false
}

defer func() {
if err := pipeData.File.Close(); err != nil {
logger.Errorf("Failed to close pipe: %+v", err)
Expand Down

0 comments on commit d592f56

Please sign in to comment.