DecRef (in Goroutine) leads to crash #16
Replies: 2 comments 5 replies
-
@jshiwam one of the first things I notice is there's no proper error handling, nil checks. This is how I tend do it: detectdataPy := detect.CallObject(args)
if !(detectdataPy != nil && python3.PyErr_Occurred() == nil) { //retval: New reference
python3.PyErr_Print()
return nil, fmt.Errorf("error calling function detect")
}
defer detectdataPy.DecRef() (note that this might not necessarily be the best way to do it, just what I tend to use) |
Beta Was this translation helpful? Give feedback.
-
I think part of the problem is that in this example you're releasing the GIL in the
However, directly after these lines Python stuff is happening in the main function. If you move the above block to the line before you start the Goroutines it doesn't seem to crash:
https://go.dev/play/p/ogdAmVyl5F0 Maybe as a tiny bit of advice (maybe it helps you, maybe not). I highly recommend to look up every Python C-API function that you call in the Python C-API docs. Imho it's the only way to avoid deadly surprises. |
Beta Was this translation helpful? Give feedback.
-
originally posted by @jshiwam as comment to #5 .
moved from the PR to github discussions by the mod.
author: @jshiwam
date: 14-Jun-22
Beta Was this translation helpful? Give feedback.
All reactions