Repository navigation
Parse a snapshot of the Admin API input lists with the GIL too - #2432
Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
Conversation
The AdminClient methods that take a list, such as alter_configs(), create_topics(), delete_topics() or alter_user_scram_credentials(), parse a private snapshot of it, from PyList_GetSlice() or PyDict_Copy(), but only when Py_GIL_DISABLED is defined. With the GIL they iterated the caller's list with a count read before the loop and borrowed items, as if nothing else could run in between. The loops run Python code, though: str() of topic names, group ids and config values, attribute getters, and in alter_user_scram_credentials() the .value of each ScramMechanism, an Enum property. That code can change the list, and another thread can take the GIL there and do the same; the loop then reads freed or out-of-bounds items. For example, alter_configs() segfaults on Python 3.11 when a set_config value clears the list in its __str__(). describe_consumer_groups() and describe_user_scram_credentials() also hand strings borrowed from the items to librdkafka after releasing the GIL. Take the snapshots on every build. The new tests clear the list from such a callback, one per method: all ten segfaulted on a GIL build, and they pass on both builds now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the AdminClient snapshot-protection changes.
No open issues 🎉
What changed in this PR
Extends the AdminClient snapshot protections introduced in #2334 to GIL-enabled Python, preventing crashes when input containers change during parsing.
Changes:
- Makes existing list and configuration-dictionary snapshots unconditional in
Admin.cand documents why the GIL alone is insufficient. - Adds 10 subprocess-isolated regression tests for list mutation triggered by string conversion, attribute getters, and Enum property access.
- Documents the customer-facing crash fix in the changelog.
| File | Description |
|---|---|
tests/concurrency/test_memory_safety.py |
Adds deterministic re-entrant list-mutation tests for 10 AdminClient methods. |
src/confluent_kafka/src/Admin.c |
Enables existing snapshot creation and cleanup on all builds. |
CHANGELOG.md |
Records the AdminClient memory-safety fix under Unreleased. |
Review effort: Balanced
|
Thanks for opening this PR. Could you provide the real world use case that your client faced this problem? |
|
No client reported this one; I found it by reading The failing case: an application that passes a list to I have not seen it in a real deployment. If you only want fixes for incidents seen in production, tell me and I'll close it. |
What
Since #2334, the
AdminClientmethods that take a list parse a private snapshot of it (PyList_GetSlice(), andPyDict_Copy()for config dicts), but only on free-threaded builds: the snapshots are under#ifdef Py_GIL_DISABLED. With the GIL, the parse loops iterate the caller's list with a count read before the loop and borrowed items. Those loops run Python code, though, and the GIL can switch threads there:str()of each topic (delete_topics()), group id (describe_consumer_groups(),delete_consumer_groups()), user (describe_user_scram_credentials()) and config value (alter_configs(),create_topics());describe_configs(),alter_configs(),create_acls(),delete_acls());alter_user_scram_credentials(), the.valueof eachScramMechanism, an Enum property.That code, or another thread that takes the GIL there, can change the caller's list, and the loop then reads freed or out-of-bounds items. For example,
alter_configs()segfaults on Python 3.11 when a config value clears the list in its__str__().describe_consumer_groups()anddescribe_user_scram_credentials()also pass strings borrowed from the items to librdkafka after releasing the GIL, while another thread can free them.This takes the snapshots of
Admin.con every build, which removes its 62#ifdef Py_GIL_DISABLEDblocks, and adds a comment that says why.Checklist
tests/concurrency/test_memory_safety.py, one per method, run in a subprocess like the race tests there. In each, something the parse loop calls clears the list. They need no second thread.References
Test & Review
describe_consumer_groups()while another replaces the items of the same list: 6 invalid reads of freedstrobjects on master, none with this change.python -m pytest --timeout 1200 --ignore=dest --ignore=tests/integrationon 3.11: 2564 passed, 16 skipped.tests/test_Admin.pyandtests/concurrencyon 3.14t: 102 passed.tools/style-format.shandflake8pass.Open questions / Follow-ups
test_alter_configs_races_list_mutationfailed once in an earlier run on a GIL build under load. This change does not explain it: with that test's plainConfigResourceitems the loop calls no Python code on GIL builds (measured). It did not reproduce in 400 runs.describe_consumer_groups()anddescribe_user_scram_credentials()keep thechar *ofstr(item)after releasing the string (for astrsubclass such as aStrEnummember the pointer dangles);create_topics()andcreate_partitions()check items withPyObject_IsInstance()before casting;Producer.produce(headers=[...])andproduce_batch()have the same GIL-only snapshot gap.🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN