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

Global var defined within a proc gets initialized more than once under ORC #24621

Closed
nitely opened this issue Jan 18, 2025 · 5 comments
Closed

Comments

@nitely
Copy link
Contributor

nitely commented Jan 18, 2025

Description

While experimenting with globals and threads I found they get reassigned each time the proc where they are defined is called. This seems to cause race conditions in threads, even though the global var is not mutated. The example is a reduced version that just shows bar is called more than once. If this is intended, the docs need fixing. Works as expected under refc.

proc bar(n: int): seq[int] =
  echo "bar"
  result = newSeq[int](n)
  for i in 0 .. result.len-1:
    result[i] = i

proc genfoo(): seq[int] =
  var foo {.global.} = bar(100)
  return foo

proc fooworker() =
  var f = genfoo()
  doAssert f.len == 100, $f.len

proc main() =
  fooworker()
  fooworker()

main()

Nim Version

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

git hash: 70d057f
active boot switches: -d:release

Current Output

bar
bar

Expected Output

bar

Known Workarounds

No response

Additional Information

No response

@nitely nitely changed the title Global var defined within a proc gets initialized multiple times under ORC Global var defined within a proc gets initialized more than once under ORC Jan 18, 2025
@nitely
Copy link
Contributor Author

nitely commented Jan 18, 2025

mmh looks like a dup of #17552 ; the workaround does not work in this case, though.

@nitely nitely closed this as completed Jan 18, 2025
@metagn
Copy link
Collaborator

metagn commented Jan 18, 2025

A workaround is to mark genfoo as {.nodestroy.}

@nitely
Copy link
Contributor Author

nitely commented Jan 18, 2025

I get an OOM error when adding nodestroy in my real program. But adding things that should do nothing like doAssert true later result in a sigsevg, or a index error...

When checking the above example (with nodestroy) in valgrind it looks like there are some double free total heap usage: 2 allocs, 3 frees. So likely some memory corruption.

@Araq
Copy link
Member

Araq commented Jan 18, 2025

A workaround is to mark genfoo as {.nodestroy.}

No, it's not. ;-)

@nitely
Copy link
Contributor Author

nitely commented Jan 18, 2025

proc genfoo(): lent seq[int] {.nodestroy.} = i.e: adding lent + nodestroy seems to work. The global is never destroyed though. but this should not be allowed pretty sure -> #24624

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

No branches or pull requests

3 participants