Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug] Crash with PySimpleGUI + psgtray + PyGame #6812

@DanTappan

Description

@DanTappan

Type of Issue (Enhancement, Error, Bug, Question)

Bug/Crash


Operating System

Windows 11

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkintr


Versions (NOTE - PSG4 is no longer supported)

Python Interpeter: C:\Users\vmix\GIL-crash\GIL-crash.venv\Scripts\python.exe
Python version: 3.12.3
Platform: Windows
Platform version: ('11', '10.0.22631', 'SP0', 'Multiprocessor Free')
Port: PySimpleGUI
tkinter version: 8.6.13
PySimpleGUI version: 5.0.6
PySimpleGUI filename: C:\Users\vmix\GIL-crash\GIL-crash.venv\Lib\site-packages\PySimpleGUI\PySimpleGUI.py


Your Experience In Months or Years (optional)

Years Python programming experience
5+

Years Programming experience overall
30+

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine)
no

Anything else you think would be helpful?

This may be related to #4880 because it only happens when window.read(timeout), not with window.read()


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • None of your GUI code was generated by an AI algorithm like GPT
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.com
  • Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • duplicated the problem running python from the command line
  • problem originally found using Python 3.10, reproduced with Python 3.12

Detailed Description

Problem: Program crashes with error

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

The full error dump is below

Duplicating the problem requires all of the following conditions:

  1. import pygame and call pygame.init()

  2. add a menu to the main PySimpleGUI window

  3. create a psgtray SystemTray
  4. 
call window.read(timeout) - the crash does not occur if no timeout

  5. call pygame.events.get() - the crash does not occur is this is not called

  6. select an item from the window menu - the crash occurs when the item is selected, the window.read() call
 does not return


Code To Duplicate

I was able to duplicate the problem by inserting code for the above into a reduced version of the psgtray sample program. I have not been able to reduce it to 10-20 lines

import PySimpleGUI as sg
from psgtray import SystemTray
import pygame

"""
    Demonstrate GIL Crash with PyGame + psgtray
    the following are necessary for the crash:
    1. import pygame and call pygame.init()
    2. add a menu to the main PySimpleGUI window
    3. create a psgtray SystemTray 
    4. call window.read(timeout) - the crash does not occur if no timeout
    5. call pygame.events.get() - the crash does not occur is this is not called
    7. select an item from the window menu - the crash occurs when the item is selected, the window.read() call
       does not return
"""

def main():

    pygame.init( )

    menu = ['',
            ['Show Window', 'Hide Window', 'Exit']]
    tooltip = 'Tooltip'

    winmenu = [['Menu',
            ['Show Window', 'Hide Window', 'Exit']]]

    layout = [[sg.Menu(winmenu)],
              [sg.Text('My PySimpleGUI Celebration Window - X will minimize to tray')],
              [sg.T('Double clip icon to restore or right click and choose Show Window')],
              [sg.Multiline(size=(60, 10), reroute_stdout=False, reroute_cprint=True, write_only=True, key='-OUT-')],
              [sg.Button('Go'), sg.B('Hide Icon'), sg.B('Show Icon'), sg.B('Hide Window'), sg.Button('Exit')]]

    window = sg.Window('Window Title', layout, finalize=True, enable_close_attempted_event=True)

    tray = SystemTray(menu, single_click_events=False, window=window, tooltip=tooltip, icon=sg.DEFAULT_BASE64_ICON)

    sg.cprint(sg.main_get_debug_data())
    sg.cprint(sg.get_versions())

    while True:
        event, values = window.read(10)

        if event == sg.TIMEOUT_EVENT:
            events = pygame.event.get()
            continue

        # IMPORTANT step. It's not required, but convenient. Set event to value from tray
        # if it's a tray event, change the event variable to be whatever the tray sent
        if event == tray.key:
            sg.cprint(f'System Tray Event = ', values[event], c='white on red')
            event = values[event]  # use the System Tray's event as if was from the window

        if event in (sg.WIN_CLOSED, 'Exit'):
            break

        sg.cprint(event, values)
        tray.show_message(title=event, message=values)

        if event in ('Show Window', sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED):
            window.un_hide()
            window.bring_to_front()
        elif event in ('Hide Window', sg.WIN_CLOSE_ATTEMPTED_EVENT):
            window.hide()
            tray.show_icon()  # if hiding window, better make sure the icon is visible
            # tray.notify('System Tray Item Chosen', f'You chose {event}')


    tray.close()  # optional but without a close, the icon may "linger" until moused over
    window.close()

if __name__ == '__main__':
    main()

Screenshot, Sketch, or Drawing


Watcha Makin?

I am working on an enhanced version of the project https://github.com/International-Anglican-Church/visca-joystick , adding better window support, configuration, controller hot plugging ...

Full Error dump

pygame 2.6.1 (SDL 2.28.4, Python 3.10.11)
Hello from the pygame community. https://www.pygame.org/contribute.html
Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Thread 0x00001164 (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 147 in _mainloop
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 128 in _run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_base.py", line 212 in run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\psgtray\psgtray.py", line 324 in _pystray_thread
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953 in run
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016 in _bootstrap_inner
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 973 in _bootstrap

Current thread 0x00003d8c (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 70 in main
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 110 in <module>

Extension modules: PIL._imaging, pygame.base, pygame.constants, pygame.rect, pygame.rwobject, pygame.surflock, pygame.bufferproxy, pygame.math, pygame.surface, pygame.display, pygame.draw, pygame.event, pygame.imageext, pygame.image, pygame.joystick, pygame.key, pygame.mouse, pygame.time, pygame.mask, pygame.pixelcopy, pygame.transform, pygame.font, pygame.mixer_music, pygame.mixer, pygame.scrap, pygame._freetype, PIL._imagingmath, PIL._webp (total: 28)
(.venv) VMIX:psgtray_test vmix$ .venv/Scripts/python psgtray-test.py
pygame 2.6.1 (SDL 2.28.4, Python 3.10.11)
Hello from the pygame community. https://www.pygame.org/contribute.html
Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Thread 0x00003bd0 (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 147 in _mainloop
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 128 in _run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_base.py", line 212 in run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\psgtray\psgtray.py", line 324 in _pystray_thread
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953 in run
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016 in _bootstrap_inner
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 973 in _bootstrap

Current thread 0x00004404 (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 70 in main
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 110 in <module>

Extension modules: PIL._imaging, pygame.base, pygame.constants, pygame.rect, pygame.rwobject, pygame.surflock, pygame.bufferproxy, pygame.math, pygame.surface, pygame.display, pygame.draw, pygame.event, pygame.imageext, pygame.image, pygame.joystick, pygame.key, pygame.mouse, pygame.time, pygame.mask, pygame.pixelcopy, pygame.transform, pygame.font, pygame.mixer_music, pygame.mixer, pygame.scrap, pygame._freetype, PIL._imagingmath, PIL._webp (total: 28)
(.venv) VMIX:psgtray_test vmix$ .venv/Scripts/python psgtray-test.py
pygame 2.6.1 (SDL 2.28.4, Python 3.10.11)
Hello from the pygame community. https://www.pygame.org/contribute.html
Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Thread 0x000041e0 (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 147 in _mainloop
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_win32.py", line 128 in _run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\pystray\_base.py", line 212 in run
  File "C:\Users\vmix\psgtray-test\psgtray_test\.venv\lib\site-packages\psgtray\psgtray.py", line 324 in _pystray_thread
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953 in run
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016 in _bootstrap_inner
  File "C:\Users\vmix\AppData\Local\Programs\Python\Python310\lib\threading.py", line 973 in _bootstrap

Current thread 0x000045b4 (most recent call first):
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 70 in main
  File "C:\Users\vmix\psgtray-test\psgtray_test\psgtray-test.py", line 110 in <module>

Extension modules: PIL._imaging, pygame.base, pygame.constants, pygame.rect, pygame.rwobject, pygame.surflock, pygame.bufferproxy, pygame.math, pygame.surface, pygame.display, pygame.draw, pygame.event, pygame.imageext, pygame.image, pygame.joystick, pygame.key, pygame.mouse, pygame.time, pygame.mask, pygame.pixelcopy, pygame.transform, pygame.font, pygame.mixer_music, pygame.mixer, pygame.scrap, pygame._freetype, PIL._imagingmath, PIL._webp (total: 28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    InteroperabilityAn interface between PySimpleGUI and some other program or driver

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions