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

(Unintended) Destruction of Thread object causes hard to debug crash #7172

Closed
skunkiferous opened this issue Feb 1, 2018 · 9 comments
Closed

Comments

@skunkiferous
Copy link

Me, and people trying to help me on the forum, spent a while to debug a crash, which was due to a Thread object being destroyed, while the thread is still running. In this particular case, I believe the Thread object was on the stack, but I assume the same would happen with a GCed Thread object.

Here is a simple example that causes the crash, at least for me on Windows, and was also reproducible on Archlinux:

import os

proc whatever() {.thread, nimcall.} =
  echo("TEST")

proc initProcess(): void =
  echo("In initProcess()")
  var thread: Thread[void]
  createThread(thread, whatever)
  echo("initProcess() done")

proc doStuff(): void =
  echo("In doStuff()")
  # ...
  initProcess()
  sleep(500)
  # ...
  echo("Crashes before getting here!")

doStuff()

If "var thread" is moved to the global scope, the crash does not happen.

It is not clear to me if the crash can be prevented though a compile-time error, but at least it would be good to have an error message that users understand. It might also be good to mention that this can happen in the Thread and/or createThread documentation.

@cheatfate
Copy link
Member

@andreaferretti, please unmark Threads, because this is not bug of threads. If people creates something in stack, they need to be ready that this will be lost after procedure exit.

@andreaferretti
Copy link
Collaborator

I agree it is not a bug of threads - in some sense it is not a bug at all. Still I thought it would be useful to locate issues such as this when looking for thread-related problems

@ghost
Copy link

ghost commented Feb 5, 2018

Doesnt crash for me. Nim latest head.

@ghost
Copy link

ghost commented Feb 5, 2018

Crashes for me on Arch Linux with Clang and GCC
@konqoro what is your platform/compiler?

@cheatfate
Copy link
Member

cheatfate commented Feb 5, 2018

@konqoro, please run executable many times , crashes can be not constant...

@ghost
Copy link

ghost commented Feb 5, 2018

Tried again and without '-d:release' crashes every single time. with release doesn't. gcc version 7.2.1 20180116 (GCC) manjaro

@genotrance
Copy link
Contributor

Confirmed works on Windows, crashes on Ubuntu.

@narimiran
Copy link
Member

without '-d:release' crashes every single time

I've run it several times (without -d:release) on Manjaro Linux, with Nim 1.5, and it never crashed.

@kobi2187
Copy link
Contributor

I think that it is not clear from the docs what happens on createThread.
If the thread instance is added to some global sequence, and can be retrieved later, then I guess it shouldn't crash. But if you're trying to run an operation through an object which by this time is already nil, you will get a runtime error, in other words, a crash. Should the compiler warn? usually it doesn't know about runtime state.

Varriount added a commit that referenced this issue Oct 6, 2022
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
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

7 participants