Skip to content

Commit

Permalink
Input operator stop must not crash even if start was not called (#36262)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanotti authored Nov 8, 2024
1 parent d0f4d09 commit 9c382d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/stanza/operator/input/windows/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,28 @@ func (i *Input) Start(persister operator.Persister) error {

// Stop will stop reading events from a subscription.
func (i *Input) Stop() error {
i.cancel()
// Warning: all calls made below must be safe to be done even if Start() was not called or failed.

if i.cancel != nil {
i.cancel()
}

i.wg.Wait()

var errs error
if err := i.subscription.Close(); err != nil {
return fmt.Errorf("failed to close subscription: %w", err)
errs = errors.Join(errs, fmt.Errorf("failed to close subscription: %w", err))
}

if err := i.bookmark.Close(); err != nil {
return fmt.Errorf("failed to close bookmark: %w", err)
errs = errors.Join(errs, fmt.Errorf("failed to close bookmark: %w", err))
}

if err := i.publisherCache.evictAll(); err != nil {
return fmt.Errorf("failed to close publishers: %w", err)
errs = errors.Join(errs, fmt.Errorf("failed to close publishers: %w", err))
}

return i.stopRemoteSession()
return errors.Join(errs, i.stopRemoteSession())
}

// readOnInterval will read events with respect to the polling interval until it reaches the end of the channel.
Expand Down
6 changes: 6 additions & 0 deletions pkg/stanza/operator/input/windows/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func newTestInput() *Input {
})
}

// TestInputCreate_Stop ensures the input correctly shuts down even if it was never started.
func TestInputCreate_Stop(t *testing.T) {
input := newTestInput()
assert.NoError(t, input.Stop())
}

// TestInputStart_LocalSubscriptionError ensures the input correctly handles local subscription errors.
func TestInputStart_LocalSubscriptionError(t *testing.T) {
persister := testutil.NewMockPersister("")
Expand Down

0 comments on commit 9c382d9

Please sign in to comment.