Found it!! Use PyEval_SaveThread() instead of PyEval_ReleaseLock(). Several websites clearly state that PyEval_ReleaseLock() is to be used, but it seems they're wrong. The official python documentation has a cryptic warning that suggests that using PyEval_SaveThread() is preferred. And indeed, that fixes the bug!