diff mbox series

[v2] selftest: tpm2: Add Client.__del__() to close /dev/tpm* handle

Message ID 20220920131518.1984701-1-stefanb@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series [v2] selftest: tpm2: Add Client.__del__() to close /dev/tpm* handle | expand

Commit Message

Stefan Berger Sept. 20, 2022, 1:15 p.m. UTC
The following output can bee seen when the test is executed:

  test_flush_context (tpm2_tests.SpaceTest) ... \
    /usr/lib64/python3.6/unittest/case.py:605: ResourceWarning: \
    unclosed file <_io.FileIO name='/dev/tpmrm0' mode='rb+' closefd=True>

An instance of Client does not implicitly close /dev/tpm* handle, once it
gets destroyed. Close the file handle in the class destructor
Client.__del__().

Fixes: 6ea3dfe1e0732 ("selftests: add TPM 2.0 tests")
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>

---
 tools/testing/selftests/tpm2/tpm2.py | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Jarkko Sakkinen Sept. 21, 2022, 5:59 p.m. UTC | #1
On Tue, Sep 20, 2022 at 09:15:18AM -0400, Stefan Berger wrote:
> The following output can bee seen when the test is executed:
> 
>   test_flush_context (tpm2_tests.SpaceTest) ... \
>     /usr/lib64/python3.6/unittest/case.py:605: ResourceWarning: \
>     unclosed file <_io.FileIO name='/dev/tpmrm0' mode='rb+' closefd=True>
> 
> An instance of Client does not implicitly close /dev/tpm* handle, once it
> gets destroyed. Close the file handle in the class destructor
> Client.__del__().
> 
> Fixes: 6ea3dfe1e0732 ("selftests: add TPM 2.0 tests")
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: linux-kselftest@vger.kernel.org
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> 
> ---
>  tools/testing/selftests/tpm2/tpm2.py | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
> index 057a4f49c79d..c7363c6764fc 100644
> --- a/tools/testing/selftests/tpm2/tpm2.py
> +++ b/tools/testing/selftests/tpm2/tpm2.py
> @@ -371,6 +371,10 @@ class Client:
>              fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
>              self.tpm_poll = select.poll()
>  
> +    def __del__(self):
> +        if self.tpm:
> +            self.tpm.close()
> +
>      def close(self):
>          self.tpm.close()
>  
> -- 
> 2.36.1
> 


Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko
diff mbox series

Patch

diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
index 057a4f49c79d..c7363c6764fc 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -371,6 +371,10 @@  class Client:
             fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
             self.tpm_poll = select.poll()
 
+    def __del__(self):
+        if self.tpm:
+            self.tpm.close()
+
     def close(self):
         self.tpm.close()