使用SQLAlchemy 2.0查询MariaDB时,Python崩溃(_PyMem_PygRawFree)

a6b3iqyw  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(184)

由于我不知道这个问题是由我的普通语法错误还是SQLAlchemy,MariaDB Connector或Python引起的,我在这里问:
我的应用程序在SQLAlchemy 1.4、MariaDB 1.4和Python 3.10中运行良好。为了迁移到SQLAlchemy 2.0,我对SQLAlchemy的ORM部分进行了调整。Python崩溃发生在SQLAlchemy 2.0.23、MariaDB Connector 1.1.8、Python 3.12.0、MariaDB 11.1(也是10.4)、Windows 10 x64 22H2
最新消息:感谢下面Gord Thompson的评论,我现在知道它可以与PyMySQL连接器一起工作。所以问题似乎在MariaDB连接器中。
对这张table

CREATE TABLE `accounts` (
    `id` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(40) NOT NULL COLLATE 'utf8mb3_general_ci',
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE INDEX `name` (`name`) USING BTREE
)

字符串
Python在MariaDB连接器中崩溃,代码如下:

db = 'mariadb+mariadbconnector://***:***@localhost/test'
engine = sa.create_engine(db)
Session = orm.sessionmaker(bind=engine)
class Base(orm.DeclarativeBase):
   pass

class Account(Base):
   __tablename__ = 'accounts'

   _id: orm.Mapped[int] = orm.mapped_column('id', sa.Integer, primary_key=True)
   _name: orm.Mapped[str] = orm.mapped_column('name', sa.String)

with Session() as sess:
   sel = sa.select(Account) \
      .where(Account._name == 'My Account')
   res = sess.execute(sel).scalars()
   for acc in res:
      print(acc._name)


当调用sess.execute时,输出如下:

C:\Users\Jens\Projects\Mails\Python> c: && cd c:\Users\Jens\Projects\Mails\Python && cmd /C ""C:\Program Files\Python\Python312\python.exe" c:\Users\Jens\.vscode\extensions\ms-python.python-2023.20.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher 50504 -- C:\Users\Jens\Projects\Mails\Python\test.py "
Debug memory block at address p=000001BDB62F37D0: API 'r'
    15 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=000001BDB62F37DF are FORBIDDENBYTE, as expected.
    Data at p: 39 31 30 30 33 38 35 00 00 00 00 00 00 00 00

Enable tracemalloc to get the memory block allocation traceback

Fatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API 'r', verified using API 'm'
Python runtime state: initialized

[...]

Current thread 0x00000edc (most recent call first):
  File "C:\Program Files\Python\Python312\Lib\site-packages\mariadb\cursors.py", line 135 in _substitute_parameters
  File "C:\Program Files\Python\Python312\Lib\site-packages\mariadb\cursors.py", line 306 in execute
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\engine\default.py", line 922 in do_execute
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1969 in _exec_single_context
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1848 in _execute_context
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1639 in _execute_clauseelement
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\sql\elements.py", line 516 in _execute_on_connection
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1416 in execute
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\orm\context.py", line 293 in orm_execute_statement
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\orm\session.py", line 2190 in _execute_internal
  File "C:\Program Files\Python\Python312\Lib\site-packages\sqlalchemy\orm\session.py", line 2308 in execute
  File "C:\Users\Jens\Projects\Mails\Python\test.py", line 22 in <module>
[...]

Extension modules: _wmi, sqlalchemy.cyextension.collections, sqlalchemy.cyextension.immutabledict, sqlalchemy.cyextension.processors, sqlalchemy.cyextension.resultproxy, sqlalchemy.cyextension.util, greenlet._greenlet, mariadb._mariadb (total: 8)


当SQL结构中的.where子句被省略时,它不会崩溃。
感谢任何帮助...

9o685dep

9o685dep1#

我终于能够重现这个bug -崩溃似乎是由在where子句中替换参数时错误的引用计数引起的。
我在MariaDB bug跟踪器中提交了一个ticket,一旦这个问题得到解决,我会更新我的答案。

**更新:**问题已修复,将在MariaDB Connector/Python 1.1.9中提供

相关问题