Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 197651b

Browse files
committed
Issue #10133: Make multiprocessing deallocate buffer if socket read fails.
Patch by Hallvard B Furuseth.
1 parent 2a2ce4f commit 197651b

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Core and Builtins
7070
Library
7171
-------
7272

73+
- Issue #10133: Make multiprocessing deallocate buffer if socket read
74+
fails. Patch by Hallvard B Furuseth.
75+
7376
- Issue #13854: Make multiprocessing properly handle non-integer
7477
non-string argument to SystemExit.
7578

Modules/_multiprocessing/socket_connection.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static Py_ssize_t
117117
conn_recv_string(ConnectionObject *conn, char *buffer,
118118
size_t buflength, char **newbuffer, size_t maxlength)
119119
{
120-
int res;
120+
Py_ssize_t res;
121121
UINT32 ulength;
122122

123123
*newbuffer = NULL;
@@ -132,20 +132,23 @@ conn_recv_string(ConnectionObject *conn, char *buffer,
132132
if (ulength > maxlength)
133133
return MP_BAD_MESSAGE_LENGTH;
134134

135-
if (ulength <= buflength) {
136-
Py_BEGIN_ALLOW_THREADS
137-
res = _conn_recvall(conn->handle, buffer, (size_t)ulength);
138-
Py_END_ALLOW_THREADS
139-
return res < 0 ? res : ulength;
140-
} else {
141-
*newbuffer = PyMem_Malloc((size_t)ulength);
142-
if (*newbuffer == NULL)
135+
if (ulength > buflength) {
136+
*newbuffer = buffer = PyMem_Malloc((size_t)ulength);
137+
if (buffer == NULL)
143138
return MP_MEMORY_ERROR;
144-
Py_BEGIN_ALLOW_THREADS
145-
res = _conn_recvall(conn->handle, *newbuffer, (size_t)ulength);
146-
Py_END_ALLOW_THREADS
147-
return res < 0 ? (Py_ssize_t)res : (Py_ssize_t)ulength;
148139
}
140+
141+
Py_BEGIN_ALLOW_THREADS
142+
res = _conn_recvall(conn->handle, buffer, (size_t)ulength);
143+
Py_END_ALLOW_THREADS
144+
145+
if (res >= 0) {
146+
res = (Py_ssize_t)ulength;
147+
} else if (*newbuffer != NULL) {
148+
PyMem_Free(*newbuffer);
149+
*newbuffer = NULL;
150+
}
151+
return res;
149152
}
150153

151154
/*

0 commit comments

Comments
 (0)