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

thread.kill() gets stuck endlessly #4

Open
iMaxopoly opened this issue Jul 3, 2016 · 3 comments
Open

thread.kill() gets stuck endlessly #4

iMaxopoly opened this issue Jul 3, 2016 · 3 comments

Comments

@iMaxopoly
Copy link

Hello,

I hope I am not simply making a mistake but I have noticed that when I create a pthread inside a new goroutine(not the main), and I try to call thread.kill(), it does not always work. Sometimes I does, other times it just gets stuck.

It would be very kind of you to investigate this in your free time.

Thank you

@liamzdenek
Copy link
Owner

Can you provide an isolated test case for this? I cannot begin debugging without it as the behavior is inconsistent, and I cannot be sure that it's not due to something specific to your application. It's a potentially endless time-sink.

You can try wrapping your calls to go-pthreads functions with runtime.LockOsThread() and runtime.UnlockOsThread(). This will ensure that the calls to pthreads occur in the main OS thread. I'm unsure if this will have any impact.
https://golang.org/pkg/runtime/#LockOSThread

@iMaxopoly
Copy link
Author

Hi,

This is an example program I wrote to demonstrate that threads don't exit properly, and sometimes do.
It's a very rough code and I'm new to go but here's hoping you can tell me what I'm doing wrong.

https://gist.github.com/kryptodev/b2479035cc09f0d90a6a16b8709b7abb

Thank you

@liamzdenek
Copy link
Owner

I stripped out all the Python stuff to make sure that the problem actually exists in this library and isn't some python.h logic that dislikes multithreading.

It looks like the deadlock appears in pthread.Create(). I'm also creating and destroying the threads serially, so there couldn't be any overlap (eg, killing the wrong thread). Sometimes, the thread doesn't start. C.pthread_create returns status 0 (no error), but it just never runs. If I remove the call to thread.Kill(), my attached script works correctly.[1]

However, it appears to be happening if thread.Kill() is called before the thread actually exists. A secondary channel can be used to prevent this by sending a value down this channel before any call to thread.Kill()[2]

I've got no idea why this is happening. I don't know the internal details of how pthread operates, and I don't have time to dig too deep in this at the moment.

[1] https://gist.github.com/liamzdenek/864b327bc660f4ab445f3729238f038b
[2] https://gist.github.com/liamzdenek/afa4f4b5d8b1e8ce1df8ca2756d13d69

Repository owner deleted a comment from the80srobot Mar 20, 2021
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

2 participants