Skip to content

Commit

Permalink
🐛 fix provider shutdown race condition (#3775)
Browse files Browse the repository at this point in the history
* 🐛 fix provider shutdown race condition

Signed-off-by: Ivan Milchev <[email protected]>

* fix tests

Signed-off-by: Ivan Milchev <[email protected]>

---------

Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Apr 17, 2024
1 parent 93ffc40 commit a41b5a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
12 changes: 2 additions & 10 deletions providers/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,9 @@ func (c *coordinator) RemoveRuntime(runtime *Runtime) {
}
}

// Analyze the providers that are still being used by active runtimes
usedProviders := map[string]struct{}{}
for _, r := range c.runtimes {
for _, p := range r.providers {
usedProviders[p.Instance.ID] = struct{}{}
}
}

// Shutdown any providers that are not being used anymore
for id, p := range c.runningByID {
if _, ok := usedProviders[id]; !ok {
if len(c.runtimes) == 0 {
for _, p := range c.runningByID {
log.Debug().Msg("shutting down unused provider " + p.Name)
if err := c.stop(p); err != nil {
log.Warn().Err(err).Str("provider", p.Name).Msg("failed to shut down provider")
Expand Down
9 changes: 5 additions & 4 deletions providers/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestRemoveRuntime_StopUnusedProvider(t *testing.T) {

// Setup another provider with another runtime
mockPlugin2 := NewMockProviderPlugin(ctrl)
mockPlugin2.EXPECT().Shutdown(gomock.Any()).Times(1).Return(nil, nil)
p2 := &RunningProvider{
ID: "provider2",
Plugin: mockPlugin2,
Expand Down Expand Up @@ -140,12 +141,12 @@ func TestRemoveRuntime_StopUnusedProvider(t *testing.T) {
},
}

// Remove the first runtime
// Remove all runtimes
c.RemoveRuntime(r1)
c.RemoveRuntime(r2)

// Verify that the first provider is stopped
assert.NotContains(t, c.runningByID, "provider1")
assert.Contains(t, c.runningByID, "provider2")
// Verify that all provider are stopped
assert.Empty(t, c.runningByID)
}

func TestRemoveRuntime_UsedProvider(t *testing.T) {
Expand Down

0 comments on commit a41b5a5

Please sign in to comment.