appium / python-client

Python language bindings for Appium
Apache License 2.0
1.62k stars 554 forks source link

ResourceWarning: unclosed file <_io.BufferedReader name #962

Closed WrrngnCode closed 3 months ago

WrrngnCode commented 3 months ago

ResourceWarning: unclosed file <_io.BufferedReader name error message when trying to destroy AppiumService via stop method:

/usr/lib/python3.8/subprocess.py:946: ResourceWarning: subprocess 16298 is still running
  _warn("subprocess %s is still running" % self.pid,
Object allocated at (most recent call last):
  File "...../.venv/lib/python3.8/site-packages/appium/webdriver/appium_service.py", lineno 196
    self._process = sp.Popen(args=args, stdout=stdout, stderr=stderr, env=env)
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name=7>
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name=9>

It seems to me that the stderr and stout file handles are not closed properly. According to subprocess.Popen documentation process.communicate shall be callled in order to close the stdout and stderr file handles.

Environment

Appium Version: 2.5.1 Appium-Python-Client 3.1.0 selenium 4.15.2 Python3.8.10 on Ubuntu 20.04 node.js 20.11.1

Code To reproduce issue

https://github.com/appium/python-client/blob/92583ce39003b740bb5e57bbf2114d283d884d22/appium/webdriver/appium_service.py#L237 After self._process.terminate a call to Popen.communicate()](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate) shall be made, which will close the file handles.

self._process.terminate()
self._process.communicate()
mykola-mokhnach commented 3 months ago

Thanks for the report @WrrngnCode Feel free to open a PR to address this issue

WrrngnCode commented 3 months ago

PR created: https://github.com/appium/python-client/pull/965