[issue38680] PyGILState_Release does not release gil correctly, resulting in deadlock

2019-11-05 Thread Damien LEFEVRE


Damien LEFEVRE  added the comment:

I see the same problem.

We call python functions from C++ and use this locking class within each call:

'''
#pragma once
#ifdef _DEBUG
#undef _DEBUG
#include 
#define _DEBUG
#else
#include 
#endif // _DEBUG

#include "gcc_helper.h"

GCC_DIAG_OFF(maybe-uninitialized)

class GIL_lock
{
public:
GIL_lock()
{
m_wasPreviouslyLockedByThisThread = PyGILState_Check() == 1;

// If already locked, we shouldn't lock again.
if (m_wasPreviouslyLockedByThisThread == false)
{
m_state = PyGILState_Ensure();
}
}

~GIL_lock()
{
release();
}

void release()
{
// Release only if it wasn't locked previously and wasn't released 
during lifetime.
if (m_released == false && m_wasPreviouslyLockedByThisThread == false)
{
PyGILState_Release(m_state);
}

m_released = true;
}

private:
PyGILState_STATE m_state;
bool m_released = false;
bool m_wasPreviouslyLockedByThisThread = false;
};
'''

Example:
'''
void PythonInterpreterPrivate::setPythonHome(const QString& path)
{
GIL_lock gilLock;

#ifdef _WIN32
wchar_t* pyHome = new wchar_t[MAX_PATH];
#else
wchar_t* pyHome = new wchar_t[PATH_MAX];
#endif
QDir pythonDir(path);

if (pythonDir.exists())
{
path.toWCharArray(pyHome);
pyHome[path.size()] = 0;
Py_SetPythonHome(pyHome);
}
}
'''

With Python3.6, the first instance of GIL_lock in the main thread goes to 
PyGILState_Ensure(). With Python3.7, still in the main thread 
PyGILState_Check() reports the lock is already taken while we don't take it.

In a new thread, with Python3.7 GIL_lock() goes to PyGILState_Ensure() and 
deadlocks there. With Python3.6 PyGILState_Ensure() returns the state.

--
nosy: +Damien LEFEVRE

___
Python tracker 
<https://bugs.python.org/issue38680>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38680] PyGILState_Release does not release gil correctly, resulting in deadlock

2019-11-05 Thread Damien LEFEVRE


Damien LEFEVRE  added the comment:

@ronaldoussoren

The issue here is that the behavior between Python 3.6 and 3.7 has changed.

Our code runs perfectly fine with 3.6. We release the lock each time our 
functions get out of scope and so we know for sure there is no lock left behind.

Starting with 3.7 GIL is acquired when we start the application, but not by us, 
maybe internally?, and never gets released; thus the deadlock.

--

___
Python tracker 
<https://bugs.python.org/issue38680>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38680] PyGILState_Release does not release gil correctly, resulting in deadlock

2019-11-19 Thread Damien LEFEVRE


Damien LEFEVRE  added the comment:

Back to testing this.

I have the same issue on Linux, i.e: the deadlock on PyGILState_Ensure.

I cannot test 3.8 because many of the modules are not yet available for this 
version.

I'll try making a small test application to reproduce the issue.

--

___
Python tracker 
<https://bugs.python.org/issue38680>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38680] PyGILState_Release does not release gil correctly, resulting in deadlock

2019-11-21 Thread Damien LEFEVRE


Damien LEFEVRE  added the comment:

Here is a code example reproducing the issue.

--
Added file: https://bugs.python.org/file48728/interpreterlock.zip

___
Python tracker 
<https://bugs.python.org/issue38680>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com