Skip to content

TSan: data race accessing socket fd #116912

Description

@pitrou

Bug report

Bug description:

The sock_fd member in PySocketSockObject can be mutated while the socket is being used, for example when calling socket.close or socket.detach. However, some other threads can access that member without holding the GIL, and without using atomic instructions. This can trip up Thread Sanitizer in test_asyncio, for example:

0:00:31 load avg: 3.29 [15/52/1] test_asyncio.test_sock_lowlevel worker non-zero exit code (Exit code 66)
==================
WARNING: ThreadSanitizer: data race (pid=7227)
  Write of size 4 at 0x7fd903611570 by thread T19:
    #0 sock_close /home/runner/work/cpython/cpython/./Modules/socketmodule.c:3339:20 (_socket.cpython-313d-x86_64-linux-gnu.so+0xdbde) (BuildId: 7a12da96172d453cd023792c027755908db49f4e)
[...]

  Previous read of size 4 at 0x7fd903611570 by main thread:
    #0 sock_sendto_impl /home/runner/work/cpython/cpython/./Modules/socketmodule.c:4474:29 (_socket.cpython-313d-x86_64-linux-gnu.so+0x131e5) (BuildId: 7a12da96172d453cd023792c027755908db49f4e)
    #1 sock_call_ex /home/runner/work/cpython/cpython/./Modules/socketmodule.c:968:19 (_socket.cpython-313d-x86_64-linux-gnu.so+0x10fa1) (BuildId: 7a12da96172d453cd023792c027755908db49f4e)
    #2 sock_call /home/runner/work/cpython/cpython/./Modules/socketmodule.c:1020:12 (_socket.cpython-313d-x86_64-linux-gnu.so+0xf1a1) (BuildId: 7a12da96172d453cd023792c027755908db49f4e)
    #3 sock_sendto /home/runner/work/cpython/cpython/./Modules/socketmodule.c:4532:9 (_socket.cpython-313d-x86_64-linux-gnu.so+0xf1a1)
[...]

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    testsTests in the Lib/test dir
    on Mar 16, 2024
  2. changed the title [-]TSan: data race access socket fd[/-] [+]TSan: data race accessing socket fd[/+] on Mar 16, 2024
  3. pitrou commented on Mar 16, 2024

    @pitrou
    MemberAuthor
  4. deleted a comment from Davidzcdn on Jul 10, 2024
  5. deleted a comment from Davidzcdn on Jul 10, 2024
  6. corona10 commented on Oct 3, 2024

    @corona10
    Member
  7. corona10 commented on Oct 3, 2024

    @corona10
    Member
     ==================
    WARNING: ThreadSanitizer: data race (pid=11811)
      Write of size 4 at 0x7f788e470220 by main thread:
        #0 _socket_socket_close_impl /home/runner/work/cpython/cpython/./Modules/socketmodule.c:3369:20 (_socket.cpython-314td-x86_64-linux-gnu.so+0xe6fa) (BuildId: fd621939d2f906d64ec9d9113f22b21082c40173)
        #1 _socket_socket_close /home/runner/work/cpython/cpython/./Modules/clinic/socketmodule.c.h:32:20 (_socket.cpython-314td-x86_64-linux-gnu.so+0xe6fa)
        #2 method_vectorcall_NOARGS /home/runner/work/cpython/cpython/Objects/descrobject.c:447:24 (python+0x27d040) (BuildId: f4b70dfbd84ba1a7db1e594342dc691a3ebbd7b7)
        #3 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:167:11 (python+0x261c31) (BuildId: f4b70dfbd84ba1a7db1e594342dc691a3ebbd7b7)
        #4 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x263730) (BuildId: f4b70dfbd84ba1a7db1e594342dc691a3ebbd7b7)
    
  8. kumaraditya303 commented on Jan 1, 2025

    @kumaraditya303
    Contributor

    Superseded by #128277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions