Skip to content

Commit de6338e

Browse files
committed
python: use Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS
Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent b56b92e commit de6338e

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

python/crun_python.c

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ make_context (PyObject *self, PyObject *args, PyObject *kwargs)
139139
ctx->state_root = xstrdup (state_root);
140140
ctx->notify_socket = xstrdup (notify_socket);
141141
return PyCapsule_New (ctx, CONTEXT_OBJ_TAG, NULL);
142-
143142
}
144143

145144
static PyObject *
@@ -150,6 +149,7 @@ container_run (PyObject *self, PyObject *args)
150149
PyObject *ctr_obj = NULL;
151150
libcrun_container *ctr;
152151
struct libcrun_context_s *ctx;
152+
int ret;
153153

154154
if (!PyArg_ParseTuple (args, "OO", &ctx_obj, &ctr_obj))
155155
return NULL;
@@ -162,7 +162,10 @@ container_run (PyObject *self, PyObject *args)
162162
if (ctr == NULL)
163163
return NULL;
164164

165-
if (libcrun_container_run (ctx, ctr, LIBCRUN_RUN_OPTIONS_PREFORK, &err) < 0)
165+
Py_BEGIN_ALLOW_THREADS;
166+
ret = libcrun_container_run (ctx, ctr, LIBCRUN_RUN_OPTIONS_PREFORK, &err);
167+
Py_END_ALLOW_THREADS;
168+
if (ret < 0)
166169
return set_error (&err);
167170

168171
Py_RETURN_NONE;
@@ -176,6 +179,7 @@ container_create (PyObject *self, PyObject *args)
176179
PyObject *ctr_obj = NULL;
177180
libcrun_container *ctr;
178181
struct libcrun_context_s *ctx;
182+
int ret;
179183

180184
if (!PyArg_ParseTuple (args, "OO", &ctx_obj, &ctr_obj))
181185
return NULL;
@@ -188,7 +192,10 @@ container_create (PyObject *self, PyObject *args)
188192
if (ctr == NULL)
189193
return NULL;
190194

191-
if (libcrun_container_create (ctx, ctr, &err) < 0)
195+
Py_BEGIN_ALLOW_THREADS;
196+
ret = libcrun_container_create (ctx, ctr, &err);
197+
Py_END_ALLOW_THREADS;
198+
if (ret < 0)
192199
return set_error (&err);
193200

194201
Py_RETURN_NONE;
@@ -202,6 +209,7 @@ container_delete (PyObject *self, PyObject *args)
202209
char *id = NULL;
203210
bool force;
204211
struct libcrun_context_s *ctx;
212+
int ret;
205213

206214
if (!PyArg_ParseTuple (args, "Osn", &ctx_obj, &id, &force))
207215
return NULL;
@@ -210,7 +218,10 @@ container_delete (PyObject *self, PyObject *args)
210218
if (ctx == NULL)
211219
return NULL;
212220

213-
if (libcrun_container_delete (ctx, NULL, id, force, &err) < 0)
221+
Py_BEGIN_ALLOW_THREADS;
222+
ret = libcrun_container_delete (ctx, NULL, id, force, &err);
223+
Py_END_ALLOW_THREADS;
224+
if (ret < 0)
214225
return set_error (&err);
215226

216227
Py_RETURN_NONE;
@@ -224,6 +235,7 @@ container_kill (PyObject *self, PyObject *args)
224235
char *id = NULL;
225236
int signal;
226237
struct libcrun_context_s *ctx;
238+
int ret;
227239

228240
if (!PyArg_ParseTuple (args, "Osi", &ctx_obj, &id, &signal))
229241
return NULL;
@@ -232,7 +244,10 @@ container_kill (PyObject *self, PyObject *args)
232244
if (ctx == NULL)
233245
return NULL;
234246

235-
if (libcrun_container_kill (ctx, id, signal, &err) < 0)
247+
Py_BEGIN_ALLOW_THREADS;
248+
ret = libcrun_container_kill (ctx, id, signal, &err);
249+
Py_END_ALLOW_THREADS;
250+
if (ret < 0)
236251
return set_error (&err);
237252

238253
Py_RETURN_NONE;
@@ -245,6 +260,7 @@ container_start (PyObject *self, PyObject *args)
245260
PyObject *ctx_obj = NULL;
246261
char *id = NULL;
247262
struct libcrun_context_s *ctx;
263+
int ret;
248264

249265
if (!PyArg_ParseTuple (args, "Os", &ctx_obj, &id))
250266
return NULL;
@@ -253,7 +269,10 @@ container_start (PyObject *self, PyObject *args)
253269
if (ctx == NULL)
254270
return NULL;
255271

256-
if (libcrun_container_start (ctx, id, &err) < 0)
272+
Py_BEGIN_ALLOW_THREADS;
273+
ret = libcrun_container_start (ctx, id, &err);
274+
Py_END_ALLOW_THREADS;
275+
if (ret < 0)
257276
return set_error (&err);
258277

259278
Py_RETURN_NONE;
@@ -266,8 +285,9 @@ containers_list (PyObject *self, PyObject *args)
266285
PyObject *ctx_obj = NULL;
267286
struct libcrun_context_s *ctx;
268287
libcrun_container_list_t *containers, *it;
269-
PyObject *ret;
288+
PyObject *retobj;
270289
Py_ssize_t i = 0;
290+
int ret;
271291

272292
if (!PyArg_ParseTuple (args, "O", &ctx_obj))
273293
return NULL;
@@ -276,23 +296,27 @@ containers_list (PyObject *self, PyObject *args)
276296
if (ctx == NULL)
277297
return NULL;
278298

279-
if (libcrun_get_containers_list (&containers, ctx->state_root, &err) < 0)
299+
Py_BEGIN_ALLOW_THREADS;
300+
ret = libcrun_get_containers_list (&containers, ctx->state_root, &err);
301+
Py_END_ALLOW_THREADS;
302+
if (ret < 0)
280303
return set_error (&err);
281304

282305
i = 0;
283306
for (it = containers; it; it = it->next)
284307
i++;
285-
ret = PyList_New (i);
286-
if (ret == NULL)
308+
309+
retobj = PyList_New (i);
310+
if (retobj == NULL)
287311
return NULL;
288312

289313
i = 0;
290314
for (it = containers; it; it = it->next)
291-
PyList_SetItem (ret, i++, PyUnicode_FromString (it->name));
315+
PyList_SetItem (retobj, i++, PyUnicode_FromString (it->name));
292316

293317
libcrun_free_containers_list (containers);
294318

295-
return ret;
319+
return retobj;
296320
}
297321

298322
static PyObject *
@@ -305,6 +329,7 @@ container_status (PyObject *self, PyObject *args)
305329
libcrun_container_status_t status;
306330
cleanup_free char *buffer = NULL;
307331
FILE *memfile;
332+
int ret;
308333

309334
if (!PyArg_ParseTuple (args, "Os", &ctx_obj, &id))
310335
return NULL;
@@ -322,8 +347,12 @@ container_status (PyObject *self, PyObject *args)
322347
memset (buffer, 0, 4096);
323348

324349
memfile = fmemopen (buffer, 4095, "w");
325-
if (libcrun_container_state (ctx, id, memfile, &err) < 0)
350+
Py_BEGIN_ALLOW_THREADS;
351+
ret = libcrun_container_state (ctx, id, memfile, &err);
352+
Py_END_ALLOW_THREADS;
353+
if (ret < 0)
326354
return set_error (&err);
355+
327356
fclose (memfile);
328357

329358
return PyUnicode_FromString (buffer);
@@ -367,7 +396,10 @@ container_update (PyObject *self, PyObject *args)
367396
return NULL;
368397
}
369398

399+
Py_BEGIN_ALLOW_THREADS;
370400
ret = libcrun_container_exec (ctx, id, process, &err);
401+
Py_END_ALLOW_THREADS;
402+
371403
free_oci_container_process (process);
372404
if (ret < 0)
373405
return set_error (&err);
@@ -391,7 +423,9 @@ container_spec (PyObject *self, PyObject *args)
391423
return NULL;
392424

393425
memfile = fmemopen (buffer, 4095, "w");
426+
Py_BEGIN_ALLOW_THREADS;
394427
ret = libcrun_container_spec (geteuid () == 0, memfile, &err);
428+
Py_END_ALLOW_THREADS;
395429
if (ret < 0)
396430
return set_error (&err);
397431
buffer[ret] = '\0';

0 commit comments

Comments
 (0)