Skip to content

Commit d5a046a

Browse files
committed
Makes calling consumer.close() multiple times safe. Throws a
RuntimeError not a segfaults
1 parent d788937 commit d5a046a

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

confluent_kafka/src/Consumer.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,17 +550,24 @@ static PyObject *Consumer_poll (Handle *self, PyObject *args,
550550

551551

552552
static PyObject *Consumer_close (Handle *self, PyObject *ignore) {
553-
CallState cs;
554553

555-
CallState_begin(self, &cs);
554+
if (!self->rk) {
555+
PyErr_SetString(PyExc_RuntimeError,
556+
"Consumer already closed");
557+
return NULL;
558+
}
556559

557-
rd_kafka_consumer_close(self->rk);
560+
CallState cs;
558561

559-
rd_kafka_destroy(self->rk);
560-
self->rk = NULL;
562+
CallState_begin(self, &cs);
561563

562-
if (!CallState_end(self, &cs))
563-
return NULL;
564+
rd_kafka_consumer_close(self->rk);
565+
566+
rd_kafka_destroy(self->rk);
567+
self->rk = NULL;
568+
569+
if (!CallState_end(self, &cs))
570+
return NULL;
564571

565572
Py_RETURN_NONE;
566573
}

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