← Back to Pull requests

Keep the str() of group and user names until librdkafka has copied them #2434

Closed
unknown closed this pull request without merging on Nov 19, 2026 09:48 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 23:44 UTC
Author

What
----
describe_consumer_groups(), describe_user_scram_credentials() and delete_consumer_groups() pass librdkafka the UTF-8 data of str() of each name, and release that str right after taking its data. librdkafka reads the data later: after the loop for the first two, and right after the release for delete_consumer_groups(). For an exact str, str() returns the item itself, which the list keeps alive. For a str subclass, such as an enum.StrEnum member, it returns a new object: the pointers dangle, and the next names reuse the same memory. On Python 3.11, describe_consumer_groups() with two such distinct group ids fails with "Duplicate groups not allowed", and describe_user_scram_credentials() with "Duplicate users aren't allowed in the same request". A name with a lone surrogate has no UTF-8 data at all, and the NULL pointer crashed the process.

describe_consumer_groups() and describe_user_scram_credentials() now keep the converted strings in a list until the request has been created, delete_consumer_groups() releases each one once rd_kafka_DeleteGroup_new() has copied it, and a name that cannot be encoded raises UnicodeEncodeError.

Checklist
------------------

  • Contains customer facing changes? Including API/behavior changes

  • - These methods no longer reject distinct str-subclass names as duplicates, and raise UnicodeEncodeError instead of crashing. 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_str_of_names_kept_until_the_request_is_made, for describe_consumer_groups() and describe_user_scram_credentials(), gives two names whose str() returns a copy whose release waits for the result of the request, which only comes once the request has been made with the names.
    - test_delete_consumer_groups_copies_names_before_releasing_them: two distinct such names make a request instead of being rejected as duplicates.
    - test_describe_consumer_groups_name_not_encodable_as_utf8_raises and the same for the two other methods, each run in a subprocess like the other crash tests there.

    References
    ----------

  • Found while checking #2432, which takes the snapshots of these lists on every build: the snapshot keeps the names alive, not their str() copies.
  • Test & Review
    ------------

  • CPython 3.11.16 (GIL) and 3.14.7t (GIL disabled), librdkafka 2.16.0, Linux x86_64:
  • | | master | this branch |
    |---|---|---|
    | test_str_of_names_kept_until_the_request_is_made, 2 methods | fail on both: [False, False] == [True, True] | pass on both |
    | test_delete_consumer_groups_copies_names_before_releasing_them | fails on 3.11 ("Duplicate groups not allowed"), passes on 3.14t | passes on both |
    | test_*_name_not_encodable_as_utf8_raises, 3 methods | segfault on both (returncode -11) | pass on both |

  • With the list of describe_consumer_groups() and describe_user_scram_credentials() released right after the conversion loop, before the request, test_str_of_names_kept_until_the_request_is_made fails for both on 3.11, while the earlier test of this PR, test_str_of_names_kept_until_all_are_converted, passed.

  • The new tests passed 60 of 60 runs on each interpreter, 3 at a time on one CPU, each run with a different PYTHONHASHSEED.
  • On master, Python 3.11: describe_consumer_groups([Name.ALICE, Name.BOB]), with class Name(str, enum.Enum) and __str__ = str.__str__ as in enum.StrEnum, fails with "Duplicate groups not allowed", and describe_user_scram_credentials() with "Duplicate users aren't allowed in the same request".

  • tests/test_Admin.py and tests/concurrency/test_memory_safety.py (with the consumer group and SCRAM race tests): 65 passed on 3.11 and on 3.14t.

  • tools/style-format.sh on the test file and flake8 pass. clang-format 18.1.3 and 21.1.8 report no new differences in Admin.c, and gcc no warnings.

  • Merges without conflicts with master and with #2408, #2427, #2429, #2432 and #2433.
  • Open questions / Follow-ups
    --------------------------

  • The other unchecked UTF-8 conversions (topic, configuration and header names) are handled in a separate PR.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

    Diff: 545 lines (+408 / -137) across 6 files Reviewer: pylaterreur Review duration: 7803m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 545 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2434"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48% (MEDIUM) · Higher = larger blast radius
    48%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.8% · Safe to merge
    20.8%