Skip to content

Commit 2d39c8c

Browse files
committed
fix: allow module to be imported twice in the same process
1 parent 6beea4b commit 2d39c8c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

coverage/ctracer/module.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99

1010
#define MODULE_DOC PyDoc_STR("Fast coverage tracer.")
1111

12-
static int module_loaded = 0;
12+
static BOOL module_inited = FALSE;
13+
static PyMutex modinit_mutex = {0};
1314

1415
static int
1516
tracer_exec(PyObject *mod)
1617
{
17-
// https://docs.python.org/3/howto/isolating-extensions.html#opt-out-limiting-to-one-module-object-per-process
18-
if (module_loaded) {
19-
PyErr_SetString(PyExc_ImportError,
20-
"cannot load module more than once per process");
21-
return -1;
18+
PyMutex_Lock(&modinit_mutex);
19+
if (module_inited) {
20+
PyMutex_Unlock(&modinit_mutex);
21+
return 0;
2222
}
23-
module_loaded = 1;
2423

2524
if (CTracer_intern_strings() < 0) {
2625
return -1;
@@ -52,6 +51,9 @@ tracer_exec(PyObject *mod)
5251
return -1;
5352
}
5453

54+
module_inited = TRUE;
55+
PyMutex_Unlock(&modinit_mutex);
56+
5557
return 0;
5658
}
5759

0 commit comments

Comments
 (0)