Skip to contentSkip to conversation

selftests: tpm2: Implement class desstructor to close file descriptor

PATCH In review Stefan Berger 4 participants 4 messages Sep 9, 2022 – Sep 20, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: tpm2: Implement class desstructor to close file descriptor

From: Stefan Berger <[email protected]>

Implement a class destructor to close the open TPM file descriptor and avoid the following error message:

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>

Fixes 6ea3dfe1e0732 ("selftests: add TPM 2.0 tests")
Cc Shuah Khan <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
---
 tools/testing/selftests/tpm2/tpm2.py | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/tpm2/tpm2.py +4 -0
index 057a4f49c79d..c7363c6764fc 100644
@@ -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()
 
Signature · 1 line
2.35.1
Tested-by on v1 1/1
#1 lore raw
Quoted 9 lines · Stefan Berger

From: Stefan Berger <[email protected]>

Implement a class destructor to close the open TPM file descriptor and avoid the following error message:

  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>
Quoted 5 lines
Fixes 6ea3dfe1e0732 ("selftests: add TPM 2.0 tests")
Cc Shuah Khan <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Quoted 20 lines
---
  tools/testing/selftests/tpm2/tpm2.py | 4 ++++
  1 file changed, 4 insertions(+)
tools/testing/selftests/tpm2/tpm2.py +4 -0
index 057a4f49c79d..c7363c6764fc 100644
@@ -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()
 
Signature · 2 lines
Thanks and Regards
R.Nageswara Sastry
maintainer
#2 lore raw
In reply to Stefan Berger

s/desstructor/destructor/

I'd change it to "selftest: tpm2: Add Client.__del__() to close /dev/tpm* handle"

Quoted 8 lines · Stefan Berger

From: Stefan Berger <[email protected]>

Implement a class destructor to close the open TPM file descriptor and avoid the following error message:

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>

I don't recall seeing this. Does this happen on every test case?

Should better describe what is going on, e.g.

"
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__().
"

Quoted 6 lines
Fixes 6ea3dfe1e0732 ("selftests: add TPM 2.0 tests")
Cc Shuah Khan <[email protected]>
Cc Jarkko Sakkinen <[email protected]>

BR, Jarkko

#3 lore raw
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines
  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>

I don't recall seeing this. Does this happen on every test case?

No, only on that one test case.

Stefan