gh-116616: Use relaxed atomic ops to access socket module defaulttimeout (#116623)

Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
Erlend E. Aasland 2024-03-12 14:44:39 +01:00 committed by GitHub
parent 02918aa961
commit 3b7fe117fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 5 deletions

View File

@ -1054,8 +1054,8 @@ init_sockobject(socket_state *state, PySocketSockObject *s,
else else
#endif #endif
{ {
s->sock_timeout = state->defaulttimeout; s->sock_timeout = _Py_atomic_load_int64_relaxed(&state->defaulttimeout);
if (state->defaulttimeout >= 0) { if (s->sock_timeout >= 0) {
if (internal_setblocking(s, 0) == -1) { if (internal_setblocking(s, 0) == -1) {
return -1; return -1;
} }
@ -6913,11 +6913,12 @@ static PyObject *
socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored)) socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored))
{ {
socket_state *state = get_module_state(self); 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; Py_RETURN_NONE;
} }
else { else {
double seconds = PyTime_AsSecondsDouble(state->defaulttimeout); double seconds = PyTime_AsSecondsDouble(timeout);
return PyFloat_FromDouble(seconds); return PyFloat_FromDouble(seconds);
} }
} }
@ -6938,7 +6939,7 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg)
return NULL; return NULL;
socket_state *state = get_module_state(self); socket_state *state = get_module_state(self);
state->defaulttimeout = timeout; _Py_atomic_store_int64_relaxed(&state->defaulttimeout, timeout);
Py_RETURN_NONE; Py_RETURN_NONE;
} }