kRPC: Control the game using C#, C++, Java, Lua, Python...

kRPC: Control the game using C#, C++, Java, Lua, Python...

7.8k Downloads

Python client crashes on connection when pre-generated stubs are not available

djungelorm opened this issue ยท 1 comments

commented

What happened?

$ python
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import krpc
>>> krpc.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "env/lib/python3.10/site-packages/krpc/__init__.py", line 54, in connect
    return Client(rpc_connection, stream_connection, use_pregenerated_stubs)
  File "env/lib/python3.10/site-packages/krpc/client.py", line 53, in __init__
    self._types.register_class_type(service_info.name, name, typ)
  File "env/lib/python3.10/site-packages/krpc/types.py", line 66, in register_class_type
    assert key not in self._types
AssertionError

How can someone else reproduce it?

Install KRPC.MechJeb (or some other third party service) and the python client crashes when connecting.

What is your environment?

n/a

Anything else we need to know?

No response

commented

This was caused by:

  1. Client connects to server and calls GetServices
  2. Sees KRPC.MechJeb service, for which it does not have pregenerated stubs, so dynamically creates the stubs.
  3. In doing so, it needs types from SpaceCenter so dynamically creates them.
  4. Then sees SpaceCenter server, for which it does have pregenerated stubs.
  5. When trying to load the types from the stubs, they collide with the dynamically created types from step 3.

The fix is to overwrite the dynamically create types in step 5, rather than throwing an assertion.
A better fix is to load all services for which the client has pre-generated stubs first, then load dynamic services. That ensures that class/enum types are already registered for those with pre-generated stubs before types are dynamically created.