Skip to content

Commit

Permalink
fix python cleanup issues
Browse files Browse the repository at this point in the history
  • Loading branch information
刘欣 committed Feb 6, 2018
1 parent abf8848 commit c24bfad
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 27 deletions.
31 changes: 18 additions & 13 deletions airtest/core/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self, serialno=None, adb_path=None, server_addr=None):
self._display_info_lock = threading.Lock()
self._forward_local_using = []
self.__class__._instances.append(self)
# reg_cleanup(self._cleanup_forwards)

@staticmethod
def builtin_adb_path():
Expand Down Expand Up @@ -85,6 +84,16 @@ def start_server(self):
"""
return self.cmd("start-server", device=False)

def kill_server(self):
"""
Perform `adb kill-server` command to kill the adb server
Returns:
None
"""
return self.cmd("kill-server", device=False)

def version(self):
"""
Perform `adb version` command and return the command output
Expand Down Expand Up @@ -690,8 +699,10 @@ def _cleanup_forwards(self):
Returns:
None
"""
for local in self._forward_local_using[:]:
self.remove_forward(local)
for local in self._forward_local_using:
self.start_cmd(["forward", "--remove", local])

self._forward_local_using = []

@property
def line_breaker(self):
Expand All @@ -703,16 +714,10 @@ def line_breaker(self):
"""
if not self._line_breaker:
if platform.system() == "Windows":
if self.sdk_version >= SDK_VERISON_NEW:
line_breaker = b"\r\n"
else:
line_breaker = b"\r\r\n"
if self.sdk_version >= SDK_VERISON_NEW:
line_breaker = os.linesep
else:
if self.sdk_version >= SDK_VERISON_NEW:
line_breaker = b"\n"
else:
line_breaker = b"\r\n"
line_breaker = b'\r' + os.linesep
self._line_breaker = line_breaker
return self._line_breaker

Expand Down Expand Up @@ -1235,4 +1240,4 @@ def cleanup_adb_forward():
adb._cleanup_forwards()


reg_cleanup(cleanup_adb_forward)
reg_cleanup(cleanup_adb_forward)
3 changes: 1 addition & 2 deletions airtest/core/android/javacap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from airtest.utils.logger import get_logger
from airtest.utils.safesocket import SafeSocket
from airtest.utils.nbsp import NonBlockingStreamReader
from airtest.utils.snippet import reg_cleanup, on_method_ready
from airtest.utils.snippet import on_method_ready
from airtest.core.android.yosemite import Yosemite
import struct
LOGGING = get_logger(__name__)
Expand All @@ -20,7 +20,6 @@ class Javacap(Yosemite):
def __init__(self, adb):
super(Javacap, self).__init__(adb)
self.frame_gen = None
reg_cleanup(self.teardown_stream)

@on_method_ready('install_or_upgrade')
def _setup_stream_server(self):
Expand Down
2 changes: 1 addition & 1 deletion airtest/core/android/minicap.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(self, adb, projection=None):
self.frame_gen = None
self.stream_lock = threading.Lock()
self.quirk_flag = 0
reg_cleanup(self.teardown_stream)

@ready_method
def install_or_upgrade(self):
Expand Down Expand Up @@ -260,6 +259,7 @@ def _setup_stream_server(self, lazy=False):
# minicap server setup error, may be already setup by others
# subprocess exit immediately
raise RuntimeError("minicap server quit immediately")
reg_cleanup(proc.kill)
return proc, nbsp, localport

def get_frame_from_stream(self):
Expand Down
4 changes: 2 additions & 2 deletions airtest/core/android/minitouch.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def setup_server(self):
# server setup error, may be already setup by others
# subprocess exit immediately
raise RuntimeError("minitouch server quit immediately")
reg_cleanup(p.kill)
self.server_proc = p
# reg_cleanup(self.server_proc.kill)
return p

@on_method_ready('install_and_setup')
Expand Down Expand Up @@ -378,7 +378,7 @@ def setup_client_backend(self):
self.backend_stop_event = threading.Event()
self.setup_client()
t = threading.Thread(target=self._backend_worker, name="minitouch")
t.daemon = True
# t.daemon = True
t.start()
self.backend_thread = t
self.handle = self.backend_queue.put
Expand Down
11 changes: 9 additions & 2 deletions airtest/core/android/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, adb):
self.ow_proc = None
self.ow_callback = []
self._t = None
reg_cleanup(self.teardown)

@on_method_ready('start')
def get_ready(self):
Expand All @@ -43,7 +44,11 @@ def _install_and_setup(self):
if p.poll() is not None:
raise RuntimeError("orientationWatcher setup error")
self.ow_proc = p
reg_cleanup(self.ow_proc.kill)
# reg_cleanup(self.ow_proc.kill)

def teardown(self):
if self.ow_proc:
self.ow_proc.kill()

def start(self):
"""
Expand All @@ -60,6 +65,8 @@ def _refresh_by_ow():
if line == b"":
if LOGGING is not None: # may be None atexit
LOGGING.error("orientationWatcher has ended")
else:
print("orientationWatcher has ended")
return None

ori = int(line) / 90
Expand All @@ -81,7 +88,7 @@ def _run():
traceback.print_exc()

self._t = threading.Thread(target=_run, name="rotationwatcher")
self._t.daemon = True
# self._t.daemon = True
self._t.start()

def reg_callback(self, ow_callback):
Expand Down
3 changes: 0 additions & 3 deletions airtest/core/ios/minicap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from airtest.utils.logger import get_logger
from airtest.utils.nbsp import NonBlockingStreamReader
from airtest.utils.safesocket import SafeSocket
from airtest.utils.snippet import reg_cleanup

LOGGING = get_logger(__name__)

Expand All @@ -28,9 +27,7 @@ def __init__(self, udid=None, port=12345):

def setup(self):
cmd = [self.executable, "--udid", self.udid, "--port", str(self.port), "--resolution", self.resolution]
print(cmd)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
reg_cleanup(proc.kill)
nbsp = NonBlockingStreamReader(proc.stdout, print_output=True, name="minicap_sever")
while True:
line = nbsp.readline(timeout=10.0)
Expand Down
2 changes: 1 addition & 1 deletion airtest/core/win/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import win32api
import win32ui
import win32con
from airtest.aircv.utils import Image, pil_2_cv2
from aircv.utils import Image, pil_2_cv2


SM_XVIRTUALSCREEN = 76
Expand Down
2 changes: 1 addition & 1 deletion airtest/utils/nbsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _populateQueue(stream, queue, kill_event):
elif raise_EOF:
raise UnexpectedEndOfStream
else:
LOGGING.debug("EndOfStream")
print("EndOfStream: %s" % self.name)
break

self._kill_event = Event()
Expand Down
38 changes: 36 additions & 2 deletions airtest/utils/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import atexit
import sys
from functools import wraps
from .compat import str_class
from .compat import str_class, queue


def split_cmd(cmds):
Expand Down Expand Up @@ -38,6 +38,10 @@ def get_std_encoding(stream):
return getattr(stream, "encoding", None) or sys.getfilesystemencoding()



CLEANUP_CALLS = queue.Queue()


def reg_cleanup(func, *args, **kwargs):
"""
Clean the register for given function
Expand All @@ -51,7 +55,37 @@ def reg_cleanup(func, *args, **kwargs):
None
"""
atexit.register(func, *args, **kwargs)
CLEANUP_CALLS.put((func, args, kwargs))
# atexit.register(func, *args, **kwargs)


def _cleanup():
# cleanup together to prevent atexit thread issue
while not CLEANUP_CALLS.empty():
(func, args, kwargs) = CLEANUP_CALLS.get()
func(*args, **kwargs)


# atexit.register(_cleanup)

import threading


_shutdown = threading._shutdown


def exitfunc():
print("exiting.......")
_cleanup()
_shutdown()


# use threading._shutdown to exec cleanup when main thread exit
# atexit exec after all thread exit, which needs to cooperate with daemon thread.
# daemon thread is evil, which abruptly exit causing unexpected error
threading._shutdown = exitfunc




def on_method_ready(method_name):
Expand Down

0 comments on commit c24bfad

Please sign in to comment.