Skip to content

Commit 3257fea

Browse files
authored
Merge pull request confluentinc#254 from johnistan/jdennison/multiple-close-calls-cause-segfault
Makes calling consumer.close() multiple times safe
2 parents 9e9d7cf + 5f57932 commit 3257fea

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

confluent_kafka/src/Consumer.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,19 +550,25 @@ static PyObject *Consumer_poll (Handle *self, PyObject *args,
550550

551551

552552
static PyObject *Consumer_close (Handle *self, PyObject *ignore) {
553-
CallState cs;
553+
CallState cs;
554+
555+
if (!self->rk) {
556+
PyErr_SetString(PyExc_RuntimeError,
557+
"Consumer already closed");
558+
return NULL;
559+
}
554560

555-
CallState_begin(self, &cs);
561+
CallState_begin(self, &cs);
556562

557-
rd_kafka_consumer_close(self->rk);
563+
rd_kafka_consumer_close(self->rk);
558564

559-
rd_kafka_destroy(self->rk);
560-
self->rk = NULL;
565+
rd_kafka_destroy(self->rk);
566+
self->rk = NULL;
561567

562568
if (!CallState_end(self, &cs))
563569
return NULL;
564570

565-
Py_RETURN_NONE;
571+
Py_RETURN_NONE;
566572
}
567573

568574

@@ -736,6 +742,7 @@ static PyMethodDef Consumer_methods[] = {
736742
"see :py:func::`poll()` for more info.\n"
737743
"\n"
738744
" :rtype: None\n"
745+
" :raises: RuntimeError if called on a closed consumer\n"
739746
"\n"
740747
},
741748
{ NULL }

tests/test_Consumer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,22 @@ def poll(self, somearg):
158158
sc = SubConsumer({"group.id": "test", "session.timeout.ms": "90"})
159159
sc.poll("astring")
160160
sc.close()
161+
162+
163+
def test_multiple_close_throw_exception():
164+
""" Calling Consumer.close() multiple times should throw Runtime Exception
165+
"""
166+
c = Consumer({'group.id': 'test',
167+
'enable.auto.commit': True,
168+
'enable.auto.offset.store': False,
169+
'socket.timeout.ms': 50,
170+
'session.timeout.ms': 100})
171+
172+
c.subscribe(["test"])
173+
174+
c.unsubscribe()
175+
c.close()
176+
177+
with pytest.raises(RuntimeError) as ex:
178+
c.close()
179+
assert 'Consumer already closed' == str(ex.value)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy