Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Channels do not deep copy with gc:orc contrary to the docs

Open yglukhov opened this issue 6 months ago • 3 comments

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64] Compiled at 2025-04-09 Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 29a2e25d1e47deaa7fbaaf5aaf78ab5be430c731 active boot switches: -d:release

Description

type
  Msg = object
    content: Content
  Content = ref object
    value: int

var chan: Channel[Msg]

var contentAddress: int

# This proc will be run in another thread using the threads module.
proc firstWorker() =
  let content = Content(value: 5)
  contentAddress = cast[int](content)
  echo "Sending content: ", contentAddress
  chan.send(Msg(content: content))

# Initialize the channel.
chan.open()

# Launch the worker.
var worker1: Thread[void]
createThread(worker1, firstWorker)

# Block until the message arrives, then print it out.
let c = chan.recv()
echo "Received content: ", cast[int](c.content)
doAssert(contentAddress != cast[int](c.content)) # FAILS HERE!!!

# Wait for the thread to exit
worker1.joinThread()

Current Output

Sending content: 139901182169160
Received content: 139901182169160
/home/y/proj/test/tests/test2.nim(160) test2
/home/y/proj/nim-devel/lib/std/assertions.nim(41) failedAssertImpl
/home/y/proj/nim-devel/lib/std/assertions.nim(36) raiseAssert
/home/y/proj/nim-devel/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: /home/y/proj/test/tests/test2.nim(160, 9) `contentAddress != cast[int](c.content)`  [AssertionDefect]

Expected Output

No assertsion failure

Known Workarounds

No response

Additional Information

Behaves correctly with --gc:refc

yglukhov avatar Apr 09 '25 13:04 yglukhov