nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).

Home Page:https://nim-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async memory leak when raising exception on ORC

nitely opened this issue · comments

Description

It seems there is a mem leak when raising exceptions from an async proc.

The command I ran:

$ nim c -d:danger --mm:orc --debugger:native --threads:off -d:useMalloc bug.nim && valgrind --leak-check=full --show-leak-kinds=all ./bug
import std/asyncdispatch

proc err {.async.} =
  raise newException(ValueError, "err1")
  #discard

proc amain {.async.} =
  await sleepAsync(1)
  try:
    await err()
  except ValueError:
    discard

proc main() =
  waitFor amain()

main()
doAssert not hasPendingOperations()
setGlobalDispatcher(nil)
GC_fullCollect()
echo "ok"

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2024-05-16
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 2c85515
active boot switches: -d:release

Current Output

Hint: mm: orc; opt: speed; options: -d:danger
63095 lines; 2.754s; 87.977MiB peakmem; proj: /home/esteban/AtomProjects/crap/bug.nim; out: /home/esteban/AtomProjects/crap/bug [SuccessX]
==145078== Memcheck, a memory error detector
==145078== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==145078== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==145078== Command: ./bug
==145078== 
ok
==145078== 
==145078== HEAP SUMMARY:
==145078==     in use at exit: 136 bytes in 2 blocks
==145078==   total heap usage: 19 allocs, 17 frees, 113,298 bytes allocated
==145078== 
==145078== 56 bytes in 1 blocks are indirectly lost in loss record 1 of 2
==145078==    at 0x484DCD3: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==145078==    by 0x10CC5F: _ZN6system11reallocImplE7pointer25range09223372036854775807 (malloc.nim:17)
==145078==    by 0x10CC5F: _ZN6system14alignedReallocE7pointer25range0922337203685477580725range0922337203685477580725range09223372036854775807 (memalloc.nim:383)
==145078==    by 0x10CC5F: prepareSeqAddUninit (seqs_v2.nim:121)
==145078==    by 0x10D697: _ZN6system3addE3varI3seqIN6system15StackTraceEntryEEE4sinkIN6system15StackTraceEntryEE (seqs_v2.nim:161)
==145078==    by 0x10D697: raiseExceptionEx (excpt.nim:504)
==145078==    by 0x1184E8: amain::amainX20X28AsyncX29_ (asyncmacro.nim:170)
==145078==    by 0x117297: amain::amainNimAsyncContinue (asyncmacro.nim:35)
==145078==    by 0x1188D0: bug::amain (asyncmacro.nim:293)
==145078==    by 0x118ADE: _ZN3bug4mainE (bug.nim:15)
==145078==    by 0x118ADE: NimMainModule (bug.nim:17)
==145078==    by 0x10A3F5: NimMainInner (asyncfutures.nim:234)
==145078==    by 0x10A3F5: NimMain (asyncfutures.nim:245)
==145078==    by 0x10A3F5: main (asyncfutures.nim:253)
==145078== 
==145078== 136 (80 direct, 56 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==145078==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==145078==    by 0x10CF3A: _ZN6system10alloc0ImplE25range09223372036854775807 (malloc.nim:11)
==145078==    by 0x10CF3A: _ZN6system13alignedAlloc0E25range0922337203685477580725range09223372036854775807 (memalloc.nim:353)
==145078==    by 0x10CF3A: nimNewObj (arc.nim:96)
==145078==    by 0x117507: _ZN3err18errX20X28AsyncX29_E (asyncfutures.nim:211)
==145078==    by 0x117507: err::errX20X28AsyncX29_ (asyncmacro.nim:250)
==145078==    by 0x116FAA: err::errNimAsyncContinue (asyncmacro.nim:33)
==145078==    by 0x118230: bug::err (asyncmacro.nim:293)
==145078==    by 0x118434: amain::amainX20X28AsyncX29_ (bug.nim:10)
==145078==    by 0x11725A: amain::amainNimAsyncContinue (asyncmacro.nim:33)
==145078==    by 0x1188D0: bug::amain (asyncmacro.nim:293)
==145078==    by 0x118ADE: _ZN3bug4mainE (bug.nim:15)
==145078==    by 0x118ADE: NimMainModule (bug.nim:17)
==145078==    by 0x10A3F5: NimMainInner (asyncfutures.nim:234)
==145078==    by 0x10A3F5: NimMain (asyncfutures.nim:245)
==145078==    by 0x10A3F5: main (asyncfutures.nim:253)
==145078== 
==145078== LEAK SUMMARY:
==145078==    definitely lost: 80 bytes in 1 blocks
==145078==    indirectly lost: 56 bytes in 1 blocks
==145078==      possibly lost: 0 bytes in 0 blocks
==145078==    still reachable: 0 bytes in 0 blocks
==145078==         suppressed: 0 bytes in 0 blocks
==145078== 
==145078== For lists of detected and suppressed errors, rerun with: -s
==145078== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Expected Output

No leaks

Possible Solution

No response

Additional Information

Valgrind does not show leaks when trying in Nim 1.6.20

Bisect points to commits in this PR: #23612 (or before/after it, bug appears near those commits). 🤔