Skip to content

Socket file descriptor races in GIL-enabled build #132886

Closed
@colesbury

Description

@colesbury

Bug report

In the free threading build, but not the default GIL-enabled build, the reads and writes sock_fd use relaxed atomics.

This can lead to data races because the sock_fd field is read when the GIL is released.

static inline void
set_sock_fd(PySocketSockObject *s, SOCKET_T fd)
{
#ifdef Py_GIL_DISABLED
#if SIZEOF_SOCKET_T == SIZEOF_INT
_Py_atomic_store_int_relaxed((int *)&s->sock_fd, (int)fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG
_Py_atomic_store_long_relaxed((long *)&s->sock_fd, (long)fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG_LONG
_Py_atomic_store_llong_relaxed((long long *)&s->sock_fd, (long long)fd);
#else
#error "Unsupported SIZEOF_SOCKET_T"
#endif
#else
s->sock_fd = fd;
#endif
}

https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174

Thread sanitizer:

WARNING: ThreadSanitizer: data race (pid=10596)
  Read of size 4 at 0x7feeb9bc4470 by thread T32:
    #0 get_sock_fd /home/runner/work/cpython/cpython/./Modules/socketmodule.c:603:15 (_socket.cpython-314d-x86_64-linux-gnu.so+0x147a2) (BuildId: 5fd9421785689189a09a4eaff953e770708561c7)
    #1 sock_send_impl /home/runner/work/cpython/cpython/./Modules/socketmodule.c:4568:24 (_socket.cpython-314d-x86_64-linux-gnu.so+0x147a2)
    #2 sock_call_ex /home/runner/work/cpython/cpython/./Modules/socketmodule.c:10[13](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:14):19 (_socket.cpython-314d-x86_64-linux-gnu.so+0x120e0) (BuildId: 5fd9421785689189a09a4eaff953e770708561c7)
    #3 sock_call /home/runner/work/cpython/cpython/./Modules/socketmodule.c:1065:12 (_socket.cpython-3[14](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:15)d-x86_64-linux-gnu.so+0x120e0)
    #4 sock_send /home/runner/work/cpython/cpython/./Modules/socketmodule.c:4594:9 (_socket.cpython-314d-x86_64-linux-gnu.so+0xf18c) (BuildId: 5fd9421785689189a09a4eaff953e770708561c7)
    #5 method_vectorcall_VARARGS /home/runner/work/cpython/cpython/Objects/descrobject.c:325:24 (python+0x265e8f) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #6 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x24db5b) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #7 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x24f340) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #8 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:3886:35 (python+0x47f974) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #9 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #10 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #11 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #12 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x2550cb) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #13 method_vectorcall /home/runner/work/cpython/cpython/Objects/classobject.c:94:18 (python+0x2537c1) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #14 _PyVectorcall_Call /home/runner/work/cpython/cpython/Objects/call.c:273:16 (python+0x24f247) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #[15](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:16) _PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:348:16 (python+0x24f458) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #[16](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:17) PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:373:12 (python+0x24f657) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #17 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:2475:32 (python+0x477cac) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #18 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #19 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #20 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #21 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x2550cb) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #22 method_vectorcall /home/runner/work/cpython/cpython/Objects/classobject.c:94:18 (python+0x2537c1) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #23 _PyVectorcall_Call /home/runner/work/cpython/cpython/Objects/call.c:273:16 (python+0x24f247) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #24 _PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:348:16 (python+0x24f458) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #25 PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:373:12 (python+0x24f657) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #26 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:2475:32 (python+0x477cac) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #27 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #28 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #29 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #30 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x2550cb) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #31 method_vectorcall /home/runner/work/cpython/cpython/Objects/classobject.c:72:20 (python+0x253745) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #32 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x4e42d9) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #33 context_run /home/runner/work/cpython/cpython/Python/context.c:728:29 (python+0x4e42d9)
    #34 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:3551:35 (python+0x47dd02) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #35 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #40 call_method /home/runner/work/cpython/cpython/Objects/typeobject.c:3006:19 (python+0x362dc3) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #41 slot_tp_call /home/runner/work/cpython/cpython/Objects/typeobject.c:10268:12 (python+0x362dc3)
    #42 _PyObject_MakeTpCall /home/runner/work/cpython/cpython/Objects/call.c:242:18 (python+0x24e2ba) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #43 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:167:16 (python+0x24dbbd) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #44 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x24f340) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #45 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:3886:35 (python+0x47f974) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #46 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #47 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #48 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #49 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x2550cb) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #50 method_vectorcall /home/runner/work/cpython/cpython/Objects/classobject.c:94:18 (python+0x2537c1) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #51 _PyVectorcall_Call /home/runner/work/cpython/cpython/Objects/call.c:273:16 (python+0x24f247) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #52 _PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:348:16 (python+0x24f458) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #53 PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:373:12 (python+0x24f657) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #54 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:2475:32 (python+0x477cac) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #55 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #56 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #57 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #58 _PyObject_VectorcallDictTstate /home/runner/work/cpython/cpython/Objects/call.c:135:15 (python+0x24de89) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #59 _PyObject_Call_Prepend /home/runner/work/cpython/cpython/Objects/call.c:504:24 (python+0x24fc4d) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #60 call_method /home/runner/work/cpython/cpython/Objects/typeobject.c:3006:19 (python+0x362dc3) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #61 slot_tp_call /home/runner/work/cpython/cpython/Objects/typeobject.c:10268:12 (python+0x362dc3)
    #62 _PyObject_MakeTpCall /home/runner/work/cpython/cpython/Objects/call.c:242:18 (python+0x24e2ba) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #63 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:167:16 (python+0x24dbbd) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #64 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x24f340) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #65 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:3886:35 (python+0x47f974) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #66 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #67 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #68 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #69 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x2550cb) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #70 method_vectorcall /home/runner/work/cpython/cpython/Objects/classobject.c:94:18 (python+0x2537c1) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #71 _PyVectorcall_Call /home/runner/work/cpython/cpython/Objects/call.c:273:16 (python+0x24f247) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #72 _PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:348:16 (python+0x24f458) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #73 PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:373:12 (python+0x24f657) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #74 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:2475:32 (python+0x477cac) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #75 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #76 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #77 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #78 _PyObject_VectorcallDictTstate /home/runner/work/cpython/cpython/Objects/call.c:135:15 (python+0x24de89) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #79 _PyObject_Call_Prepend /home/runner/work/cpython/cpython/Objects/call.c:504:24 (python+0x24fc4d) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #80 call_method /home/runner/work/cpython/cpython/Objects/typeobject.c:3006:19 (python+0x362dc3) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #81 slot_tp_call /home/runner/work/cpython/cpython/Objects/typeobject.c:10268:12 (python+0x362dc3)
    #82 _PyObject_MakeTpCall /home/runner/work/cpython/cpython/Objects/call.c:242:18 (python+0x24e2ba) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #83 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:167:16 (python+0x24dbbd) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #84 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x24f340) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #85 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:1451:35 (python+0x47265b) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #86 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #87 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #88 PyEval_EvalCode /home/runner/work/cpython/cpython/Python/ceval.c:870:21 (python+0x46a727) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #89 builtin_exec_impl /home/runner/work/cpython/cpython/Python/bltinmodule.c:1158:[17](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:18) (python+0x463291) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #90 builtin_exec /home/runner/work/cpython/cpython/Python/clinic/bltinmodule.c.h:568:20 (python+0x463291)
    #91 cfunction_vectorcall_FASTCALL_KEYWORDS /home/runner/work/cpython/cpython/Objects/methodobject.c:470:24 (python+0x2ef6c7) (BuildId: aaa34bd06c4565b8343ea0751a4b01[18](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:19)212c23b3)
    #92 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:169:11 (python+0x24db5b) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #93 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x24f340) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #94 _PyEval_EvalFrameDefault /home/runner/work/cpython/cpython/Python/generated_cases.c.h:1451:35 (python+0x47265b) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #95 _PyEval_EvalFrame /home/runner/work/cpython/cpython/./Include/internal/pycore_ceval.h:1[19](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:20):16 (python+0x46ab79) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #96 _PyEval_Vector /home/runner/work/cpython/cpython/Python/ceval.c:1967:12 (python+0x46ab79)
    #97 _PyFunction_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c (python+0x24f82c) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #98 _PyVectorcall_Call /home/runner/work/cpython/cpython/Objects/call.c:273:16 (python+0x24f247) (BuildId: aaa34bd06c4565b8343ea0751a4b0118[21](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:22)2c23b3)
    #99 _PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:348:16 (python+0x24f458) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #100 PyObject_Call /home/runner/work/cpython/cpython/Objects/call.c:373:12 (python+0x24f657) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #101 pymain_run_module /home/runner/work/cpython/cpython/Modules/main.c:345:14 (python+0x5d44b7) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c[23](https://github.com/python/cpython/actions/runs/14630615998/job/41052077715?pr=131174#step:14:24)b3)
    #102 pymain_run_python /home/runner/work/cpython/cpython/Modules/main.c (python+0x5d3285) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #103 Py_RunMain /home/runner/work/cpython/cpython/Modules/main.c:767:5 (python+0x5d3285)
    #104 pymain_main /home/runner/work/cpython/cpython/Modules/main.c:797:12 (python+0x5d4369) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #105 Py_BytesMain /home/runner/work/cpython/cpython/Modules/main.c:821:12 (python+0x5d43e9) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)
    #106 main /home/runner/work/cpython/cpython/./Programs/python.c:15:12 (python+0x168de0) (BuildId: aaa34bd06c4565b8343ea0751a4b0118212c23b3)

SUMMARY: ThreadSanitizer: data race /home/runner/work/cpython/cpython/./Modules/socketmodule.c:603:15 in get_sock_fd
==================
ThreadSanitizer: reported 1 warnings

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions