Repository navigation
fix(api): SQL Server 新连接上的第一条查询偶发 ODBC SQL type -155(#442) - #443
Merged
Merged
Conversation
SQLAlchemy 的 mssql 方言在 on_connect 里注册 DATETIMEOFFSET 转换器,走 aioodbc 时 那一步进了线程池、返回的 future 没人等,第一条查询可能先跑。connect 事件里同步往 底层 pyodbc 连接注册同一个转换器;create_database_async_engine 和 alembic env.py 两处都挂。最小复现 + 回归用例(方言那步换成永不完成的 future,修前稳定 -155)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #442
症状
#441 的 CI 里
pytest · SQL Server红了一条与改动无关的审批并发用例:ODBC SQL type -155 is not yet supported(-155 =DATETIMEOFFSET,本仓库所有时间列在 SQL Server 上都是它)。生产上同样会发生:连接池新建连接后的第一条读时间列的查询,偶发 500。根因(已实测)
SQLAlchemy 的 mssql 方言在
on_connect里注册 -155 的转换器;走 aioodbc 时那一步被丢进线程池、返回的 future 没人等,第一条查询可能先于注册执行。最小复现:把线程池里的注册拖慢 0.3 秒,新连接的第一条
SELECT CAST(SYSDATETIMEOFFSET() AS DATETIMEOFFSET)稳定报出同一句 -155。修法
backend/database/db.py: register_mssql_datetimeoffset——connect事件里同步往底层 pyodbc 连接注册同一个转换器(此时连接上还没有任何查询)。挂在create_database_async_engine和alembic/env.py(它自己建引擎)两处;非 SQL Server 不生效。验证
backend/tests/test_mssql_datetimeoffset.py(只在 SQL Server 跑,方言那一步换成永不完成的 future):修前两条红在 -155,修后通过;核对了微秒截断和负偏移-03:30test_concurrency.py连跑 5 遍全过backend/tests全套 511 passed · 14 skipped · 1 xfailed · 0 失败apps/api/backend/database/AGENTS.md记了症状 / 根因 / 修法 / 证据🤖 Generated with Claude Code