Repository navigation
Snapshot headers, message batches and topic lists with the GIL too - #2436
Conversation
Like the Admin methods before confluentinc#2432, Producer.produce(headers=...), Producer.produce_batch(), Consumer.subscribe() and the methods that take TopicPartition lists (py_to_c_parts()) took a private copy of the list or dict they convert only when Py_GIL_DISABLED is defined. The conversion runs Python code, though: str() of each header key in py_headers_list_to_c() and py_headers_dict_to_c(), and in produce_batch() the lookups of the message fields, which call __eq__ of any key with the same hash. That code can change the container, and with the GIL another thread can do the same there; the loop then goes on with a stale count and borrowed items. produce() with a header key whose __str__ clears the header list, and produce_batch() with such a message dict, segfaulted on Python 3.11. The extensions dict returned by an oauth_cb is converted the same way, into an array sized before the loop. Take these copies on every build. ShareConsumer.subscribe() took no copy at all: with a topic whose __str__ clears the list, it subscribed to that topic only. It now copies the list like Consumer.subscribe(). The copy of the configuration dict in common_conf_setup() stays free-threaded only: that dict is already a private copy. 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. |
|
Thanks for opening this PR. Could you provide the real world use case that your client faced this problem? |
|
No client reported this one; it is the same audit as #2432, applied to The failing case: an application that builds a header list, header dict or topic list once and reuses it across threads (for example a shared headers list that one thread edits while another calls 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
Like the Admin methods before #2432,
Producer.produce(headers=...),Producer.produce_batch(),Consumer.subscribe()and the methods that takeTopicPartitionlists (py_to_c_parts():assign(),commit(offsets=...),pause(), ...) convert a private copy of their list or dict only whenPy_GIL_DISABLEDis defined. With the GIL, they iterate the caller's container with a count read before the loop and borrowed items. The conversion runs Python code, though:str()of each header key, inpy_headers_list_to_c()andpy_headers_dict_to_c();produce_batch(), the lookups of the message fields (PyDict_GetItemString()), which call__eq__of any key of the message dict with the same hash.That code can change the container, and with the GIL another thread can take over there and do the same. With a header key whose
__str__clears the header list,produce()segfaults on Python 3.11, and so doesproduce_batch()with such a message dict. The extensions dict returned by anoauth_cbis converted the same way, into an array sized before the loop.This takes these copies on every build.
ShareConsumer.subscribe()took no copy at all: with a topic whose__str__clears the list, it subscribed to that topic only. It now copies the list likeConsumer.subscribe(). The copy of the configuration dict incommon_conf_setup()stays free-threaded only, because that dict is already a private copy.Checklist
tests/concurrency/test_memory_safety.py:test_produce_headers_list_cleared_by_key_str,test_produce_headers_dict_cleared_by_key_str(the value of the header being converted must outlive the clearing of the dict) andtest_produce_batch_list_cleared_by_message_key_eq, run in a subprocess like the other crash tests there. Intests/test_ShareConsumer.py:test_subscribe_list_cleared_by_topic_str.References
Admin.c.produce_batch()reject headers; theproduce_batch()test uses a message dict key instead, so it does not depend on headers.Test & Review
CPython 3.11.16 and 3.14.7t, librdkafka 2.16.0, Linux x86_64:
__str____str__produce_batch()list cleared by a key's__eq__ShareConsumer.subscribe()list cleared by a topic's__str__tests/test_Producer.py,tests/test_Consumer.py,tests/test_ShareConsumer.py,tests/test_oauth_cb.pyandtests/concurrency/test_memory_safety.py: 171 passed on 3.11; the same withouttests/test_Producer.py(its imports needurllib3, missing from my 3.14t environment), 116 passed on 3.14t.tools/style-format.shon the test files andflake8pass. clang-format 18.1.8 and 20.1.8 report no new differences in the changed C files.Merges without conflicts with Raise NotImplementedError on headers in produce_batch (#2320) #2408, Apply the google-re2 cap on Python 3.9 only #2426, Keep simulated Ctrl+C inside the interrupt tests #2427, Measure the timeout of chunked waits instead of counting chunks #2429, Parse a snapshot of the Admin API input lists with the GIL too #2432 and Purge the queue before closing the producers of the flush interrupt test #2433.
Open questions / Follow-ups
__str__adds entries to the dict.🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN