Fix reference leak and double-DECREF in pyodbc.drivers() (#1503)#1505
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix reference leak and double-DECREF in pyodbc.drivers() (#1503)#1505apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
mod_drivers() had two reference-counting bugs around the Object RAII wrapper: 1. Leak: after PyList_Append() (which keeps its own reference), the code called name.Detach(), which clears the wrapper without decrefing, leaking the string for every driver on every call. Drop the Detach() so the destructor releases it. 2. Double-DECREF: on the ODBC error path it did Py_DECREF(result) and then returned, after which ~Object() decref'd the same list again, double-freeing it. Drop the manual Py_DECREF and let the destructor release it once. Verified the leak fix with sys.getallocatedblocks(): a 3-driver configuration leaked 3 blocks per call before (60001 over 20000 calls) and ~0 after. Fixes mkleehammer#1503.
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 #1503.
mod_drivers()(backingpyodbc.drivers()) had two reference-counting bugs around theObjectRAII wrapper.1. Leaked driver name after
PyList_AppendPyList_Appendincrements the refcount, so the local reference should be released by~Object().Detach()prevents that, leaking one string per driver per call. Fix: drop theDetach().2. Double-DECREF of
resulton the ODBC error pathThe manual
Py_DECREFplus the destructor double-frees the list on the driver-manager error path. Fix: drop the manualPy_DECREFand let~Object()release it once.Verification
Leak (bug 1), built locally against unixODBC with three dummy drivers, using
sys.getallocatedblocks():drivers()callsmasterpyodbc.drivers()still returns the same list. Bug 2 is on the ODBC error path (hard to trigger without a broken driver manager), but the double-DECREF follows directly from theObjectwrapper semantics — the destructor already releasesresult.Disclosure: prepared with AI assistance; reviewed and verified locally.