Report oauth_cb failures to librdkafka and fail construction fast #2441
All Vouch review confidence checks passed
Residual risk 0.42 = Change Risk (0.75) × (1 − Review Confidence (0.44)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.75 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.388
Shallow Review Depth
shallow_review_depth
0.344
Sensitive Paths
sensitive_paths
0.075
Real PR Conversation Timeline
1 comments
"Review active on #2441"
general
weight: 0.35
pylaterreur
What
----
Three problems with
oauth_cbfailures:1. When
oauth_cbreturned something other than a(token, expiry[, principal, extensions])tuple, or one of its extensions could not be converted, the Coauth_cb()left theTypeErrorset on librdkafka's background thread and never told librdkafka. librdkafka only schedules another token refresh once a token or a failure is set (rd_kafka_oauthbearer_enqueue_token_refresh_if_necessary()), so after one such return the client never asked for a token again. A raised exception was reported withrd_kafka_oauthbearer_set_token_failure(), but as "OAuth callback raised exception", without its message.2.
wait_for_oauth_token_set(), which the client constructors call, only watched for a token. A client whoseoauth_cbfailed during construction slept the full 10 seconds and raisedOAuth token not set within 10 seconds timeout, although librdkafka retries a failed refresh only 10 seconds later. It also slept in steps of a second, so creating any client with anoauth_cbtook at least a second, and it ignored Ctrl+C: theKeyboardInterruptstayed set until the token came, and the constructor then failed withSystemError: ... returned a result with an exception set.3. When the construction failed, destroying a
ConsumerorShareConsumerran the consumer close. The close served the queued_AUTHENTICATIONerror toerror_cbwhile the constructor'sKafkaExceptionwas set, so with anerror_cb, the constructor raisedSystemError: <built-in method append of list object at ...> returned a result with an exception setinstead. This is the latent bug thattest_share_consumer_oauth_expired_token_surfaces_error_cbintests/integration/share_consumer_oauth/test_oauth_integration.pyis skipped for.With this change:
1. Every failure (exception, invalid return value, extension that can't be converted, token rejected by librdkafka) is reported with
rd_kafka_oauthbearer_set_token_failure()asoauth_cb failed: <type>: <message>, and the exception is then cleared. librdkafka retries 10 seconds later, anderror_cbgets_AUTHENTICATION:Failed to acquire SASL OAUTHBEARER token: oauth_cb failed: TypeError: expect returned value from oauth_cb to be .... The extension strings already converted when a later one fails are now freed; they leaked.2.
wait_for_oauth_token_set()checks every 100 ms withHandle_sleep(). As soon asoauth_cbhas failed, it raisesKafkaExceptionwith the same code as before,SASL_AUTHENTICATION_FAILED, and the callback's error:Failed to acquire SASL OAUTHBEARER token: oauth_cb failed: RuntimeError: .... It also returns when a signal handler raises. The 10 second timeout is unchanged.3. A
ConsumerorShareConsumerwhose construction fails is destroyed withRD_KAFKA_DESTROY_F_NO_CONSUMER_CLOSE: it never subscribed, so the close only served callbacks.ProducerandAdminClientdon't serve callbacks when destroyed. TheConsumer's queue is destroyed before the handle, as librdkafka requires and asConsumer_close()does; it was left for dealloc, and leaked when__init__was called again.4.
common_conf_setup()clears the token state beforerd_kafka_new(), so that a client initialized again after a failed construction waits for its own token instead of failing with the first one's error.Checklist
------------------
- A client whose
oauth_cbfails during construction now raises right away, with the callback's error in the message, instead of after 10 seconds with a timeout message. TheKafkaErrorcode is unchanged. Creating a client with a workingoauth_cbtakes about 0.1 s instead of 1 s. CHANGELOG entry under Unreleased, which also says that the text of the callback's exception reaches librdkafka's log, and thaterror_cbgets an_AUTHENTICATIONerror for each failed refresh when that text changes every time.- New tests in
tests/test_oauth_cb.py:-
test_oauth_cb_token_refresh_invalid_return: a refresh returning an invalid value is reported to librdkafka, anderror_cbgets the callback's error. librdkafka's next refresh, 10 seconds after a failure, is not waited for.-
test_oauth_cb_failure_fails_construction_with_its_error:Consumer,Producer,AdminClientandShareConsumer, each with anerror_cband anoauth_cbthat raises or returns an invalid value, raise the callback's error, anderror_cbis not called.-
test_oauth_cb_failed_init_does_not_fail_the_next_one: a second__init__after a failed one waits for its own token.-
test_oauth_cb_token_wait_is_interruptible: Ctrl+C during the wait raisesKeyboardInterrupt.References
----------
tests/integration/share_consumer_oauth/test_oauth_integration.py::test_share_consumer_oauth_expired_token_surfaces_error_cbwas skipped for theSystemErrorof point 3, and runs again. Itsoauth_cbnow gives librdkafka an expiry in the future, so that only the broker rejects the expired token; with this PR, librdkafka's rejection of the expired token would fail the construction first. I could not run the integration tests (no broker here).Test & Review
------------
- the construction cases take 10 s, and a
ConsumerorShareConsumerwhose callback raises then fails with theSystemError;- the refresh test's
error_cbnever gets the error;- the interrupt test gets the
SystemError.test_oauth_cb_failed_init_does_not_fail_the_next_one, added after review, fails on master at its first__init__(timeout error instead of the callback's), and failed at its second__init__(the first one's error) beforecommon_conf_setup()cleared the token state.tests/test_oauth_cb.pypass on both. The existingtest_oauth_cb_failureand theShareConsumerOAuth tests intests/test_ShareConsumer_callbacks.pystill pass.test_oauth_cb_malformed_return_fails_constructionchecks that the message mentions the token or SASL, and it still does.tests/test_oauth_cb.pypassed 40 of 40 runs on each, 3 at a time on one CPU, each run with a differentPYTHONHASHSEED.Consumerinitialized again after a failed construction: 216 bytes definitely lost fromrd_kafka_queue_get_consumer()inConsumer_initwhen the queue was left for dealloc, none now.oauth_cbthat returns a token right away, 3 runs each: 1.00 s on master forProducer,AdminClientandConsumer, 0.10 s with this branch.tests/test_*.pyandtests/concurrency: 648 passed and 5 skipped on 3.11. On 3.14t, 487 passed, 7 skipped and 2 failed, plus 3 collection errors, all from my environment: the 2tests/test_unasync.pytests needisort, andtests/test_log.py,tests/test_Producer.pyandtests/test_serializer_error.pyneedurllib3, which my 3.14t environment lacks.tests/test_kafka_error.py::test_kafkaError_unknown_errorwas deselected there: with that environment's pluggy it stops the whole run, which https://github.com/confluentinc/confluent-kafka-python/pull/2444 fixes.tools/style-format.shandflake8pass on the test files. clang-format 18.1.3 reports no differences in the changed C lines, and neither gcc (with the build's flags) nor clang-Wall -Wextrawarns about them.CHANGELOG.mdconflicts with https://github.com/confluentinc/confluent-kafka-python/pull/2425 and https://github.com/confluentinc/confluent-kafka-python/pull/2429, which this PR already had before the review changes;Consumer.c,ShareConsumer.candconfluent_kafka.hmerge cleanly.Open questions / Follow-ups
--------------------------
🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN