I’m working on a multithreaded plugin (st3) that does automated edits on the text buffers simultaneously with the user. Occasionally, when a user edit occurs at exactly the right time simultaneously with my plugin’s edits, a race condition occurs between the main UI thread and my plugin, as evidenced by intermittent single character edit placement errors during high speed typing.
Normally, I would solve this problem with locks to ensure that the “calculate edit” + “perform edit” steps were atomic, but Sublime doesn’t seem to support any kind of buffer locking (understandably…) Optimizing my plugin go be as fast as possible could mitigate the problem by minimizing the “danger” time frame, but that wouldn’t solve the root cause here. It really seems like I need to block the UI thread momentarily, or problems will inevitably occur with low frequency.
Maybe I could coerce the “on_modified” callback into acting as a lock, or potentially I could make the “perform edit” function do error checking, and recursively retry until success without race conditions (but icky).
Has anyone else dealt with this problem?