nim-lang / threading

New atomics, thread primitives, atomic refcounting for --gc:arc/orc.
MIT License
73 stars 12 forks source link

Channels deadlocks with TSan #69

Closed planetis-m closed 3 months ago

planetis-m commented 3 months ago

I am running the non-blocking example with nim c --cc:clang --mm:arc --threads:on --tlsEmulation:off -t:"-fsanitize=thread" -l:"-fsanitize=thread" -d:release -d:nosignalhandler -g it always deadlocks.

planetis-m commented 3 months ago
==================
WARNING: ThreadSanitizer: data race (pid=3169)
  Write of size 8 at 0x722c00000090 by thread T1 (mutexes: write M0):
    #0 channels::channelSend(ptr<channels::ChannelObj>, pointer, int, static<bool>) /home/antonisg/Code/threading/threading/channels.nim:192:130 (test_chan+0x105b22) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 channels::send(Chan<string>, sink<Isolated<string>>) /home/antonisg/Code/threading/threading/channels.nim:288:27 (test_chan+0x105f2f) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 test_chan::slowWorker(range09223372036854775807) /home/antonisg/Code/threading/threading/test_chan.nim:14:2 (test_chan+0x105f2f)
    #3 system::threadProcWrapDispatch(ptr<Thread<range09223372036854775807>>) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:69:2 (test_chan+0x105044) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #4 system::threadProcWrapStackFrame(ptr<Thread<range09223372036854775807>>) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:95:2 (test_chan+0x105044)
    #5 typedthreads::threadProcWrapper(pointer) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:101:2 (test_chan+0x101b73) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)

  Previous read of size 8 at 0x722c00000090 by main thread:
    #0 channels::channelReceive(ptr<channels::ChannelObj>, pointer, int, static<bool>) /home/antonisg/Code/threading/threading/channels.nim:205:22 (test_chan+0x105bea) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 channels::tryRecv(Chan<string>, var<string>) /home/antonisg/Code/threading/threading/channels.nim:281:11 (test_chan+0x10606c) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 test_chan::main /home/antonisg/Code/threading/threading/test_chan.nim:28:78 (test_chan+0x10606c)
    #3 NimMainModule /home/antonisg/Code/threading/threading/test_chan.nim:46:2 (test_chan+0x1064e8) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #4 NimMainInner /home/antonisg/Code/threading/threading/test_chan.nim:40:2 (test_chan+0x1064e8)
    #5 NimMain /home/antonisg/Code/threading/threading/test_chan.nim:51:2 (test_chan+0x1064e8)
    #6 main /home/antonisg/Code/threading/threading/test_chan.nim:59:2 (test_chan+0x1064e8)

  As if synchronized via sleep:
    #0 nanosleep <null> (test_chan+0x81272) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 nossleep /home/antonisg/Build/Nim/lib/pure/os.nim:704:22 (test_chan+0x1056b8) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 test_chan::slowWorker(range09223372036854775807) /home/antonisg/Code/threading/threading/test_chan.nim:13:2 (test_chan+0x105eb9) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #3 system::threadProcWrapDispatch(ptr<Thread<range09223372036854775807>>) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:69:2 (test_chan+0x105044) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #4 system::threadProcWrapStackFrame(ptr<Thread<range09223372036854775807>>) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:95:2 (test_chan+0x105044)
    #5 typedthreads::threadProcWrapper(pointer) /home/antonisg/Build/Nim/lib/system/threadimpl.nim:101:2 (test_chan+0x101b73) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)

  Location is heap block of size 176 at 0x722c00000000 allocated by main thread:
    #0 malloc <null> (test_chan+0x763ee) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 channels::allocChannel(int, int) /home/antonisg/Code/threading/threading/channels.nim:134:24 (test_chan+0x105871) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 channels::newChan(range19223372036854775807) /home/antonisg/Code/threading/threading/channels.nim:316:13 (test_chan+0x105871)
    #3 NimMainModule /home/antonisg/Code/threading/threading/test_chan.nim:7:26 (test_chan+0x1064cd) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #4 NimMainInner /home/antonisg/Code/threading/threading/test_chan.nim:40:2 (test_chan+0x1064cd)
    #5 NimMain /home/antonisg/Code/threading/threading/test_chan.nim:51:2 (test_chan+0x1064cd)
    #6 main /home/antonisg/Code/threading/threading/test_chan.nim:59:2 (test_chan+0x1064cd)

  Mutex M0 (0x722c00000000) created at:
    #0 pthread_mutex_init <null> (test_chan+0x7880f) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 locks::initLock(var<syslocks::SysLockObj>) /home/antonisg/Build/Nim/lib/core/locks.nim:38:2 (test_chan+0x1058bd) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 channels::allocChannel(int, int) /home/antonisg/Code/threading/threading/channels.nim:139:2 (test_chan+0x1058bd)
    #3 channels::newChan(range19223372036854775807) /home/antonisg/Code/threading/threading/channels.nim:316:13 (test_chan+0x1058bd)
    #4 NimMainModule /home/antonisg/Code/threading/threading/test_chan.nim:7:26 (test_chan+0x1064cd) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #5 NimMainInner /home/antonisg/Code/threading/threading/test_chan.nim:40:2 (test_chan+0x1064cd)
    #6 NimMain /home/antonisg/Code/threading/threading/test_chan.nim:51:2 (test_chan+0x1064cd)
    #7 main /home/antonisg/Code/threading/threading/test_chan.nim:59:2 (test_chan+0x1064cd)

  Thread T1 (tid=3171, running) created by main thread at:
    #0 pthread_create <null> (test_chan+0x77b5b) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #1 typedthreads::createThread(var<Thread<range09223372036854775807>>, proc<range09223372036854775807>, range09223372036854775807) /home/antonisg/Build/Nim/lib/std/typedthreads.nim:286:106 (test_chan+0x101c6d) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #2 test_chan::main /home/antonisg/Code/threading/threading/test_chan.nim:19:2 (test_chan+0x106011) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #3 NimMainModule /home/antonisg/Code/threading/threading/test_chan.nim:46:2 (test_chan+0x1064e8) (BuildId: 2d5c85dd3dec47f560cba3a2aff690c3a16e11a5)
    #4 NimMainInner /home/antonisg/Code/threading/threading/test_chan.nim:40:2 (test_chan+0x1064e8)
    #5 NimMain /home/antonisg/Code/threading/threading/test_chan.nim:51:2 (test_chan+0x1064e8)
    #6 main /home/antonisg/Code/threading/threading/test_chan.nim:59:2 (test_chan+0x1064e8)

SUMMARY: ThreadSanitizer: data race /home/antonisg/Code/threading/threading/channels.nim:192:130 in channels::channelSend(ptr<channels::ChannelObj>, pointer, int, static<bool>)
==================
planetis-m commented 3 months ago

Shit my bad, I forgot atlas changes the dependencies to the last available tag instead of master