Skip to content

Commit

Permalink
clean USED_DISPLAY_NR_LIST in stop() #83
Browse files Browse the repository at this point in the history
  • Loading branch information
ponty committed Mar 12, 2023
1 parent ae67efc commit 923dcc4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pyvirtualdisplay/abstractdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(
self._reset_global_env = False
self._pipe_wfd = None
self._retries_current = 0
self._display_to_rm = None

helptext = get_helptext(program)
self._has_displayfd = "-displayfd" in helptext
Expand Down Expand Up @@ -230,6 +231,7 @@ def _start1(self):
self.new_display_var = ":%s" % int(self.display)

_USED_DISPLAY_NR_LIST.append(self.display)
self._display_to_rm = self.display

self._command = self._cmd() + self._extra_args
log.debug("command: %s", self._command)
Expand Down Expand Up @@ -356,6 +358,11 @@ def stop(self):

if self._use_xauth:
self._clear_xauth()

if self._display_to_rm:
with _mutex:
_USED_DISPLAY_NR_LIST.remove(self._display_to_rm)
self._display_to_rm = None
return self

def _read_stdout_stderr(self):
Expand Down
21 changes: 20 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tutil import has_xvnc, rfbport

from pyvirtualdisplay import Display
from pyvirtualdisplay.abstractdisplay import XStartError
from pyvirtualdisplay.abstractdisplay import _USED_DISPLAY_NR_LIST, XStartError
from pyvirtualdisplay.xephyr import XephyrDisplay
from pyvirtualdisplay.xvfb import XvfbDisplay
from pyvirtualdisplay.xvnc import XvncDisplay
Expand Down Expand Up @@ -229,3 +229,22 @@ def test_display():
assert d.display is None
d.start()
assert d.display >= 0


def test_USED_DISPLAY_NR_LIST():
n = len(_USED_DISPLAY_NR_LIST)
vd = Display()
vd._obj._has_displayfd = False
vd.start()
assert len(_USED_DISPLAY_NR_LIST) == n + 1

vd2 = Display()
vd2._obj._has_displayfd = False
vd2.start()
assert len(_USED_DISPLAY_NR_LIST) == n + 2

vd2.stop()
assert len(_USED_DISPLAY_NR_LIST) == n + 1

vd.stop()
assert len(_USED_DISPLAY_NR_LIST) == n + 0

0 comments on commit 923dcc4

Please sign in to comment.