Skip to content

Commit fdee513

Browse files
committed
Fix for CONPY-144: Segfault in connection pool
Due to wrong reference count connection was closed by gc, after obtaining the same connection from ConnectionPool operations ended into a seg fault. Additionally 2 smaller memleaks were fixed.
1 parent f5da77b commit fdee513

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

mariadb/mariadb_pooling.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,20 @@ MrdbPool_initialize(MrdbPool *self, PyObject *args, PyObject *kwargs)
195195
goto error;
196196
}
197197
clock_gettime(CLOCK_MONOTONIC_RAW, &self->connection[i]->last_used);
198-
Py_INCREF(self->connection[i]);
199198
self->connection[i]->pool= self;
200199
}
201200
self->connection_cnt= self->pool_size;
202201
}
203202
PyDict_SetItemString(cnx_pool, self->pool_name, (PyObject *)self);
204-
Py_INCREF(self);
203+
204+
Py_XDECREF(conn_kwargs);
205+
Py_XDECREF(pool_kwargs);
205206

206207
return 0;
207208
error:
209+
Py_XDECREF(conn_kwargs);
210+
Py_XDECREF(pool_kwargs);
211+
208212
if (self->connection)
209213
{
210214
for (i=0; i < self->pool_size; i++)
@@ -300,10 +304,11 @@ MrdbPool_Type =
300304
void
301305
MrdbPool_dealloc(MrdbPool *self)
302306
{
303-
PyObject_GC_UnTrack(self);
304-
MrdbPool_close(self);
305-
306-
Py_TYPE(self)->tp_free((PyObject*)self);
307+
if (self)
308+
{
309+
MrdbPool_close(self);
310+
Py_TYPE(self)->tp_free((PyObject*)self);
311+
}
307312
}
308313
/* }}} */
309314

@@ -366,6 +371,7 @@ MrdbPool_getconnection(MrdbPool *self)
366371
pthread_mutex_unlock(&self->lock);
367372
if (conn)
368373
{
374+
Py_INCREF(conn);
369375
return (PyObject *)conn;
370376
}
371377
mariadb_throw_exception(NULL, Mariadb_PoolError, 0,
@@ -526,17 +532,21 @@ MrdbPool_close(MrdbPool *self)
526532
MARIADB_FREE_MEM(self->connection);
527533
self->connection= NULL;
528534
}
535+
529536
self->pool_size= 0;
530537

531538
if (self->pool_name)
532539
{
533-
if (PyDict_Contains(cnx_pool, PyUnicode_FromString(self->pool_name)))
540+
PyObject *obj= PyUnicode_FromString(self->pool_name);
541+
if (PyDict_Contains(cnx_pool, obj))
534542
{
535543
PyDict_DelItemString(cnx_pool, self->pool_name);
536544
}
537545
MARIADB_FREE_MEM(self->pool_name);
538546
self->pool_name= 0;
547+
Py_DECREF(obj);
539548
}
549+
Py_XDECREF(self->configuration);
540550
pthread_mutex_unlock(&self->lock);
541551
pthread_mutex_destroy(&self->lock);
542552
Py_INCREF(Py_None);

0 commit comments

Comments
 (0)