android systrace close_fds错误

时间:2017-11-16 01:39:05

标签: android python systrace

当我尝试在android中运行systrace时出现以下错误。 “ValueError:如果重定向stdin / stdout / stderr,Windows平台上不支持close_fds”

有没有其他人面对这个问题?堆栈跟踪

CRITICAL:root:(TimeoutThread-1-for-MainThread) Exception on _RunAdbCmd(<class 'devil.android.sdk.adb_wrapper.AdbWrapper'>, ['devices'], retries=2, timeout=30), attempt 1 of 3: ValueError('close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr',)
CRITICAL:root:(TimeoutThread-2-for-MainThread) Exception on _RunAdbCmd(<class 'devil.android.sdk.adb_wrapper.AdbWrapper'>, ['devices'], retries=2, timeout=30), attempt 2 of 3: ValueError('close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr',)
Traceback (most recent call last):
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\systrace.py", line 49, in <module>
    sys.exit(run_systrace.main())
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 194, in main
    main_impl(sys.argv)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 155, in main_impl
    devices = [a.GetDeviceSerial() for a in adb_wrapper.AdbWrapper.Devices()]
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\sdk\adb_wrapper.py", line 363, in Devices
    retries=retries)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\sdk\adb_wrapper.py", line 383, in _RawDevices
    output = cls._RunAdbCmd(cmd, timeout=timeout, retries=retries)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 57, in timeout_retry_wrapper
    retry_if_func=retry_if_func)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 159, in Run
    error_log_func=error_log_func)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 186, in JoinAll
    self._JoinAll(watcher, timeout)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 158, in _JoinAll
    thread.ReraiseIfException()
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 81, in run
    self._ret = self._func(*self._args, **self._kwargs)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 152, in <lambda>
    child_thread = reraiser_thread.ReraiserThread(lambda: func(*args, **kwargs),
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 47, in impl
    return f(*args, **kwargs)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\sdk\adb_wrapper.py", line 249, in _RunAdbCmd
    timeout_retry.CurrentTimeoutThreadGroup().GetRemainingTime())
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\cmd_helper.py", line 313, in GetCmdStatusAndOutputWithTimeout
    stderr=subprocess.STDOUT)
  File "C:\Users\shaq\AppData\Local\Android\android-sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\cmd_helper.py", line 105, in Popen
    shell=shell, close_fds=True, env=env, preexec_fn=preexec_fn)
  File "C:\Python27\lib\subprocess.py", line 347, in __init__
    raise ValueError("close_fds is not supported on Windows "
ValueError: close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr

1 个答案:

答案 0 :(得分:0)

答案很晚。

如错误日志所述,在Windows上,如果要重定向任何stdin / stdout / stderr,则无法使用close_fds

但是,如果你真的需要使用close_fds(例如在我的情况下,以便子进程不从其他文件描述符继承),你可能不会使用close_fds并使用魔法黑客,如

def _hack_windows_subprocess():
    """HACK: python 2.7 file descriptors.
    This magic hack fixes https://bugs.python.org/issue19575
    by adding HANDLE_FLAG_INHERIT to all already opened file descriptors.
    """
    # See https://github.com/secdev/scapy/issues/1136
    import ctypes, msvcrt, stat
    HANDLE_FLAG_INHERIT = 0x00000001
    for fd in range(100):
        try:
            s = os.fstat(fd)
        except:
            break
        if stat.S_ISREG(s.st_mode):
            ctypes.windll.kernel32.SetHandleInformation(
                msvcrt.get_osfhandle(fd),
                HANDLE_FLAG_INHERIT,
                0)

并在子进程调用之前运行_hack_windows_subprocess