-
Notifications
You must be signed in to change notification settings - Fork 933
Add null checks to fix SEGV #2122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds null checks to prevent segmentation faults when Kafka client methods are called on uninitialized instances (where super().__init__() was not called). The fix addresses issue #1590 by checking if the internal self->rk handle is NULL before operations and raising appropriate RuntimeError exceptions.
Key changes:
- Added standardized error message constants for closed/uninitialized client states
- Added NULL checks before operations in Producer, Consumer, and AdminClient methods
- Added comprehensive test coverage for all affected methods
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/confluent_kafka/src/confluent_kafka.h | Defines error message constants for uninitialized/closed client states |
| src/confluent_kafka/src/Producer.c | Adds NULL checks to Producer methods and uses standardized error messages |
| src/confluent_kafka/src/Consumer.c | Adds NULL checks to Consumer methods and uses standardized error messages |
| src/confluent_kafka/src/Admin.c | Adds NULL checks to AdminClient methods and uses standardized error messages |
| src/confluent_kafka/src/Metadata.c | Updates metadata methods to use standardized error message constants |
| tests/test_Producer.py | Adds test coverage for uninitialized Producer instance behavior |
| tests/test_Consumer.py | Adds test coverage for uninitialized Consumer instance behavior |
| tests/test_Admin.py | Adds test coverage for uninitialized AdminClient instance behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/confluent_kafka/src/Producer.c
Outdated
| if (!self->rk) { | ||
| rd_kafka_consumer_group_metadata_destroy(cgmd); | ||
| rd_kafka_topic_partition_list_destroy(c_offsets); | ||
| PyErr_SetString(PyExc_RuntimeError, ERR_MSG_PRODUCER_CLOSED); | ||
| return NULL; | ||
| } |
Copilot
AI
Nov 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cleanup of cgmd and c_offsets before the NULL check creates resource management complexity. Consider moving the NULL check before argument parsing to avoid unnecessary resource allocation and cleanup when the producer is uninitialized.
tests/test_Admin.py
Outdated
|
|
||
|
|
Copilot
AI
Nov 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line should be removed to maintain consistent spacing with other test functions.
54df42c to
14fc4a3
Compare
|


Overview
This PR fixes segmentation fault (SEGV) issues that occur when Kafka client methods are called on uninitialized instances. The fix addresses issue #1590
Problem
When a class is derived from
Producer,Consumer, orAdminClientand doesn't callsuper().__init__(), the internalself->rkpointer in the C code remainsNULL. Calling methods on such instances causes segmentation faults because these methods attempt to useself->rkwithout checking if it'sNULL.Example Crash Scenario
Solution
Null Checks Added
Added null checks before operations in all affected methods using a consistent pattern:
The checks are placed:
self->rk(to prevent NULL dereference)Changes
Producer (
src/confluent_kafka/src/Producer.c)Added null checks to 7 methods that were missing them:
Producer_produce_batch()- Added check beforerd_kafka_topic_new()Producer_init_transactions()- Added check beforerd_kafka_init_transactions()Producer_begin_transaction()- Added check beforerd_kafka_begin_transaction()Producer_send_offsets_to_transaction()- Added check with proper cleanup ofcgmdandc_offsetsProducer_commit_transaction()- Added check beforerd_kafka_commit_transaction()Producer_abort_transaction()- Added check beforerd_kafka_abort_transaction()Producer_purge()- Added check beforerd_kafka_purge()Consumer (
src/confluent_kafka/src/Consumer.c)Added null checks to all Consumer methods that were missing them:
Consumer_commit()Consumer_committed()Consumer_position()Consumer_seek()Consumer_get_watermark_offsets()Consumer_store_offsets()Consumer_offsets_for_times()Consumer_consumer_group_metadata()AdminClient (
src/confluent_kafka/src/Admin.c)Added null checks to all AdminClient methods:
Admin_create_topics()Admin_delete_topics()Admin_create_partitions()Admin_describe_configs()Admin_alter_configs()Admin_incremental_alter_configs()Admin_create_acls()Admin_delete_acls()Admin_describe_acls()Admin_list_consumer_groups()Admin_describe_consumer_groups()Test Coverage
Producer Tests (
tests/test_Producer.py)Consumer Tests (
tests/test_Consumer.py)AdminClient Tests (
tests/test_Admin.py)All tests verify that:
RuntimeErrorinstead of crashing