odwdinc / Python-SimConnect

Python interface for MSFS2020 SimConnect.dll
GNU Affero General Public License v3.0
277 stars 105 forks source link

Exception in Thread-1 when sim is closed #91

Closed Delilovic closed 3 years ago

Delilovic commented 3 years ago

Hello! Thank you for the great library!

I have an issue when closing the simulator while connected with SimConnect. The library throws an error in Thread 1 which I can not catch. Doing something like:

try:
    print("Waiting for MSFS_2020 Simulator...")
    sm = SimConnect()
    print("Connected to MSFS_2020!")
except ConnectionError as err:
    print("Error connecting to MSFS2020: {}".format(err))

will not catch the error when connected to the simulator and suddenly closing the simulator. The following exception is thrown and can not be caught, any ideas? Thank you!

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python\Python39\lib\site-packages\SimConnect\SimConnect.py", line 173, in _run
    self.dll.CallDispatch(self.hSimConnect, self.my_dispatch_proc_rd, None)
  File "_ctypes/callproc.c", line 997, in GetResult
OSError: [WinError -1073741648] Windows Error 0xc00000b0
storca commented 3 years ago

Hi Looks like that you have to loop on a condition to check if the simulator is started, it's not done in the small extract you gave. See https://github.com/odwdinc/Python-SimConnect/blob/master/local_example.py#L69

odwdinc commented 3 years ago

OSError: [WinError -1073741648] Windows Error 0xc00000b0

Try OSError, lol 🙃

try:
    print("Waiting for MSFS_2020 Simulator...")
    sm = SimConnect()
    print("Connected to MSFS_2020!")
except ConnectionError as err:
    print("Error connecting to MSFS2020: {}".format(err))
except OSError as err:
        print("OS error: {0}".format(err))
Delilovic commented 3 years ago

Hi Looks like that you have to loop on a condition to check if the simulator is started, it's not done in the small extract you gave. See https://github.com/odwdinc/Python-SimConnect/blob/master/local_example.py#L69

@storca

This might be the case if I would send or receive some requests from the simulator which I don't. It is true that I am inside a while loop but not doing anything. Here is a very simple way to reproduce this exception.

  1. Start the sim
  2. Run this python script:
    if __name__ == '__main__':
    sm = SimConnect()
    while True:
        sleep(1)

    3 Quit the sim 4 Read the exception from the console

@odwdinc it doesn't matter if you put try and except around it, the exception happens inside the thread of SimConnect() that is why you see Thread-1 exception

odwdinc commented 3 years ago

should be fix in 0.4.24 reopen if not.