Skip to content

Commit eef31ea

Browse files
authored
Free buffered results without acquiring the connection lock (#815)
`mysql_store_result()` reads every row into client memory. When another Python thread drops the last reference to a buffered result, it can free that memory without taking the connection lock. This avoids the spurious `ProgrammingError` reported in #808 when the connection owner starts its next operation. Unbuffered results still take the lock because `mysql_free_result()` may read pending rows from the connection.
1 parent 2d37f14 commit eef31ea

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

‎src/MySQLdb/_mysql.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,22 @@ _mysql_ResultObject_Initialize(
327327
&_mysql_ConnectionObject_Type, &conn, &use, &conv))
328328
return -1;
329329

330-
self->conn = (PyObject *) conn;
331-
Py_INCREF(conn);
330+
/* A result keeps its original connection and buffering mode until deallocation. */
331+
bool already_initialized;
332+
#ifdef Py_GIL_DISABLED
333+
Py_BEGIN_CRITICAL_SECTION(self);
334+
#endif
335+
already_initialized = self->conn != NULL;
336+
if (!already_initialized) {
337+
self->conn = Py_NewRef(conn);
338+
}
339+
#ifdef Py_GIL_DISABLED
340+
Py_END_CRITICAL_SECTION();
341+
#endif
342+
if (already_initialized) {
343+
PyErr_SetString(_mysql_ProgrammingError, "Result is already initialized");
344+
return -1;
345+
}
332346
self->use = use;
333347
if (BEGIN_CONNECTION_LOCK(conn) < 0) {
334348
return -1;
@@ -2587,13 +2601,14 @@ static void
25872601
_mysql_ResultObject_dealloc(
25882602
_mysql_ResultObject *self)
25892603
{
2590-
PyObject *conn = self->conn;
25912604
PyObject_GC_UnTrack((PyObject *)self);
2592-
if (conn != NULL) {
2605+
/* Only an unbuffered result may need to read remaining rows from the connection. */
2606+
bool lock_connection = self->use && self->result != NULL && self->conn != NULL;
2607+
if (lock_connection) {
25932608
BEGIN_RESULT_CONNECTION_LOCK(self);
25942609
}
25952610
mysql_free_result(self->result);
2596-
if (conn != NULL) {
2611+
if (lock_connection) {
25972612
END_RESULT_CONNECTION_LOCK(self);
25982613
}
25992614
_mysql_ResultObject_clear(self);

‎tests/test_connection.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import enum
22
import threading
33
import time
4+
from concurrent.futures import ThreadPoolExecutor
45

56
import pytest
67
from configdb import connection_factory, connection_kwargs
78

89
from MySQLdb._exceptions import ProgrammingError
910
from MySQLdb.connections import Connection
10-
from MySQLdb.constants import CLIENT
11+
from MySQLdb.constants import CLIENT, FIELD_TYPE
1112

1213

1314
@pytest.fixture
@@ -168,6 +169,41 @@ def run_slow_query():
168169
assert thread_error is None
169170

170171

172+
@pytest.mark.parametrize("use", [False, True])
173+
def test_result_rejects_reinitialization(conn, use):
174+
conn.query("SELECT 1 UNION ALL SELECT 2 ORDER BY 1")
175+
result = conn.use_result() if use else conn.store_result()
176+
with pytest.raises(ProgrammingError, match="already initialized"):
177+
result.__init__(conn, not use)
178+
179+
assert result.fetch_row() == ((1,),)
180+
del result # Leave the second row for cleanup.
181+
conn.query("SELECT 3")
182+
assert conn.store_result().fetch_row() == ((3,),)
183+
184+
185+
def test_stored_result_dealloc_while_connection_locked(conn):
186+
stored = []
187+
188+
def store_result():
189+
conn.query("SELECT 1")
190+
stored.append(conn.store_result())
191+
192+
with ThreadPoolExecutor(max_workers=1) as executor:
193+
# On free-threaded CPython, drop the last reference on its owning thread.
194+
executor.submit(store_result).result()
195+
196+
def convert(value):
197+
# fetch_row holds the connection lock while calling this converter.
198+
executor.submit(stored.clear).result(timeout=5)
199+
return int(value)
200+
201+
conn.converter[FIELD_TYPE.NEWDECIMAL] = convert
202+
conn.query("SELECT CAST(2 AS DECIMAL(1,0))")
203+
assert conn.use_result().fetch_row() == ((2,),)
204+
assert not stored
205+
206+
171207
def test_result_concurrent_use_raises(conn):
172208
"""While fetch_row holds the connection lock streaming a slow result,
173209
any other access from a second thread must raise ProgrammingError immediately."""

0 commit comments

Comments
 (0)