From 16b046c2a2e6959befc420e9f4fcd78b7157942b Mon Sep 17 00:00:00 2001 From: Dimitri Arrigoni Date: Mon, 21 Nov 2022 22:26:34 +0100 Subject: [PATCH] Fix warning related to finalizer in docker backend --- lib/specinfra/backend/docker.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/specinfra/backend/docker.rb b/lib/specinfra/backend/docker.rb index f85d21a7e..067eec93c 100644 --- a/lib/specinfra/backend/docker.rb +++ b/lib/specinfra/backend/docker.rb @@ -17,7 +17,7 @@ def initialize(config = {}) @base_image = get_or_pull_image(image) create_and_start_container - ObjectSpace.define_finalizer(self, proc { cleanup_container }) + ObjectSpace.define_finalizer(self, self.class.__send__(:finalizer_for, @container)) elsif container = get_config(:docker_container) @container = ::Docker::Container.get(container) else @@ -25,6 +25,25 @@ def initialize(config = {}) end end + class << self + protected + + # Get a finalizer for given container. + # + # @param [::Docker::Container, nil] container + # + # @return [Proc] + def finalizer_for(container) + proc do + # noinspection RubyNilAnalysis + unless container.nil? + container.stop + container.delete + end + end + end + end + def run_command(cmd, opts={}) cmd = build_command(cmd) run_pre_command(opts) @@ -77,8 +96,7 @@ def create_and_start_container end def cleanup_container - @container.stop - @container.delete + self.class.__send__(:finalizer_for, @container).call end def current_image