Skip to content
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

Safely release resources in Handle_clear #434

Merged
merged 2 commits into from Aug 13, 2018

Conversation

coldeasy
Copy link

During tests we create and destroy instances of Consumer and Producer on every test case. We noticed segfaults and memory corruption (the logger pointer was referring to other random objects).

This seems to be due to the case where both tp_clear and tp_dealloc are called on the handle, resulting in a double release of error_cb, stats_cb and logger. I've tested this locally and have not encountered the segfaults/memory corruption since.

@ghost
Copy link

ghost commented Aug 10, 2018

It looks like @coldeasy hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement here.

Once you've signed reply with [clabot:check] to prove it.

Appreciation of efforts,

clabot

@rnpridgeon
Copy link
Contributor

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

@coldeasy
Copy link
Author

Thanks for the quick reply @rnpridgeon. Would you consider merging this to create a patch release in the meantime?

Copy link
Contributor

@edenhill edenhill left a 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)
Copy link
Contributor

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) {

Copy link
Author

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) {
Copy link
Contributor

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

Copy link
Author

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.
@coldeasy
Copy link
Author

[clabot:check]

@ghost
Copy link

ghost commented Aug 12, 2018

@confluentinc It looks like @coldeasy just signed our Contributor License Agreement. 👍

Always at your service,

clabot

Copy link
Contributor

@edenhill edenhill left a 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.

@edenhill edenhill merged commit 1a1586c into confluentinc:master Aug 13, 2018
@edenhill
Copy link
Contributor

Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants