Skip to content

Commit

Permalink
🧹 make sure all asset runtime are shut down after job (#903)
Browse files Browse the repository at this point in the history
On distributeJob, we create runtimes but only shut them down at the end of the execution. However, it is possible to exit this function before all runtimes have been executed, i.e. before they all have a chance to shut down properly. With this deferred function we make sure all new runtimes are terminated.

See mondoohq/cnquery#2387

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Oct 26, 2023
1 parent 919f90b commit 8a4bcb6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions policy/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,20 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
// assets = append(assets, runtime.Provider.Connection.Asset)
}

// For each asset candidate, we initialize a new runtime and connect to it.
// Within this process, we set up a catch-all deferred function, that shuts
// down all runtimes, in case we exit early. The list of assets only gets
// set in the block below this deferred function.
defer func() {
for i := range assets {
asset := assets[i]
// we can call close multiple times and it will only execute once
if asset.runtime != nil {
asset.runtime.Close()
}
}
}()

// for each asset candidate, we initialize a new runtime and connect to it.
for i := range assetCandidates {
candidate := assetCandidates[i]
Expand Down

0 comments on commit 8a4bcb6

Please sign in to comment.