From 8a4bcb6bd4331fe8f67df82333e7904b2cea3bce Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Thu, 26 Oct 2023 15:48:26 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20make=20sure=20all=20asset=20runt?= =?UTF-8?q?ime=20are=20shut=20down=20after=20job=20(#903)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/mondoohq/cnquery/pull/2387 Signed-off-by: Dominik Richter --- policy/scan/local_scanner.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/policy/scan/local_scanner.go b/policy/scan/local_scanner.go index 346e2bc0..98f91fe5 100644 --- a/policy/scan/local_scanner.go +++ b/policy/scan/local_scanner.go @@ -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]