I've written a program using tkinter which when the main window is closed is supposed to be minimized to the system tray. But when I try to exit the program clicking "Close" in the tray that triggers the following function:
def quit_window(icon, item):
    icon.stop() # Удаление иконки из трея
    sys.exit(0) # Завершение программы
But it does not work and throws the following exception:
An error occurred when calling message handler
Traceback (most recent call last):
  File "C:\Users\a-par\mini_library_2020\env\lib\site-packages\pystray\_win32.py", line 386, in _dispatcher
    return int(icon._message_handlers.get(
  File "C:\Users\a-par\mini_library_2020\env\lib\site-packages\pystray\_win32.py", line 207, in _on_notify 
    descriptors[index - 1](self)
  File "C:\Users\a-par\mini_library_2020\env\lib\site-packages\pystray\_base.py", line 267, in inner
    callback(self)
  File "C:\Users\a-par\mini_library_2020\env\lib\site-packages\pystray\_base.py", line 368, in __call__
    return self._action(icon, self)
  File "c:/Users/a-par/mini_library_2020/LC.pyw", line 2976, in quit_window
    sys.exit(0)
SystemExit: 0
Also there's a VK bot in the program which is supposed to work when the program is minimized (it's the reason for actually minimizing to the tray). The bot works in a different from GUI thread. I tried to delete the bot fully but it didn't any help. Maybe the problem is threads but I don't think that way...
Minimally reproducible non-working code:
import pystray
import sys
import time
from PIL import Image
from pystray import Menu, MenuItem
def exit_action(icon):
    sys.exit(0)
def setup(icon):
    icon.visible = True
    
    i = 0
    while icon.visible:
        # Some payload code
        print(i)
        i += 1
        
        time.sleep(5)
def init_icon():
    icon = pystray.Icon('mon')
    icon.menu = Menu(
        MenuItem('Exit', lambda : exit_action(icon)),
    )
    icon.icon = Image.open('C:/Users/a-par/mini_library_2020/logo.ico')
    icon.title = 'tooltip'
    icon.run(setup)
init_icon()
 
    