Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-122581: Avoid data races when collecting parser statistics #122694

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Include/internal/pycore_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ extern "C" {
struct _parser_runtime_state {
#ifdef Py_DEBUG
long memo_statistics[_PYPEGEN_NSTATISTICS];
#ifdef Py_GIL_DISABLED
PyMutex mutex;
#endif
#else
int _not_used;
#endif
struct _expr dummy_name;
};

_Py_DECLARE_STR(empty, "")
#if defined(Py_DEBUG) && defined(Py_GIL_DISABLED)
#define _parser_runtime_state_INIT \
{ \
.mutex = {0}, \
.dummy_name = { \
.kind = Name_kind, \
.v.Name.id = &_Py_STR(empty), \
Expand All @@ -40,6 +45,20 @@ _Py_DECLARE_STR(empty, "")
.end_col_offset = 0, \
}, \
}
#else
#define _parser_runtime_state_INIT \
{ \
.dummy_name = { \
.kind = Name_kind, \
.v.Name.id = &_Py_STR(empty), \
.v.Name.ctx = Load, \
.lineno = 1, \
.col_offset = 0, \
.end_lineno = 1, \
.end_col_offset = 0, \
}, \
}
#endif

extern struct _mod* _PyParser_ASTFromString(
const char *str,
Expand Down
17 changes: 17 additions & 0 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,22 @@ _PyPegen_fill_token(Parser *p)
#define NSTATISTICS _PYPEGEN_NSTATISTICS
#define memo_statistics _PyRuntime.parser.memo_statistics

#ifdef Py_GIL_DISABLED
#define MUTEX_LOCK() PyMutex_Lock(&_PyRuntime.parser.mutex)
#define MUTEX_UNLOCK() PyMutex_Unlock(&_PyRuntime.parser.mutex)
#else
#define MUTEX_LOCK()
#define MUTEX_UNLOCK()
#endif

void
_PyPegen_clear_memo_statistics(void)
{
MUTEX_LOCK();
for (int i = 0; i < NSTATISTICS; i++) {
memo_statistics[i] = 0;
}
MUTEX_UNLOCK();
}

PyObject *
Expand All @@ -311,18 +321,23 @@ _PyPegen_get_memo_statistics(void)
if (ret == NULL) {
return NULL;
}

MUTEX_LOCK();
for (int i = 0; i < NSTATISTICS; i++) {
PyObject *value = PyLong_FromLong(memo_statistics[i]);
if (value == NULL) {
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
MUTEX_UNLOCK();
Py_DECREF(ret);
return NULL;
}
// PyList_SetItem borrows a reference to value.
if (PyList_SetItem(ret, i, value) < 0) {
MUTEX_UNLOCK();
Py_DECREF(ret);
return NULL;
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
}
}
MUTEX_UNLOCK();
return ret;
}
#endif
Expand All @@ -348,7 +363,9 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
if (count <= 0) {
count = 1;
}
MUTEX_LOCK();
memo_statistics[type] += count;
MUTEX_UNLOCK();
}
#endif
p->mark = m->mark;
Expand Down
Loading