osquery / osquery-python

Python bindings for osquery's Thrift API
Other
292 stars 51 forks source link

Python 3.5: Resource Warning #60

Closed fralau closed 5 years ago

fralau commented 5 years ago

I am using a SpawnInstance as an object within a class. Unfortunately

self._instance = osquery.SpawnInstance()
...
self._instance.open()
r = self._instance.client.query(strSQL)

Apparently the SpawnInstance object leaves three resources open on exit and this triggers Python 3 warnings:

sys:1: ResourceWarning: unclosed file <_io.BufferedWriter name=7>
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name=8>
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name=10>

I tried to force a call to the __del__ function of the instance, but this does not prevent the warning. Unfortunately this warning is difficult to mask, because the source is somewhere likely in a thread.

I am puzzled because the fowlling code does not trigger the ResourceWarning on exit:

import osquery
instance = osquery.SpawnInstance()
instance.open()  # This may raise an exception
# Issues queries and call osquery Thrift APIs.
r = instance.client.query("select timestamp from time")
print(r)

Any idea what's happening?

theopolis commented 5 years ago

This is a legit issue, most likely caused by not closing mkstemp. I cannot see where that is closed.

theopolis commented 5 years ago

https://github.com/osquery/osquery-python/issues/66 seems related.

theopolis commented 5 years ago

This will open a descriptor and should be closed: https://github.com/osquery/osquery-python/blob/master/osquery/management.py#L74