Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std/channels example fails #17380

Closed
timotheecour opened this issue Mar 15, 2021 · 2 comments
Closed

std/channels example fails #17380

timotheecour opened this issue Mar 15, 2021 · 2 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Mar 15, 2021

Example 1

taken from nim-lang/website#274

when true:
  import std/times
  var
    sender: array[10, Thread[void]]
    receiver: array[5, Thread[void]] 
  import std/[channels, isolation]
  var chan = newChannel[seq[string]](40)
  proc sendHandler() =
    chan.send(isolate(@["Hello, Nim"]))
  proc recvHandler() =
    var x: seq[string]
    chan.recv(x)
    discard x

  template benchmark() =
    for t in mitems(sender):
      t.createThread(sendHandler)
    joinThreads(sender)
    for i in 0 .. receiver.high:
      createThread(receiver[i], recvHandler)
    let start = now()
    joinThreads(receiver)
    let t2 = now()
    let t3 = t2 - start
    echo t3
  benchmark()

nim c --threads --gc:arc -g main.nim
lldb ./main

Current Output

run it a few times, and eventually you get a crash in lldb, or a hang outside of lldb:

(lldb) r
Process 68773 launched: '/Users/timothee/git_clone/nim/timn/tests/nim/all/t12015' (x86_64)
Process 68773 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x0000000100007d0d start-addr:0x0000000100007d03  t12015 eqdestroy___dS1BF3Vxjg9aJMmmhVJKSpQ(dest=0x00000001001720e8) + 45 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/fatal.nim:53:36
   50
   51    else:
   52      proc sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
-> 53        raise (ref exceptn)(msg: message)
   54
   55      proc sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
   56        raise (ref exceptn)(msg: message & arg)
Target 0: (t12015) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x0000000100007d0d start-addr:0x0000000100007d03  t12015 eqdestroy___dS1BF3Vxjg9aJMmmhVJKSpQ(dest=0x00000001001720e8) + 45 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/fatal.nim:53:36
    frame #1: 0x0000000100010333 start-addr:0x000000010001032b  t12015 eqdestroy___0RiuPw9cXhtLB9a2rQ2jA69cg(dest=0x00007ffeefbfac80) + 83 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2247:4
    frame #2: 0x0000000100019a68 start-addr:0x0000000100019a46  t12015 dollar___PRCiermVoUNsHtsSaiiDLw(dur=(seconds = 0, nanosecond = 427000)) + 968 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2247:2
    frame #3: 0x000000010001da42 start-addr:0x000000010001da2f  t12015 NimMainModule + 1346 at  /Users/timothee/git_clone/nim/timn/tests/nim/all/t12015.nim:154:15
    frame #4: 0x000000010001d4f9 start-addr:0x000000010001d4f4  t12015 NimMainInner + 9 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2210:2
    frame #5: 0x000000010001db9a start-addr:0x000000010001db98  t12015 NimMain + 42 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2218:2
    frame #6: 0x000000010001dbee start-addr:0x000000010001dbe9  t12015 main(argc=1, args=0x00007ffeefbfae90, env=0x00007ffeefbfaea0) + 62 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2225:2
  frame #7: 0x00007fff6a526cc9 libdyld.dylib`start + 1
(lldb) ^D

Expected Output

works

Example 2

same as Example 1, but replace

  benchmark()

by

  for i in 0..<100: benchmark()

then it crashes even more reliably

Additional Information

  • nim: 1.5.1 95697d0
  • tested on OSX
  • lldb isn't needed to reproduce the crash; just run it a few (~5) times
  • I can still reproduce the crash if i use var chan = newChannel[seq[string]](4000)

/cc @xflywind

@timotheecour
Copy link
Member Author

here's a stacktrace with this diff:

diff --git a/lib/std/channels.nim b/lib/std/channels.nim
index 1b0844c48..805eb99c1 100644
--- a/lib/std/channels.nim
+++ b/lib/std/channels.nim
@@ -456,7 +456,9 @@ proc `=`*[T](dest: var Channel[T], src: Channel[T]) =
 proc channelSend[T](chan: Channel[T], data: sink T, size: int, nonBlocking: bool): bool {.inline.} =
   ## Send item to the channel (FIFO queue)
   ## (Insert at last)
-  sendMpmc(chan.d, data.unsafeAddr, size, nonBlocking)
+  # sendMpmc(chan.d, data.unsafeAddr, size, nonBlocking)
+  result = sendMpmc(chan.d, data.unsafeAddr, size, nonBlocking)
+  wasMoved(result)

 proc channelReceive[T](chan: Channel[T], data: ptr T, size: int, nonBlocking: bool): bool {.inline.} =
   ## Receive an item from the channel
(lldb) r
Process 70873 launched: '/Users/timothee/git_clone/nim/timn/build/nimcache/t12015' (x86_64)
Process 70873 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xc)
    frame #0: 0x000000010000ec02 start-addr:0x000000010000ec02  t12015 rawAlloc__mE4QEVyMvGRVliDWDngZCQ(a=0x0000000100027010, requestedSize=25) + 738 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim:787:116
   784           result = c.freeList
   785           when not defined(gcDestructors):
   786             sysAssert(c.freeList.zeroField == 0, "rawAlloc 8")
-> 787           c.freeList = c.freeList.next
   788         dec(c.free, size)
   789         sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 9")
   790         sysAssert(allocInv(a), "rawAlloc: end c != nil")
Target 0: (t12015) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xc)
  * frame #0: 0x000000010000ec02 start-addr:0x000000010000ec02  t12015 rawAlloc__mE4QEVyMvGRVliDWDngZCQ(a=0x0000000100027010, requestedSize=25) + 738 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim:787:116
    frame #1: 0x00000001000090a5 start-addr:0x00000001000090a0  t12015 alloc__UxtcZ3QOXKsB7mMchxUf9cg(allocator=0x0000000100027010, size=25) + 101 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim:955:11
    frame #2: 0x000000010000a973 start-addr:0x000000010000a964  t12015 allocSharedImpl(size=25) + 51 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim:1075:11
    frame #3: 0x000000010000975d start-addr:0x0000000100009758  t12015 allocShared0Impl__KzdpcuLT9aef9bsiSHlIu9aFg(size=25) + 29 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim:1081:11
    frame #4: 0x000000010000f527 start-addr:0x000000010000f522  t12015 nimAsgnStrV2(a=0x00007ffeefbfaba0, b=NimStringV2 @ 0x00007ffeefbfaa40) + 679 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/strs_v2.nim:151:30
    frame #5: 0x000000010000f625 start-addr:0x000000010000f618  t12015 eqcopy___aBBXmHFBEivKqERloP6zmA(dest=0x00007ffeefbfaba0, src=NimStringV2 @ 0x00007ffeefbfaa70) + 37 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/fatal.nim:53:2
    frame #6: 0x00000001000190f7 start-addr:0x00000001000190e6  t12015 humanizeParts__AO89azFsQMOpWkka1p4T7pQ(parts=tySequence__sM4lkSb7zS6F7OVMvW9cffQ @ 0x00007ffeefbfab90) + 343 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system/fatal.nim:53:3
    frame #7: 0x00000001000199fd start-addr:0x00000001000199f0  t12015 dollar___PRCiermVoUNsHtsSaiiDLw(dur=(seconds = 0, nanosecond = 539000)) + 877 at  /Users/timothee/git_clone/nim/Nim_prs/lib/pure/times.nim:732:11
    frame #8: 0x000000010001da32 start-addr:0x000000010001da1f  t12015 NimMainModule + 1346 at  /Users/timothee/git_clone/nim/timn/tests/nim/all/t12015.nim:183:15
    frame #9: 0x000000010001d4e9 start-addr:0x000000010001d4e4  t12015 NimMainInner + 9 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2210:2
    frame #10: 0x000000010001db8a start-addr:0x000000010001db88  t12015 NimMain + 42 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2218:2
    frame #11: 0x000000010001dbde start-addr:0x000000010001dbd9  t12015 main(argc=1, args=0x00007ffeefbfae88, env=0x00007ffeefbfae98) + 62 at  /Users/timothee/git_clone/nim/Nim_prs/lib/system.nim:2225:2
  frame #12: 0x00007fff6a526cc9 libdyld.dylib`start + 1
  frame #13: 0x00007fff6a526cc9 libdyld.dylib`start + 1


(lldb) fr v
(tyObject_MemRegion__x81NhDv59b8ercDZ9bi85jyg *) a = 0x0000000100027010
(NI) requestedSize = 25
(void *) result = 0x000000000000000c
(NI) size = 32
(TFrame) FR_ = {
  prev = 0x00007ffeefbfa948
  procname = 0x000000010001e57c "rawAlloc"
  line = 787
  filename = 0x000000010001e264 "/Users/timothee/git_clone/nim/Nim_prs/lib/system/alloc.nim"
  len = 0
  calldepth = 4
  frameMsgLen = 0
}
(NI) s = 2
(tyObject_SmallChunk__tXn60W2f8h3jgAYdEmy5NQ *) c = 0x0000000100172000


ringabout added a commit to ringabout/Nim that referenced this issue Mar 16, 2021
@timotheecour
Copy link
Member Author

my bad with the diff, seems to work with wasMoved(data)

@Araq Araq closed this as completed in d5eb658 Mar 16, 2021
ringabout added a commit that referenced this issue Mar 19, 2021
* Revert "make system random work in VM"

* fix #17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up #17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim
ringabout added a commit that referenced this issue Mar 19, 2021
* Revert "make system random work in VM"

* fix #17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up #17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim

* fix a typo
ringabout added a commit to ringabout/Nim that referenced this issue Mar 22, 2021
ringabout added a commit to ringabout/Nim that referenced this issue Mar 22, 2021
* Revert "make system random work in VM"

* fix nim-lang#17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up nim-lang#17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim
ringabout added a commit to ringabout/Nim that referenced this issue Mar 22, 2021
* Revert "make system random work in VM"

* fix nim-lang#17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up nim-lang#17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim

* fix a typo
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
* Revert "make system random work in VM"

* fix nim-lang#17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up nim-lang#17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
* Revert "make system random work in VM"

* fix nim-lang#17380

* attempt to fix bug

* fix

* better

* fix

* a bit

* fix the leaks

* revert

* fix

* better

* follow up nim-lang#17391

* fix

* Update tchannels.nim

* Update tests/stdlib/tchannels.nim

* Update tchannels.nim

* fix a typo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants