-
Notifications
You must be signed in to change notification settings - Fork 915
Safely release resources in Handle_clear #434
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
It looks like @coldeasy hasn't signed our Contributor License Agreement, yet.
You can read and sign our full Contributor License Agreement here. Once you've signed reply with Appreciation of efforts, clabot |
Good catch @coldeasy, this actually came up very recently as t causes issues for python, .net and likely our golang client as well. In order to handle this more elegantly the following PR has been opened for librdkafka. Once merged the plan is to adapt python to use the new immediate closure flags confluentinc/librdkafka#1932 |
Thanks for the quick reply @rnpridgeon. Would you consider merging this to create a patch release in the meantime? |
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.
Looks good!
Need to fix indentation/style and remove whitespace-only diffs
@@ -1324,24 +1324,37 @@ static void log_cb (const rd_kafka_t *rk, int level, | |||
* Clear Python object references in Handle | |||
*/ | |||
void Handle_clear (Handle *h) { | |||
if (h->error_cb) | |||
Py_DECREF(h->error_cb); | |||
if (h->error_cb) |
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.
Use 8 spaces for indent.
Put open-brace on same line as if
:
if (h->error_cb) {
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.
Updated, please take another look.
|
||
if (h->initiated) { |
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.
no whitespace/indent changes please
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.
Fixed
During garbage collection tp_clear and tp_dealloc can both be called which resulted in a double release of references. This change safely decrements the reference count and invalidates the pointers.
[clabot:check] |
@confluentinc It looks like @coldeasy just signed our Contributor License Agreement. 👍 Always at your service, clabot |
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.
LGTM!
The travis failures seems to be an unrelated change in Travis' Ruby image that we use for Python.. Will fix.
Thank you for this! |
During tests we create and destroy instances of
Consumer
andProducer
on every test case. We noticed segfaults and memory corruption (thelogger
pointer was referring to other random objects).This seems to be due to the case where both
tp_clear
andtp_dealloc
are called on the handle, resulting in a double release oferror_cb
,stats_cb
andlogger
. I've tested this locally and have not encountered the segfaults/memory corruption since.