pytest-dev / pytest-dev/pytest

8.3.4 changed atexit behavior

Open
#13,021 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

So this is rather bizarre and was also wild to debug/bisect and I don't have an MRE, but maybe someone has an idea.

In a nutshell, due to 40741c4aca50582cc9701ff01504b9e6dcd3396f / https://github.com/pytest-dev/pytest/pull/12867), pytest crashes with a SEGFAULT after the tests.

Of course, it's not pytest that's crashing but the bane of my existence: https://github.com/sqlanywhere/sqlanydb

When running on commit 3d3ec5724c6f76bc07d0631ec8061f26f9ecac4c, I get an output like this after my tests already passed:

Exception during reset or similar
Traceback (most recent call last):
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 986, in _finalize_fairy
    fairy._reset(
    ~~~~~~~~~~~~^
        pool,
        ^^^^^
    ...<2 lines>...
        asyncio_safe=can_manipulate_connection,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 1432, in _reset
    pool._dialect.do_rollback(self)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 699, in do_rollback
    dbapi_connection.rollback()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 657, in rollback
    return self.api.sqlany_rollback(self.con())
                                    ~~~~~~~~^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 648, in con
    self.handleerror(InterfaceError, "not connected", -101)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 644, in handleerror
    eh(self, None, errorclass, errorvalue, sqlcode)
    ~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 383, in standardErrorHandler
    raise errorclass(errorvalue,sqlcode)
sqlanydb.InterfaceError: ('not connected', -101)
Exception terminating connection <sqlanydb.Connection object at 0x10e423d40>
Traceback (most recent call last):
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 986, in _finalize_fairy
    fairy._reset(
    ~~~~~~~~~~~~^
        pool,
        ^^^^^
    ...<2 lines>...
        asyncio_safe=can_manipulate_connection,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 1432, in _reset
    pool._dialect.do_rollback(self)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 699, in do_rollback
    dbapi_connection.rollback()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 657, in rollback
    return self.api.sqlany_rollback(self.con())
                                    ~~~~~~~~^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 648, in con
    self.handleerror(InterfaceError, "not connected", -101)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 644, in handleerror
    eh(self, None, errorclass, errorvalue, sqlcode)
    ~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 383, in standardErrorHandler
    raise errorclass(errorvalue,sqlcode)
sqlanydb.InterfaceError: ('not connected', -101)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 374, in _close_connection
    self._dialect.do_terminate(connection)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 705, in do_terminate
    self.do_close(dbapi_connection)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 708, in do_close
    dbapi_connection.close()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 688, in close
    c = self.con()
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 648, in con
    self.handleerror(InterfaceError, "not connected", -101)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 644, in handleerror
    eh(self, None, errorclass, errorvalue, sqlcode)
    ~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 383, in standardErrorHandler
    raise errorclass(errorvalue,sqlcode)
sqlanydb.InterfaceError: ('not connected', -101)

When running on 40741c4aca50582cc9701ff01504b9e6dcd3396f or later (and -q -X faulthandler, I get:

Fatal Python error: Segmentation fault

Current thread 0x0000000200a9e200 (most recent call first):
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlanydb.py", line 657 in rollback
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 699 in do_rollback
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 1432 in _reset
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 986 in _finalize_fairy
  File "/Users/hynek/Work/customer-api/.venv/lib/python3.13/site-packages/sqlalchemy/pool/base.py", line 729 in <lambda>

Stepping through the debugger unveils its part of atexit handling, which points at this unholy gem: https://github.com/sqlanywhere/sqlanydb/blob/338845cc18256d8b8c9a9b2383c58a196213b151/sqlanydb.py#L541

Anyone any idea? 😳

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Compare behavior at commit 3d3ec5724c6f76bc07d0631ec8061f26f9ecac4c with 40741c4aca50582cc9701ff01504b9e6dcd3396f, using the reported pytest options -q -X faulthandler. Start at the atexit handling referenced in sqlanydb.py line 541 and the traceback through rollback; done means isolating a reproducible cause and identifying the relevant pytest behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.