1

I am having a problem calling a shared c library from python. The library loads fine and one of the 3 functions exposed are callable, and work as expected; however, when adding another function to the library, I found that the other two do not work. What happens is that python hangs on the actual function call and just spins while doing nothing. Both the python side and the c side have detailed logging, and I can tell that the function in the library isn't even starting because none of the debug files are being written.

Below is a small snippet of the code in question:

This is the c code sample function that is failing:

    int genTest(char* filePath)
    {
       infoPrint(filePath, 2);
       return EXIT_SUCCESS;
    }

this is the python that I am calling it with:

    import ctypes as C

    lg.create_log_entry('loading dll... from ' + str(lib_path) + str(lib_name))
    myclib = C.CDLL(lib_path + lib_name)

    lg.create_log_entry('loaded')

    genTest = myclib.genTest

    genTest.argtypes = [C.c_char_p]
    genTest.restype = C.c_int

    lg.create_log_entry('test start')
    res = genTest(C.c_char_p(str(source_as_image)))
    certirxlogging.create_log_entry('test successful')

This code gets to writing 'test start' and hangs. Like I said, this works fine on another function in the library elsewhere in the python code, and python isn't throwing me any errors, so, I am unable to debug anything. Any ideas?

5
  • You could try running everything under the control of gdb and produce a backtrace when it hangs. That might help you figure out where to look. What does infoPrint do? Are you sure that filePath is non-NULL when genTest is called? Commented Jan 15, 2013 at 21:59
  • Also: What platform? (And if you can repro this on multiple platforms, that's useful info too.) Commented Jan 15, 2013 at 22:36
  • 1
    And can you provide us a SSCCE? I modified your C code (pastebin.com/NzkUbYb8) and your Python (pastebin.com/Bg6hH8pM), compiled with clang -shared -oct.dylib ct.c, and ran, and everything worked. So, what's different in your test than in mine? Commented Jan 15, 2013 at 22:43
  • @larsks - InfoPrint writes to a log file, nothing fancy, just fopen, fprintf, fclose. This works fine elsewhere in the library. Also, I hadnt tried GDB - that's a good idea, let me give that a shot. And yes, I've actually tried it with static strings, and a few other combinations (even passing null), but it all comes out the same. Commented Jan 16, 2013 at 3:08
  • @abarnert - Ubuntu 12.04 - because of the library it is platform locked for this application; however, we have multiple instances of this platform running and the 2/3 that I can test on both fail in this way. I will try to get a SSCCE soon, and I will try to run your test on my platform - results pending Commented Jan 16, 2013 at 3:13

1 Answer 1

2

So, I figured out why it was failing. Something in that call to my infoPrint function was throwing a segfault.

The reason that cause my program to hang was that I was using python's Multiprocessing library to run this function in a separate process and communicating to it with pipes. The parent process was polling the pipe with a timeout to get a return value that never came because of the sudden crash.

Thanks for the help in getting over this little bump, now I can (hopefully) figure out what is going wrong with the c code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.