Skip to content

Commit

Permalink
🧹 make sure all asset runtime are shut down after job (#2387)
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.

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
  • Loading branch information
arlimus authored Oct 26, 2023
1 parent 0e026bb commit d94b212
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,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.
// 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 i := range assetCandidates {
candidate := assetCandidates[i]

Expand Down Expand Up @@ -404,7 +417,7 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
runtime: runtime,
})

// we don't need the runtime anymore, so close it
// runtimes are single-use only. Close them once they are done.
runtime.Close()
}
finished = true
Expand Down

0 comments on commit d94b212

Please sign in to comment.