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-116616: Use relaxed atomic ops to access socket module defaulttimeout #116623

Merged
merged 1 commit into from Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions Modules/socketmodule.c
Expand Up @@ -1054,8 +1054,8 @@ init_sockobject(socket_state *state, PySocketSockObject *s,
else
#endif
{
s->sock_timeout = state->defaulttimeout;
if (state->defaulttimeout >= 0) {
s->sock_timeout = _Py_atomic_load_int64_relaxed(&state->defaulttimeout);
if (s->sock_timeout >= 0) {
if (internal_setblocking(s, 0) == -1) {
return -1;
}
Expand Down Expand Up @@ -6913,11 +6913,12 @@ static PyObject *
socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored))
{
socket_state *state = get_module_state(self);
if (state->defaulttimeout < 0) {
PyTime_t timeout = _Py_atomic_load_int64_relaxed(&state->defaulttimeout);
if (timeout < 0) {
Py_RETURN_NONE;
}
else {
double seconds = PyTime_AsSecondsDouble(state->defaulttimeout);
double seconds = PyTime_AsSecondsDouble(timeout);
return PyFloat_FromDouble(seconds);
}
}
Expand All @@ -6938,7 +6939,7 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg)
return NULL;

socket_state *state = get_module_state(self);
state->defaulttimeout = timeout;
_Py_atomic_store_int64_relaxed(&state->defaulttimeout, timeout);

Py_RETURN_NONE;
}
Expand Down