Skip to content

Parse a snapshot of the Admin API input lists with the GIL too - #2432

Open
Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/admin-snapshot-lists-gil-build
Open

Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/admin-snapshot-lists-gil-build

Conversation

@pylaterreur

Copy link
Copy Markdown
Contributor

What

Since #2334, the AdminClient methods that take a list parse a private snapshot of it (PyList_GetSlice(), and PyDict_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());
  • attribute reads on the items, which run getters when the item's class defines them (describe_configs(), alter_configs(), create_acls(), delete_acls());
  • in alter_user_scram_credentials(), the .value of each ScramMechanism, 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() and describe_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.c on every build, which removes its 62 #ifdef Py_GIL_DISABLED blocks, and adds a comment that says why.

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • These methods no longer crash when their list changes during the call. CHANGELOG entry under Unreleased.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • 10 tests in 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

  • CPython 3.11.16 (GIL) and 3.14.7t, librdkafka 2.16.0, Linux x86_64, next to 6 busy loops: the new tests segfault in every run on master with 3.11 (100 of 100) and all pass with this change; on 3.14t all pass either way.
  • valgrind on 3.11, one thread calling describe_consumer_groups() while another replaces the items of the same list: 6 invalid reads of freed str objects on master, none with this change.
  • python -m pytest --timeout 1200 --ignore=dest --ignore=tests/integration on 3.11: 2564 passed, 16 skipped. tests/test_Admin.py and tests/concurrency on 3.14t: 102 passed.
  • Cost (callgrind, calling thread, GC off): the C methods alone with 2000 items go from 9.81M to 10.04M instructions per call (+2.4%); a public call with 2000 items is about +0.6%.
  • tools/style-format.sh and flake8 pass.

Open questions / Follow-ups

  • test_alter_configs_races_list_mutation failed once in an earlier run on a GIL build under load. This change does not explain it: with that test's plain ConfigResource items the loop calls no Python code on GIL builds (measured). It did not reproduce in 400 runs.
  • Not changed here: describe_consumer_groups() and describe_user_scram_credentials() keep the char * of str(item) after releasing the string (for a str subclass such as a StrEnum member the pointer dangles); create_topics() and create_partitions() check items with PyObject_IsInstance() before casting; Producer.produce(headers=[...]) and produce_batch() have the same GIL-only snapshot gap.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

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
@confluent-cla-assistant

Copy link
Copy Markdown

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ pylaterreur
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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.c and 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

@Ankith-Confluent

Copy link
Copy Markdown
Member

Thanks for opening this PR. Could you provide the real world use case that your client faced this problem?

@pylaterreur

Copy link
Copy Markdown
Contributor Author

No client reported this one; I found it by reading Admin.c after #2334, whose snapshots only apply on free-threaded builds.

The failing case: an application that passes a list to AdminClient.delete_topics(), describe_consumer_groups(), alter_configs() and similar, and changes that list from another thread while the call runs (or whose items have a __str__/getter that does). On a GIL build, the parse loop runs Python code, so the list can change under it and the loop reads freed or out-of-bounds items. A shared, reused list of topic names that another thread edits is a plausible way to hit it. It is a crash, not an exception: alter_configs() segfaults on 3.11 when a config value clears the list in its __str__(), 100 of 100 runs, and valgrind shows 6 invalid reads of freed str objects for describe_consumer_groups() racing a second thread.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:pending-close Issue scheduled for closure — comment if still relevant

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants