Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cassandra/io/libevreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def _cleanup(self):
if not self._thread:
return

# Stop the prepare watcher first to prevent race conditions
if self._preparer:
self._preparer.stop()

for conn in self._live_conns | self._new_conns | self._closed_conns:
conn.close()
for watcher in (conn._write_watcher, conn._read_watcher):
Expand All @@ -125,8 +129,9 @@ def _cleanup(self):
self.notify() # wake the timer watcher

# PYTHON-752 Thread might have just been created and not started
# Use longer timeout to allow proper cleanup
with self._lock_thread:
self._thread.join(timeout=1.0)
self._thread.join(timeout=5.0)

if self._thread.is_alive():
log.warning(
Expand Down
16 changes: 15 additions & 1 deletion cassandra/io/libevwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ IO_dealloc(libevwrapper_IO *self) {
static void io_callback(struct ev_loop *loop, ev_io *watcher, int revents) {
libevwrapper_IO *self = watcher->data;
PyObject *result;
PyGILState_STATE gstate = PyGILState_Ensure();
PyGILState_STATE gstate;

if (!self || !self->callback) {
return; // Skip callback if object is being destroyed
}

gstate = PyGILState_Ensure();
if (revents & EV_ERROR && errno) {
result = PyObject_CallFunction(self->callback, "Obi", self, revents, errno);
} else {
Expand Down Expand Up @@ -354,6 +360,10 @@ static void prepare_callback(struct ev_loop *loop, ev_prepare *watcher, int reve
PyObject *result = NULL;
PyGILState_STATE gstate;

if (!self || !self->callback) {
return; // Skip callback if object is being destroyed
}

gstate = PyGILState_Ensure();
result = PyObject_CallFunction(self->callback, "O", self);
if (!result) {
Expand Down Expand Up @@ -473,6 +483,10 @@ static void timer_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
PyObject *result = NULL;
PyGILState_STATE gstate;

if (!self || !self->callback) {
return; // Skip callback if object is being destroyed
}

gstate = PyGILState_Ensure();
result = PyObject_CallFunction(self->callback, NULL);
if (!result) {
Expand Down
Loading