Skip to content

Std lib multi-thread demo occasionally crashes with exit code 1, or SIGSEGV: Illegal storage access. (Attempt to read from nil?) #24988

Open
@forchid

Description

@forchid

Nim Version

nim -v
Nim Compiler Version 2.2.4 [Windows: amd64]
Compiled at 2025-04-22
Copyright (c) 2006-2025 by Andreas Rumpf

active boot switches: -d:release

Description

This demo from the Nim std library doc occasionally crashes with exit code 1, or "SIGSEGV: Illegal storage access. (Attempt to read from nil?) " in nim 2.0.16 and nim 2.2.4 with "-d:release --mm:orc --threads:on". There are the same issues with "-d:debug", but it's ok in nim 1.6.20.

The demo source

# Crash with exit code 1, or SIGSEGV: Illegal storage access. (Attempt to read from nil?)
# nim 2.0.16 and 2.2.4 with "-d:release --mm:orc --threads:on" but nim 1.6.20 ok
import locks

var l: Lock

proc threadFunc(obj: ptr seq[int]) {.thread.} =
  withLock l:
    for i in 0..<10:
      obj[].add(obj[].len * obj[].len)

proc threadHandler() =
  var thr: array[0..4, Thread[ptr seq[int]]]
  var s = newSeq[int]()
  
  for i in 0..high(thr):
    createThread(thr[i], threadFunc, s.addr)
  joinThreads(thr)
  echo s

initLock(l)
threadHandler()
deinitLock(l)

Current Output

# The release test
>nim c -d:release --mm:orc --threads:on passptr
Hint: used config file '...\.choosenim\toolchains\nim-2.2.4\config\nim.cfg' [Conf]
Hint: used config file '...\.choosenim\toolchains\nim-2.2.4\config\config.nims' [Conf]
.......................................................................
CC: passptr.nim
Hint:  [Link]
Hint: mm: orc; threads: on; opt: speed; options: -d:release
28909 lines; 1.547s; 31.148MiB peakmem; proj: passptr; out: ...\passptr.exe [SuccessX]

>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]

>passptr

>echo %errorlevel%
-1073741819

>

# The debug test
>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]
Traceback (most recent call last)
...passptr.nim(22) passptr
...\.choosenim\toolchains\nim-2.2.4\lib\system.nim(651) threadHandler
...\.choosenim\toolchains\nim-2.2.4\lib\system\alloc.nim(1165) dealloc
...\.choosenim\toolchains\nim-2.2.4\lib\system\alloc.nim(1045) rawDealloc
...\.choosenim\toolchains\nim-2.2.4\lib\system\alloc.nim(602) listRemove
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

# The release test
>nim c -d:release --mm:orc --threads:on passptr
Hint: used config file '...\.choosenim\toolchains\nim-2.2.4\config\nim.cfg' [Conf]
Hint: used config file '...\.choosenim\toolchains\nim-2.2.4\config\config.nims' [Conf]
.......................................................................
CC: passptr.nim
Hint:  [Link]
Hint: mm: orc; threads: on; opt: speed; options: -d:release
28909 lines; 1.547s; 31.148MiB peakmem; proj: passptr; out: ...\passptr.exe [SuccessX]

>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]

>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]

>passptr
@[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]

>

Known Workarounds

Using nim 1.6.20

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions