Skip to content
Open
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
24 changes: 21 additions & 3 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2187,11 +2187,15 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name)
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyCriticalSection cs;
PyCriticalSection_Begin(&cs, module);
if (interp->monitoring_tool_names[tool_id] != NULL) {
PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id);
PyCriticalSection_End(&cs);
return NULL;
}
interp->monitoring_tool_names[tool_id] = Py_NewRef(name);
PyCriticalSection_End(&cs);
Comment on lines +2190 to +2198

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be refactored so we just have a single critical section exit

Py_RETURN_NONE;
}

Expand All @@ -2213,11 +2217,15 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id)

PyInterpreterState *interp = _PyInterpreterState_GET();

PyCriticalSection cs;
PyCriticalSection_Begin(&cs, module);
Comment on lines +2220 to +2221

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should prefere Py_BEGIN_CRITICAL_SECTION which gives added benefit of fork safety and is compiled out for the GIL build

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Py_BEGIN_CRITICAL_SECTION is indeed better, though using it also requires restructuring the NULL check so there's a single critical section exit, which makes the diff a bit larger. My original intention was to keep the changes minimal to make the PR easier to review, though I'm not sure which approach is better here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe can use atomics for just a simple read?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, atomic would be enough. But if we use atomic op here, the writes in other places would also need to be atomic, otherwise TSAN will still report a data race. At that point, it feels like we might not even need critical sections. So I'm inclined to pick either critical sections or atomics rather than mixing the two.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need them in sections where we make a write and read to this field?

if (interp->monitoring_tool_names[tool_id] != NULL) {
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
PyCriticalSection_End(&cs);
return NULL;
}
}
PyCriticalSection_End(&cs);

Py_RETURN_NONE;
}
Expand All @@ -2239,13 +2247,20 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
}
PyInterpreterState *interp = _PyInterpreterState_GET();

if (interp->monitoring_tool_names[tool_id] != NULL) {
bool active;
Py_BEGIN_CRITICAL_SECTION(module);
active = (interp->monitoring_tool_names[tool_id] != NULL);
Py_END_CRITICAL_SECTION();

if (active) {
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
return NULL;
}
}

Py_BEGIN_CRITICAL_SECTION(module);
Py_CLEAR(interp->monitoring_tool_names[tool_id]);
Py_END_CRITICAL_SECTION();
Py_RETURN_NONE;
}

Expand All @@ -2267,11 +2282,14 @@ monitoring_get_tool_impl(PyObject *module, int tool_id)
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *name = interp->monitoring_tool_names[tool_id];
PyObject *name;
Py_BEGIN_CRITICAL_SECTION(module);
name = Py_XNewRef(interp->monitoring_tool_names[tool_id]);
Py_END_CRITICAL_SECTION();
if (name == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(name);
return name;
}

/*[clinic input]
Expand Down
Loading