ponty / PyVirtualDisplay

Python wrapper for Xvfb, Xephyr and Xvnc
BSD 2-Clause "Simplified" License
709 stars 78 forks source link

Added del method to AbstractDisplay #15

Closed pynasty closed 5 years ago

pynasty commented 8 years ago

When a display object is deleted, the display should first be stopped to avoid any issues with orphaned processes related to the virtual display.

Issue noticed on an Ubuntu server using Xvfb. Issue was not reproducible on an Ubuntu machine with a physical display. Most simple reproduction in python terminal:

  1. Import, start and use a display
  2. Exit the python process with Ctrl-D
  3. View running processes, there may be a dbus instance related to the display that will run indefinitely.
ponty commented 8 years ago

http://stackoverflow.com/questions/6104535/i-dont-understand-this-python-del-behaviour "you cannot assume that del will ever be called - it is not a place to hope that resources are automagically deallocated. If you want to make sure that a (non-memory) resource is released, you should make a release() or similar method and then call that explicitly (or use it in a context manager as pointed out by Thanatos in comments below)."

https://docs.python.org/2/reference/datamodel.html#object.__del__ " It is not guaranteed that del() methods are called for objects that still exist when the interpreter exits."

You should call stop() explicitly or you can use the context manager:

with Display(visible=0, bgcolor='black') as disp:
    your_code()