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

Allow runner.Shutdown to be called more than once #1354

Merged
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
7 changes: 7 additions & 0 deletions pkg/osquery/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Runner struct {
instance *OsqueryInstance
instanceLock sync.Mutex
shutdown chan struct{}
interrupted bool
opts []OsqueryInstanceOption
}

Expand Down Expand Up @@ -140,6 +141,12 @@ func (r *Runner) Query(query string) ([]map[string]string, error) {
// Shutdown instructs the runner to permanently stop the running instance (no
// restart will be attempted).
func (r *Runner) Shutdown() error {
if r.interrupted {
// Already shut down, nothing else to do
return nil
}

r.interrupted = true
close(r.shutdown)
r.instanceLock.Lock()
defer r.instanceLock.Unlock()
Expand Down
22 changes: 22 additions & 0 deletions pkg/osquery/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ func TestSimplePath(t *testing.T) {
require.NoError(t, runner.Shutdown())
}

func TestMultipleShutdowns(t *testing.T) {
t.Parallel()
rootDirectory, rmRootDirectory, err := osqueryTempDir()
require.NoError(t, err)
defer rmRootDirectory()

k := typesMocks.NewKnapsack(t)
k.On("OsqueryHealthcheckStartupDelay").Return(0 * time.Second).Maybe()
runner, err := LaunchInstance(
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
require.NoError(t, err)

waitHealthy(t, runner)

for i := 0; i < 3; i += 1 {
require.NoError(t, runner.Shutdown(), "expected no error on calling shutdown but received error on attempt: ", i)
}
}

func TestRestart(t *testing.T) {
t.Parallel()
runner, teardown := setupOsqueryInstanceForTests(t)
Expand Down
Loading