liamzdenek / go-pthreads

Lightweight binding of pthreads to Google Go
BSD 2-Clause "Simplified" License
36 stars 8 forks source link

thread.kill() gets stuck endlessly #4

Open iMaxopoly opened 8 years ago

iMaxopoly commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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