This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyDict_GetItem SegFaults on simple dictionary lookup when using Ctypes
Type: crash Stage: resolved
Components: ctypes Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: apoorvreddy, larry, methane, tehybel
Priority: normal Keywords:

Created on 2019-05-19 04:49 by apoorvreddy, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.tar.gz apoorvreddy, 2019-05-19 04:49 Tar file contains code to reproduce bug.
Messages (5)
msg342834 - (view) Author: Apoorv Reddy (apoorvreddy) Date: 2019-05-19 04:49
I'm trying to use ctypes to speed up an internal function in my project. However, I'm getting a segmentation fault on a simple dictionary lookup in my C Code, on PyDict_GetItem(dict, key).

I have supplied a minimal version of the code I'm trying in C, which segfaults, with the Makefile and the Python driver code which loads the shared library.I have created a simple conda environment for Python 3.6 for the same.
msg342854 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2019-05-19 14:03
It's not surprising that you crashed the CPython interpreter by using ctypes--it's very easy to do by accident, or via a bug in your own code.  That's why we don't accept crash reports involving ctypes.

Also, it's rude to "nosy" so many people, particularly on your first bug.  Please show some courtesy in the future, rather than trying to involve as many core developers as possible with what is probably a bug in your own code.
msg342857 - (view) Author: Apoorv Reddy (apoorvreddy) Date: 2019-05-19 14:22
Greetings Larry !

I apologize for spamming so many people. I was hoping to get some insight into this ! Could you let me know to whom I could reach out for help ? I've included tehybel as I saw that he has raised/resolved some issues with PyDict in the past.

My C code is essentially just this:

#include "Python.h"

#ifdef __cplusplus
extern "C" double test(PyObject* key, PyObject* dict)
#else
double test(PyObject* key, PyObject* dict)
#endif
{
	PyObject* list = PyObject_GetItem(dict, key);
	return 0.0;
}

------------

And my Python Code is this. I'm pretty sure I've got the input and output types correct here.

from ctypes import cdll
from ctypes import *
import json
import sys
import pickle


dll = CDLL('./test2.so')
dll.test.restype = c_double
dll.test.argtypes = (py_object, py_object)

d = {68113113140: [1, 2]}
for i in d.keys():
	if i == 68113113140:
		break
print(dll.test(i, d)) # this works just fine
print(dll.test(68113113140, d) # this segfaults !

GDB shows me that PyObject_RichCompare (called inside PyDict_GetItem) is the function where the segmentation fault happens !
--------

Hoping for some guidance here ! I've been trying to resolve this for 3 days now. I have made sure that I've compiled with the correct version of Python headers and that I'm using the same version of interpreter as well (in this case Python 3.5.6)
msg342879 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-05-19 23:35
At first glance, you used ctypes.cdll, which releases GIL.  But you called Python/C API in extension module.  You shouldn't do it.
Try ctypes.pydll, which don't release GIL.

If it doesn't help you, please continue it in another communities.
I don't want to make Issue Tracker as user support forum.


> Could you let me know to whom I could reach out for help ?

Communities!

* Slack: https://pyslackers.com/
* Mailing list: https://mail.python.org/mailman/listinfo/python-list
* Stack Overflow: https://stackoverflow.com/questions/tagged/python
msg342880 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2019-05-20 00:14
Inada-san, while it is best to not call PyDict_ functions without holding the GIL, it doesn't matter unless one creates a second thread.  The GIL doesn't even exist until Python creates a second thread.

But, I too don't want bugs.python.org to become a "help people debug their programs" site.  Particularly when using ctypes, which crashes a lot.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81144
2019-05-20 00:14:19larrysetmessages: + msg342880
2019-05-19 23:35:34methanesetnosy: + methane
messages: + msg342879
2019-05-19 14:22:10apoorvreddysetnosy: - georg.brandl, rhettinger, amaury.forgeotdarc, belopolsky, christian.heimes, benjamin.peterson, ned.deily, duaneg, methane, meador.inge, serhiy.storchaka, abarry
messages: + msg342857
2019-05-19 14:03:55larrysetstatus: open -> closed
resolution: rejected
messages: + msg342854

stage: resolved
2019-05-19 05:06:05apoorvreddysetnosy: + georg.brandl, rhettinger, amaury.forgeotdarc, belopolsky, larry, christian.heimes, benjamin.peterson, ned.deily, duaneg, methane, meador.inge, serhiy.storchaka, abarry, tehybel
2019-05-19 04:49:20apoorvreddycreate