Skip to content

Fix reference leaks in option parsing - #1085

Merged
bimalkjha merged 1 commit into
ibmdb:masterfrom
rpcope1:fix/set-option-keys-leak
Nov 17, 2026
Merged

bimalkjha merged 1 commit into
ibmdb:masterfrom
rpcope1:fix/set-option-keys-leak

Conversation

@rpcope1

@rpcope1 rpcope1 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Release the list returned by PyDict_Keys() in _python_ibm_db_parse_options() on every return path, and release the reference returned by PyUnicode_FromObject() in the string-value branches of _python_ibm_db_assign_options() (statement, connection and zLOAD attributes). Without this, every set_option() call, and every connect()/prepare()/exec_immediate() given an options dict, leaks memory.

Closes #1084

Added ibm_db_tests/test_6795_SetOptionNoLeak.py. It counts list objects across 2000 set_option() calls, both successful and failing, and checks that string option values come back to their starting reference count.

Testing (Linux x86_64, Python 3.13, clidriver v11.5.9, Db2 LUW 11.5.8):

  • The new test fails on master (about one leaked list per call, refcount delta 1000) and passes with this change.
  • The full sync suite (python ibmdb_tests.py --sync) fails the same set of tests before and after this change. Those failures are environment gaps in my test database (missing sample tables) and are unrelated to this change.

_python_ibm_db_parse_options() never released the list returned by
PyDict_Keys(), so every set_option(), and every connect()/prepare()
given an options dict, leaked one list. Applications that set a
statement attribute on each prepared statement grow without bound.

The string-value branches of _python_ibm_db_assign_options() also
leaked the reference returned by PyUnicode_FromObject().

Add a regression test.

Signed-off-by: Robert Cope <robert@copesystems.com>
@bimalkjha
bimalkjha merged commit c44f898 into ibmdb:master Nov 17, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set_option() / options dicts leak a list on every call

2 participants