Araq / malebolgia

Malebolgia creates new spawns. Structured concurrency. Thread pools and related APIs.
MIT License
104 stars 10 forks source link

Fixes #4 #5

Closed planetis-m closed 1 year ago

planetis-m commented 1 year ago

The warning was caused because the conditional variable was signaled outside the critical section.

Araq commented 1 year ago

https://stackoverflow.com/questions/60472014/must-lock-be-held-when-signaling-conditional-variable

ZoomRmc commented 1 year ago

If this is indeed the reason, why don't we get the same warning for threading/channels?

planetis-m commented 1 year ago

If this is indeed the reason, why don't we get the same warning for threading/channels?

Running TSan on threading/channels test I get:

==================
WARNING: ThreadSanitizer: data race (pid=21887)
  Write of size 8 at 0x7b30000000a0 by thread T2 (mutexes: write M0):
    #0 sendMpmc__tchannels95simple_u229 threading/tests/channels.nim:248:130 (tchannels_simple+0xece4b) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 channelSend__tchannels95simple_u217 threading/threading/channels.nim:351:11 (tchannels_simple+0xed979) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 send__tchannels95simple_u199 threading/threading/channels.nim:378:27 (tchannels_simple+0xed979)
    #3 secondWorker__tchannels95simple_u292 threading/tests/tchannels_simple.nim:19:2 (tchannels_simple+0xed979)
    #4 threadProcWrapDispatch__stdZtypedthreads_u105 Nim/lib/system/threadimpl.nim:71:2 (tchannels_simple+0xeb531) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #5 threadProcWrapStackFrame__stdZtypedthreads_u95 Nim/lib/system/threadimpl.nim:100:2 (tchannels_simple+0xeb531)
    #6 threadProcWrapper__stdZtypedthreads_u81 Nim/lib/system/threadimpl.nim:106:2 (tchannels_simple+0xe7923) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)

  Previous read of size 8 at 0x7b30000000a0 by main thread:
    #0 recvMpmc__tchannels95simple_u439 threading/threading/channels.nim:292:22 (tchannels_simple+0xed3e4) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 channelReceive__tchannels95simple_u431 threading/threading/channels.nim:356:11 (tchannels_simple+0xedb8e) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 tryRecv__tchannels95simple_u420 threading/threading/channels.nim:371:11 (tchannels_simple+0xedb8e)
    #3 NimMainModule threading/tests/tchannels_simple.nim:44:36 (tchannels_simple+0xedb8e)
    #4 NimMainInner Nim/lib/system/excpt.nim:443:2 (tchannels_simple+0xede42) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #5 NimMain Nim/lib/system/excpt.nim:454:2 (tchannels_simple+0xede42)
    #6 main Nim/lib/system/excpt.nim:462:3 (tchannels_simple+0xede42)

  As if synchronized via sleep:
    #0 nanosleep <null> (tchannels_simple+0x9d3a8) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 nossleep Nim/lib/pure/os.nim:695:22 (tchannels_simple+0xed788) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 secondWorker__tchannels95simple_u292 threading/tests/tchannels_simple.nim:18:2 (tchannels_simple+0xed8ef) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #3 threadProcWrapDispatch__stdZtypedthreads_u105 Nim/lib/system/threadimpl.nim:71:2 (tchannels_simple+0xeb531) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #4 threadProcWrapStackFrame__stdZtypedthreads_u95 Nim/lib/system/threadimpl.nim:100:2 (tchannels_simple+0xeb531)
    #5 threadProcWrapper__stdZtypedthreads_u81 Nim/lib/system/threadimpl.nim:106:2 (tchannels_simple+0xe7923) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)

  Location is heap block of size 192 at 0x7b3000000000 allocated by main thread:
    #0 malloc <null> (tchannels_simple+0x73373) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 allocChannel__OOZthreadingZchannels_u68 threading/threading/channels.nim:158:24 (tchannels_simple+0xec9d1) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 newChan__tchannels95simple_u3 threading/threading/channels.nim:409:90 (tchannels_simple+0xec9d1)
    #3 NimMainModule threading/tests/tchannels_simple.nim:9:33 (tchannels_simple+0xeda1e) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #4 NimMainInner Nim/lib/system/excpt.nim:443:2 (tchannels_simple+0xede42) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #5 NimMain Nim/lib/system/excpt.nim:454:2 (tchannels_simple+0xede42)
    #6 main Nim/lib/system/excpt.nim:462:3 (tchannels_simple+0xede42)

  Mutex M0 (0x7b3000000000) created at:
    #0 pthread_mutex_init <null> (tchannels_simple+0x8c128) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 initLock__coreZlocks_u7 Nim/lib/core/locks.nim:38:2 (tchannels_simple+0xeca09) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 allocChannel__OOZthreadingZchannels_u68 threading/threading/channels.nim:163:2 (tchannels_simple+0xeca09)
    #3 newChan__tchannels95simple_u3 threading/threading/channels.nim:409:90 (tchannels_simple+0xeca09)
    #4 NimMainModule threading/tests/tchannels_simple.nim:9:33 (tchannels_simple+0xeda1e) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #5 NimMainInner Nim/lib/system/excpt.nim:443:2 (tchannels_simple+0xede42) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #6 NimMain Nim/lib/system/excpt.nim:454:2 (tchannels_simple+0xede42)
    #7 main Nim/lib/system/excpt.nim:462:3 (tchannels_simple+0xede42)

  Thread T2 (tid=21890, running) created by main thread at:
    #0 pthread_create <null> (tchannels_simple+0x671d6) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #1 createThread__stdZtypedthreads_u60 Nim/lib/std/typedthreads.nim:246:106 (tchannels_simple+0xe7a05) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #2 createThread__stdZtypedthreads_u51 Nim/lib/std/typedthreads.nim:262:2 (tchannels_simple+0xe7aee) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #3 NimMainModule threading/tests/tchannels_simple.nim:36:2 (tchannels_simple+0xedb21) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #4 NimMainInner Nim/lib/system/excpt.nim:443:2 (tchannels_simple+0xede42) (BuildId: f22605139fae68815f4f4e8605e8b7b5965f57a2)
    #5 NimMain Nim/lib/system/excpt.nim:454:2 (tchannels_simple+0xede42)
    #6 main Nim/lib/system/excpt.nim:462:3 (tchannels_simple+0xede42)

SUMMARY: ThreadSanitizer: data race threading/tests/channels.nim:248:130 in sendMpmc__tchannels95simple_u229
==================
ThreadSanitizer: reported 1 warnings