Outblock / FRW-iOS

Apache License 2.0
3 stars 4 forks source link

[BUG] Race condition between script retrieval and cadence calls #497

Open jeden opened 1 week ago

jeden commented 1 week ago

The problem

The latest script is loaded in CadenceManager.fetchScript(), indirectly invoked during the app initialization (in AppDelegate). This call is completely asynchronous and in the background, with no completion response. Moreover, it takes quite some time to complete, measured in terms of seconds, typically after the home screen has been shown.

This can cause issues to cadence calls, because they would use an older version of the script, with potential failures, if the request or response have changed between the two versions.

This race condition however is limited to two cases (if the cache is correctly working):

  1. When the app is (re)installed.
  2. When a new version of the script is released.

The former case is probably harmless, because when launching the app for the first time there is user interaction, and so the network call should be completed before the user takes any action.

The latter case can be problematic, but it would happen the first time only, as once loaded, the new script is cached locally and reused at the next launch.

Potential solution

Refactor the initialization process to make sure it's completed after the script update invocation is completed. Since the call itself is quite time expensive, it might be beneficial to have an API that returns the version number only, skipping the update altogether if the version is the same, or doing a blocking invocation otherwise.