Deadlock under concurrent multithreaded use when DEBUG logging is enabled via setup_logging()
維護者通常 2 天內回覆
@bewithgaurav 已經在處理了。
開始於 2026年7月10日。
評估
這個 Issue 還沒有評估資料。
描述
Describe the bug
When driver logging is enabled via mssql_python.setup_logging(output="file") (DEBUG level) and multiple threads concurrently open connections and execute statements (for example a ThreadPoolExecutor with 4 workers), the process permanently deadlocks. It sits at 0% CPU — fully frozen, not merely slow.
The same code with setup_logging not called does not deadlock. It also does not deadlock with a single worker thread. The trigger is therefore the combination of DEBUG logging + concurrency (workers > 1).
Environment
- mssql-python version: 1.10.0
- Python version: 3.12.11
- Operating system: macOS (arm64)
- Authentication:
ActiveDirectoryDefault(Entra ID) against a Microsoft Fabric Warehouse. The deadlock is in the logging path and is very likely authentication-independent.
Observed thread stacks
Captured with Python's faulthandler. The dumps were identical across 7 dumps over roughly 7 minutes, which confirms a permanent deadlock rather than slowness.
Thread A (opening a connection):
File ".../mssql_python/connection.py", line 571 in setautocommit
File ".../mssql_python/connection.py", line 410 in __init__
File ".../mssql_python/db_connection.py", line 55 in connect
-> (calls into Python logging) ...
File ".../logging/handlers.py", line 75 in emit
File ".../logging/__init__.py", line 1144 in flush # blocked here
Thread B (executing a statement, a COPY INTO):
File ".../mssql_python/cursor.py", line 1545 in execute # blocked in native execute
Server-side check
While the client is frozen, the database server shows no query running for this session. This confirms the block is entirely client-side and is not waiting on the server.
Hypothesized mechanism
mssql-python emits DEBUG log records from within native code paths (for example setautocommit during connect, and cursor.execute) that hold the GIL / internal locks. Under concurrency this creates a lock-acquisition cycle with Python's logging Handler locks across threads:
- Thread A holds the logging handler lock and is blocked in
flush. - Thread B is blocked in native
executeand cannot proceed.
This is a classic logging-from-native-under-the-GIL deadlock. It only reproduces with workers > 1.
Steps to reproduce (minimal sketch)
- Call
mssql_python.setup_logging(output="file")to enable DEBUG logging. - From
N >= 2threads, concurrently open connections (autocommit=True) and run statements against the same server.
The process deadlocks within seconds to a couple of minutes.
Expected behavior
Enabling DEBUG logging should not affect correctness. Concurrent, multithreaded workloads should run to completion whether or not setup_logging is enabled.
Workaround
Do not enable setup_logging for concurrent / multithreaded workloads.
Impact
This makes the DEBUG logging feature unusable for any multithreaded application (for example parallel data-loading), which is precisely the scenario where enabling DEBUG logging is most valuable for diagnosis.
Possibly related issues
The following existing issues appear related but are distinct from this report:
- #540 — GIL held for the entire duration of
cursor.execute()(describes the native-call-holds-GIL behaviour that is part of the hypothesized mechanism here, but is about starvation rather than a permanent deadlock and does not involve logging). - #321 — multithreaded hang/deadlock with 3+ threads (does not involve logging; closed as non-reproducible).
- #421 — hang with
setup_loggingenabled (single-threaded, diagnosed as a driver-load hang on Linux rather than a concurrency logging-lock deadlock).
- 主要語言
- Python
- 星號
- 478
- 分支
- 62
- 平均合併
- 3 天 19 分鐘
- 30 天內合併 PR
- 28
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
microsoft/mssql-python 的其他 Issue
-
area: bulk-copy triage needed
難度 2/5 1-3 小時 新手友好度 70/100
microsoft/mssql-python#843 · 1 則留言 ·
維護者通常 2 天內回覆
-
triage needed
難度 4/5 3-5 天 新手友好度 35/100
microsoft/mssql-python#846 · 1 則留言 ·
維護者通常 2 天內回覆
-
macOS: encrypted connections fail after Homebrew moved the `openssl` alias to `openssl@4`可能已有人在做 @bewithgaurav 於 15 天前認領。 未關閉area: packaging-platform bug triage needed
難度 4/5 3-5 天 新手友好度 55/100
microsoft/mssql-python#826 · 2 則留言 · 已指派 1 人 ·
維護者通常 2 天內回覆
-
Announcement: mssql-python is coming to Django through mssql-django 2.0可能重新可做 @bewithgaurav 於 42 天前認領,目前沒有進行中的 PR。 未關閉announcement
microsoft/mssql-python#748 · 已指派 1 人 ·
維護者通常 2 天內回覆
-
Connection pool can briefly exceed max_size when pooling is disabled under load可能已有人在做 @bewithgaurav 於 24 天前認領。 未關閉area: connectivity-auth bug triage done under development
難度 4/5 3-5 天 新手友好度 62/100
microsoft/mssql-python#746 · 1 則留言 · 已指派 1 人 ·
維護者通常 2 天內回覆
查看 microsoft/mssql-python 的全部 Issue
相似的 Issue
-
fix formatting of the sample database page of the docs可能已有人在做 @Aaqibhafeezkhan 今天認領。 未關閉documentation good first issue help wanted
難度 1/5 1 小時以內 新手友好度 72/100
chaoss/CollectOSS#544 · 1 則留言 ·
維護者通常 1 天內回覆
-
area/plugin good first issue
難度 2/5 1-3 小時 新手友好度 85/100
kestra-io/plugin-soda#137 ·
-
難度 2/5 半天 新手友好度 62/100
ibm-granite/granite.build#504 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆