polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
241 stars 115 forks source link

Fix rpc message order #311

Closed turuslan closed 1 year ago

turuslan commented 1 year ago

Looks like original code expected subscription events to arrive earlier than subscription id, e.g.:

{subscription: 1, result: ...}
{subscription: 1}
{subscription: 1, result: ...}

But there were two problems:

  1. removing processed messages was skipping some messages
  2. always reading before checking queued messages delayed those messages processing
arjanz commented 1 year ago

Thanks for the PR, I will have to run some tests to be sure that there are no unwanted side effects.

As you can imagine it was a bit of a struggle to make the async behavior of the websocket RPC play nice in a sync environment. Changes I already made where the introduction of the message queue and https://github.com/polkascan/py-substrate-interface/commit/a13577e45350b1df9b451bb16f5c221f57bf9c65 to fix race conditions between the subscription id and the first event of that subscription id.

There were sometimes still unexpected results though, so I'm looking forward to review this change, hoping to fix those issues.