Pytest:Windows 致命异常:访问冲突

Posted

技术标签:

【中文标题】Pytest:Windows 致命异常:访问冲突【英文标题】:Pytest: Windows fatal exception: access violation 【发布时间】:2020-04-23 07:29:38 【问题描述】:

我正在使用 pytest 为我的项目运行一些测试。有时(大约 30% 到 50%)我在测试完成后会出错。但这会阻止测试引擎创建测试报告,这真的很痛苦。

错误:

Windows fatal exception: access violation

Current thread 0x000019e0 (most recent call first):
  File "C:\Python38\lib\threading.py", line 1200 in invoke_excepthook
  File "C:\Python38\lib\threading.py", line 934 in _bootstrap_inner
  File "C:\Python38\lib\threading.py", line 890 in _bootstrap

Thread 0x00001b0c (most recent call first):
  File "C:\Python38\lib\site-packages\serial\serialwin32.py", line 240 in _close
  File "C:\Python38\lib\site-packages\serial\serialwin32.py", line 246 in close
  File "C:\NoBackup\svn\test_system_smets2\pyets\plugins\plugin_zigbee_dongle.py", line 141 in disconnect
  File "C:\NoBackup\svn\test_system_smets2\pyets\plugins\plugin_zigbee_dongle.py", line 232 in stop
  File "C:\NoBackup\svn\test_system_smets2\pyets\testengine\plugin_manager.py", line 286 in stop
  File "C:\NoBackup\svn\test_system_smets2\pyets\testengine\testengine.py", line 112 in session_finalize
  File "C:\NoBackup\svn\test_system_smets2\pyets\testengine\pytest_test_engine.py", line 58 in test_engine
  File "C:\Python38\lib\site-packages\_pytest\fixtures.py", line 800 in _teardown_yield_fixture
  File "C:\Python38\lib\site-packages\_pytest\fixtures.py", line 871 in finish
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 318 in _callfinalizers
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 328 in _teardown_with_finalization
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 310 in _pop_and_teardown
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 350 in _teardown_towards
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 342 in teardown_exact
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 148 in pytest_runtest_teardown
  File "C:\Python38\lib\site-packages\pluggy\callers.py", line 187 in _multicall
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 83 in <lambda>
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 92 in _hookexec
  File "C:\Python38\lib\site-packages\pluggy\hooks.py", line 286 in __call__
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 217 in <lambda>
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 244 in from_call
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 216 in call_runtest_hook
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 186 in call_and_report
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 101 in runtestprotocol
  File "C:\Python38\lib\site-packages\_pytest\runner.py", line 85 in pytest_runtest_protocol
  File "C:\Python38\lib\site-packages\pluggy\callers.py", line 187 in _multicall
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 83 in <lambda>
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 92 in _hookexec
  File "C:\Python38\lib\site-packages\pluggy\hooks.py", line 286 in __call__
  File "C:\Python38\lib\site-packages\_pytest\main.py", line 272 in pytest_runtestloop
  File "C:\Python38\lib\site-packages\pluggy\callers.py", line 187 in _multicall
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 83 in <lambda>
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 92 in _hookexec
  File "C:\Python38\lib\site-packages\pluggy\hooks.py", line 286 in __call__
  File "C:\Python38\lib\site-packages\_pytest\main.py", line 247 in _main
  File "C:\Python38\lib\site-packages\_pytest\main.py", line 191 in wrap_session
  File "C:\Python38\lib\site-packages\_pytest\main.py", line 240 in pytest_cmdline_main
  File "C:\Python38\lib\site-packages\pluggy\callers.py", line 187 in _multicall
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 83 in <lambda>
  File "C:\Python38\lib\site-packages\pluggy\manager.py", line 92 in _hookexec
  File "C:\Python38\lib\site-packages\pluggy\hooks.py", line 286 in __call__
  File "C:\Python38\lib\site-packages\_pytest\config\__init__.py", line 124 in main
  File "testrun.py", line 1184 in run_single_test
  File "testrun.py", line 1548 in main
  File "testrun.py", line 1581 in <module>

有人知道如何修复或调试它吗?

我在 win10 上使用 pytest 5.4.1 和 python 3.8.0。但这也可以在较旧的 pytest 版本中重现。

插件 plugin_zigbee_dongle.py 使用 pyserial (3.4) 与线程中的 usb-rf-dongle 通信。以下代码是这个插件的sn-p:

import serial
import threading

class ZigbeeDongleSerial(object):

    def __init__(self, test_engine):
        self.ser = serial.Serial()
        self.test_engine = test_engine

    # ----------------------------------------------------------
    def connect(self,
                port,
                baud,
                timeout):

        self.ser.baudrate = baud
        self.ser.timeout = timeout
        self.ser.port = port
        self.ser.open()
    
    # ----------------------------------------------------------
    def disconnect(self):

        try:
            if self.is_connected():
                self.ser.close() # <------- This is line 141 ----------
        except:
            pass

# ----------------------------------------------------------
# ----------------------------------------------------------
class PluginZigbeeDongle(PluginBase):

    # ----------------------------------------------------------
    def __init__(self, test_engine):
        super(PluginZigbeeDongle, self).__init__()

        self.test_engine        = test_engine
        
        self.dongle_serial = ZigbeeDongleSerial(self.test_engine)

        self._startup_lock = threading.Lock()
        self._startup_lock.acquire()

        self.reader_thread = threading.Thread(target=self._read_worker, name="ZigbeeDongleThread")

    # ----------------------------------------------------------
    def start(self):
        super(PluginZigbeeDongle, self).start()

        # connect the serial port
        self.dongle_serial.connect()

        # start the reader thread
        if self.dongle_serial.is_connected():
            self.reader_thread.start()

    # ----------------------------------------------------------
    def stop(self):
        super(PluginZigbeeDongle, self).stop()

        # disconnect the serial port
        if self.dongle_serial.is_connected():
            self.dongle_serial.disconnect() # <------- This is line 232 ----------

        # stop the reader thread
        if self.reader_thread.is_alive():
            self.reader_thread.join()

    # ----------------------------------------------------------
    def _read_worker(self):

        # start-up is now complete
        self._startup_lock.release()

        # handle the incoming character stream
        done = False
        while not done:

            try:
                c = self.dongle_serial.read_byte()

            except serial.SerialException:
                done = True

            except AttributeError:
                done = True

            if not done:
                self._read_parser(c)

【问题讨论】:

plugin_zigbee_dongle.py 中出现错误,它看起来像您项目中的模块。如果没有看到那里发生了什么,就不可能说出问题所在。你能提供一个minimal reproducible example吗? 在使用带有 cython 的夹具和底层 c 库时,我也面临访问冲突问题 @hoefling:谢谢你的评论,我加了一个代码sn-p 我在没有安装 pyserial 的情况下得到了这个。 OTOH,我已经安装了 pytest-qt,当我使用 qtbot 夹具执行操作时,似乎会出现这种错误,但只是偶尔出现:目前我不知道是哪种编码导致它。它似乎与使用 unittest.mock.patch 模拟 PyQt5 对象上的方法有关。但是其他测试处理得很好,所以现在我很困惑。 【参考方案1】:

将 pyserial 从 3.4 版降级到 2.7 版解决了这个问题

【讨论】:

以上是关于Pytest:Windows 致命异常:访问冲突的主要内容,如果未能解决你的问题,请参考以下文章

javafx 致命错误异常访问冲突(问题框架:jfxmedia.dll)

致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE [42000]:语法错误或访问冲突 PHP 和 PDO

异常访问冲突Java?

带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突:

WindowsError:异常:使用从 C++ 到 Python 的 ctypes 创建 DLL 时出现访问冲突或 Windows 错误 193

致命错误:未捕获的 PDOException:SQLSTATE[42000] 语法错误或访问冲突