Repository navigation
Fix reference leak in pyodbc.dataSources() (#1502) - #1504
Merged
mkleehammer merged 1 commit intoNov 5, 2026
Merged
Conversation
mod_datasources() builds each result entry with PyUnicode_FromString(), which returns a new reference, and adds it with PyDict_SetItem(), which does not steal references. The key and value strings were never released, so every entry returned by pyodbc.dataSources() leaked both strings on every call. Py_XDECREF the key and value after inserting them, matching the pattern already used elsewhere in the file (e.g. mod_wrap around PyDict_SetItem). Verified with sys.getallocatedblocks(): a 3-DSN configuration leaked 6 blocks per call before (120001 over 20000 calls) and ~0 after. Fixes mkleehammer#1502.
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.
Summary
Fixes #1502.
mod_datasources()(backingpyodbc.dataSources()) builds each entry withPyUnicode_FromString(), which returns a new reference, and inserts it withPyDict_SetItem(), which does not steal references. Thekey/valstrings were never released, so every entry leaks both strings on every call.Fix
Py_XDECREFthekeyandvalafter inserting them (Py_XDECREFalso handles the case where an allocation above failed). This matches the pattern already used elsewhere — e.g.cursor.cpp(Py_DECREF(index)afterPyDict_SetItem) andpyodbcmodule.cpp:549(Py_XDECREF(str)afterPyDict_SetItem).Verification
Built locally against unixODBC with three dummy DSNs configured, using
sys.getallocatedblocks():dataSources()callsmasterpyodbc.dataSources()still returns the same dict of DSNs.Disclosure: prepared with AI assistance; reviewed and verified locally.