Skip to content

Commit af1f063

Browse files
committed
Switch to _PyRecursiveMutex from PyMutex
It seems we do need to worry about re-entrancy, so switch to using a recursive mutex. Note we can't use a critical section since we need to perform blocking IO while holding the lock, which critical section would implicitly release.
1 parent 4aad8fd commit af1f063

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

Modules/posixmodule.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15582,7 +15582,7 @@ typedef struct {
1558215582
ino_t d_ino;
1558315583
int dir_fd;
1558415584
#endif
15585-
PyMutex mutex;
15585+
_PyRecursiveMutex mutex;
1558615586
} DirEntry;
1558715587

1558815588
#define DirEntry_CAST(op) ((DirEntry *)(op))
@@ -15592,12 +15592,12 @@ DirEntry_dealloc(PyObject *op)
1559215592
{
1559315593
DirEntry *entry = DirEntry_CAST(op);
1559415594
PyTypeObject *tp = Py_TYPE(entry);
15595-
PyMutex_Lock(&entry->mutex);
15595+
_PyRecursiveMutex_Lock(&entry->mutex);
1559615596
Py_XDECREF(entry->name);
1559715597
Py_XDECREF(entry->path);
1559815598
Py_XDECREF(entry->stat);
1559915599
Py_XDECREF(entry->lstat);
15600-
PyMutex_Unlock(&entry->mutex);
15600+
_PyRecursiveMutex_Unlock(&entry->mutex);
1560115601
freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
1560215602
free_func(entry);
1560315603
Py_DECREF(tp);
@@ -15743,17 +15743,17 @@ os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
1574315743
/*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
1574415744
{
1574515745
if (!follow_symlinks) {
15746-
PyMutex_Lock(&self->mutex);
15746+
_PyRecursiveMutex_Lock(&self->mutex);
1574715747
PyObject *stat = DirEntry_get_lstat(defining_class, self);
15748-
PyMutex_Unlock(&self->mutex);
15748+
_PyRecursiveMutex_Unlock(&self->mutex);
1574915749
return stat;
1575015750
}
1575115751

15752-
PyMutex_Lock(&self->mutex);
15752+
_PyRecursiveMutex_Lock(&self->mutex);
1575315753
if (!self->stat) {
1575415754
int result = os_DirEntry_is_symlink_impl(self, defining_class);
1575515755
if (result == -1) {
15756-
PyMutex_Unlock(&self->mutex);
15756+
_PyRecursiveMutex_Unlock(&self->mutex);
1575715757
return NULL;
1575815758
}
1575915759
if (result) {
@@ -15764,7 +15764,7 @@ os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
1576415764
self->stat = DirEntry_get_lstat(defining_class, self);
1576515765
}
1576615766
}
15767-
PyMutex_Unlock(&self->mutex);
15767+
_PyRecursiveMutex_Unlock(&self->mutex);
1576815768

1576915769
return Py_XNewRef(self->stat);
1577015770
}
@@ -16039,7 +16039,7 @@ DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
1603916039
entry->stat = NULL;
1604016040
entry->lstat = NULL;
1604116041
entry->got_file_index = 0;
16042-
entry->mutex = (PyMutex){0};
16042+
entry->mutex = (_PyRecursiveMutex){0};
1604316043

1604416044
entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
1604516045
if (!entry->name)
@@ -16132,7 +16132,7 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
1613216132
entry->path = NULL;
1613316133
entry->stat = NULL;
1613416134
entry->lstat = NULL;
16135-
entry->mutex = (PyMutex){0};
16135+
entry->mutex = (_PyRecursiveMutex){0};
1613616136

1613716137
if (path->fd != -1) {
1613816138
entry->dir_fd = path->fd;
@@ -16193,7 +16193,7 @@ typedef struct {
1619316193
#ifdef HAVE_FDOPENDIR
1619416194
int fd;
1619516195
#endif
16196-
PyMutex mutex;
16196+
_PyRecursiveMutex mutex;
1619716197
} ScandirIterator;
1619816198

1619916199
#define ScandirIterator_CAST(op) ((ScandirIterator *)(op))
@@ -16209,10 +16209,10 @@ ScandirIterator_is_closed(ScandirIterator *iterator)
1620916209
static void
1621016210
ScandirIterator_closedir(ScandirIterator *iterator)
1621116211
{
16212-
PyMutex_Lock(&iterator->mutex);
16212+
_PyRecursiveMutex_Lock(&iterator->mutex);
1621316213
HANDLE handle = iterator->handle;
1621416214
iterator->handle = INVALID_HANDLE_VALUE;
16215-
PyMutex_Unlock(&iterator->mutex);
16215+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1621616216

1621716217
if (handle == INVALID_HANDLE_VALUE) {
1621816218
return;
@@ -16231,7 +16231,7 @@ ScandirIterator_iternext(PyObject *op)
1623116231
BOOL success;
1623216232
PyObject *entry;
1623316233

16234-
PyMutex_Lock(&iterator->mutex);
16234+
_PyRecursiveMutex_Lock(&iterator->mutex);
1623516235
while (iterator->handle != INVALID_HANDLE_VALUE) {
1623616236
if (!iterator->first_time) {
1623716237
Py_BEGIN_ALLOW_THREADS
@@ -16254,15 +16254,15 @@ ScandirIterator_iternext(PyObject *op)
1625416254
entry = DirEntry_from_find_data(module, &iterator->path, file_data);
1625516255
if (!entry)
1625616256
break;
16257-
PyMutex_Unlock(&iterator->mutex);
16257+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1625816258
return entry;
1625916259
}
1626016260

1626116261
/* Loop till we get a non-dot directory or finish iterating */
1626216262
}
1626316263

1626416264
/* Already closed, error, or no more files */
16265-
PyMutex_Unlock(&iterator->mutex);
16265+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1626616266
ScandirIterator_closedir(iterator);
1626716267
return NULL;
1626816268
}
@@ -16278,10 +16278,10 @@ ScandirIterator_is_closed(ScandirIterator *iterator)
1627816278
static void
1627916279
ScandirIterator_closedir(ScandirIterator *iterator)
1628016280
{
16281-
PyMutex_Lock(&iterator->mutex);
16281+
_PyRecursiveMutex_Lock(&iterator->mutex);
1628216282
DIR *dirp = iterator->dirp;
1628316283
iterator->dirp = NULL;
16284-
PyMutex_Unlock(&iterator->mutex);
16284+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1628516285

1628616286
if (!dirp) {
1628716287
return;
@@ -16306,7 +16306,7 @@ ScandirIterator_iternext(PyObject *op)
1630616306
int is_dot;
1630716307
PyObject *entry;
1630816308

16309-
PyMutex_Lock(&iterator->mutex);
16309+
_PyRecursiveMutex_Lock(&iterator->mutex);
1631016310
while (iterator->dirp) {
1631116311
errno = 0;
1631216312
Py_BEGIN_ALLOW_THREADS
@@ -16335,15 +16335,15 @@ ScandirIterator_iternext(PyObject *op)
1633516335
);
1633616336
if (!entry)
1633716337
break;
16338-
PyMutex_Unlock(&iterator->mutex);
16338+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1633916339
return entry;
1634016340
}
1634116341

1634216342
/* Loop till we get a non-dot directory or finish iterating */
1634316343
}
1634416344

1634516345
/* Already closed, error, or no more files */
16346-
PyMutex_Unlock(&iterator->mutex);
16346+
_PyRecursiveMutex_Unlock(&iterator->mutex);
1634716347
ScandirIterator_closedir(iterator);
1634816348
return NULL;
1634916349
}
@@ -16476,7 +16476,7 @@ os_scandir_impl(PyObject *module, path_t *path)
1647616476
if (!iterator)
1647716477
return NULL;
1647816478

16479-
iterator->mutex = (PyMutex){0};
16479+
iterator->mutex = (_PyRecursiveMutex){0};
1648016480
#ifdef MS_WINDOWS
1648116481
iterator->handle = INVALID_HANDLE_VALUE;
1648216482
#else

0 commit comments

Comments
 (0)