Skip to content

Snapshot headers, message batches and topic lists with the GIL too - #2436

Open
Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/snapshot-headers-and-batches-with-gil
Open

Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/snapshot-headers-and-batches-with-gil

Conversation

@pylaterreur

Copy link
Copy Markdown
Contributor

What

Like the Admin methods before #2432, Producer.produce(headers=...), Producer.produce_batch(), Consumer.subscribe() and the methods that take TopicPartition lists (py_to_c_parts(): assign(), commit(offsets=...), pause(), ...) convert a private copy of their list or dict only when Py_GIL_DISABLED is 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, in py_headers_list_to_c() and py_headers_dict_to_c();
  • in 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 does produce_batch() with such a message dict. The extensions dict returned by an oauth_cb is 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 like Consumer.subscribe(). The copy of the configuration dict in common_conf_setup() stays free-threaded only, because that dict is already a private copy.

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • These calls no longer crash when Python code they run changes the container. CHANGELOG entry under Unreleased.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • In 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) and test_produce_batch_list_cleared_by_message_key_eq, run in a subprocess like the other crash tests there. In tests/test_ShareConsumer.py: test_subscribe_list_cleared_by_topic_str.

References

Test & Review

Open questions / Follow-ups

  • The oauth extensions copy has no test: its loop only overflows the array if a key's __str__ adds entries to the dict.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

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
@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.

@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; it is the same audit as #2432, applied to Producer.produce(headers=...), produce_batch(), Consumer.subscribe() and the TopicPartition list methods.

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 produce()), or passes keys whose __str__/__eq__ run arbitrary code. With the GIL the conversion loop can switch threads inside str(), and the container then changes under it. In the reproductions this segfaults on 3.11 (produce() with a header key whose __str__ clears the list, produce_batch() likewise). ShareConsumer.subscribe() took no copy at all, and subscribed to 1 of 3 topics when a topic's __str__ cleared the list.

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.

2 participants