[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-09-29 Thread Thomas Holder


Thomas Holder  added the comment:

All good, thanks for opening PR 22453! I'll close my old PR.

--

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-09-29 Thread E. Paine


Change by E. Paine :


--
pull_requests: +21482
pull_request: https://github.com/python/cpython/pull/22453

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-09-29 Thread E. Paine


E. Paine  added the comment:

I do think we should close the existing PR (TBH, I completely forgot about this 
issue). Briefly looking again at the issue, it does indeed look like the GIL 
hasn't seen the thread before the attempted call to restore the tstate (as 
Steve said). If its alright with you, Thomas, I will open a new PR with the 
attached patch so it can be formally reviewed (though you can open the PR if 
you would prefer).

--
components:  -Windows

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-09-29 Thread Thomas Holder


Thomas Holder  added the comment:

I finally managed to test tstate_acquire.diff and it works perfectly! Should I 
close my pull request? Or update it with your patch?

--

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-07-30 Thread E. Paine


E. Paine  added the comment:

Thanks Steve for your explanation. I had a quick experiment with the 
ENTER_PYTHON definition and initially just added a call to PyThreadState_Get if 
the tstate was NULL. This still crashed the interpreter with the following 
error (which I think reaffirms Steve's explanation):

Fatal Python error: PyThreadState_Get: the function must be called with the GIL 
held, but the GIL is released (the current Python thread state is NULL)

To work-around this error, I temporarily acquired the GIL and then released it 
after the call to PyThreadState_Get. The result worked correctly and can be 
found in the attached diff. I am not saying that the attached diff is the 
solution, but I don't think we need to stop using tstate in favour of the 
gstate introduced in the PR.

> I could not reproduce the crash without showing any non-tk dialog.

That would help explain why I cannot reproduce it on Linux - it is differences 
in the windowing API under Tcl not some platform-specific code in Tcl/Tk or 
_tkinter.

--
Added file: https://bugs.python.org/file49350/tstate_acquire.diff

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-07-30 Thread Thomas Holder


Thomas Holder  added the comment:

Attaching an even simpler example which doesn't use PyQt. It shows a native 
Windows dialog using ctypes. I could not reproduce the crash without showing 
any non-tk dialog.

Steve's explanation sounds plausible.

--
Added file: https://bugs.python.org/file49349/simple_tk_demo.py

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-07-29 Thread Steve Dower


Steve Dower  added the comment:

At a glance, it looks like ENTER_PYTHON will *restore* the GIL on the current 
thread, but it may be coming in on a thread that's never seen the GIL before.

"The GIL" is actually the Python thread state, which is actually a per-thread 
data structure that's either active/locked or inactive/unlocked. If the current 
thread doesn't have a thread state, PyGILState_Ensure will create one, while 
ENTER_PYTHON will not.

So the underlying issue is probably that the callbacks are coming in from a 
thread that they shouldn't be, and really ought to be marshalled back into the 
correct event loop first.

--

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-07-29 Thread E. Paine


E. Paine  added the comment:

Sorry for taking so long to do something with this issue. Can you please 
explain *why* (mostly because I don't really have a clue) acquiring the GIL 
fixes this crash (I am not disputing that it does - I have compared the current 
3.9 branch against yours and confirmed this). Also, can it be reproduced with 
just the stdlib (so we can write a unittest for this)? Is it an assumption in 
QT about the GIL rather than a problem with _tkinter?

--
nosy: +epaine
versions: +Python 3.10, Python 3.8, Python 3.9

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-03-28 Thread Terry J. Reedy


Change by Terry J. Reedy :


--
nosy: +serhiy.storchaka

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-03-26 Thread Thomas Holder


Change by Thomas Holder :


--
keywords: +patch
pull_requests: +18538
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/19178

___
Python tracker 

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



[issue40075] _tkinter PythonCmd fails to acquire GIL

2020-03-26 Thread Thomas Holder


New submission from Thomas Holder :

The attached demo application runs a Tkinter GUI and a PyQt GUI in the same 
thread. PyQt owns the main loop and keeps updating the Tkinter instance by 
calling `update()`.

On Windows, when binding a "" event, resizing the Tk window will 
lead to a crash:

```
Fatal Python error: PyEval_RestoreThread: NULL tstate

Current thread 0x1f1c (most recent call first):
  File "qt_tk_demo.py", line 50 in 
```

This crash happens in `_tkinter.c` in `PythonCmd` inside the `ENTER_PYTHON` 
macro.

The issue can be fixed by using `PyGILState_Ensure` and `PyGILState_Release` 
instead of the `ENTER_PYTHON` macro inside the `PythonCmd` function.

--
components: Tkinter, Windows
files: qt_tk_demo.py
messages: 365064
nosy: Thomas Holder, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: _tkinter PythonCmd fails to acquire GIL
type: crash
Added file: https://bugs.python.org/file49002/qt_tk_demo.py

___
Python tracker 

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