Search code examples
javascriptgoogle-apps-scriptthread-safetycollision

Google Apps Script PropertiesService access collision


I need to access the script properties safely from multiple calls to the script. By using my own method of logging, I can see that the two calls are within milliseconds of each other (2 ms to be exact).

Is there any way at all to ensure that the data written by the first call will be seen by the second? At the moment, the second call checks to see if there's already data in the script properties, but doesn't see what the first call sets because they are too close apart.


Solution

  • I have fixed the problem. Turns out it had little to do with collision using the PropertiesService. The callback where the lock is obtained never actually reached the unlocking stage.

    For the longest time, I thought it was because a new lock overwrote the current lock's data and caused the unlocking to fail, but it was actually the callback that obtained the lock throwing an exception before it could unlock the lock. I just wrapped its content in a try-catch block and put the unlock call inside the finally block.

    I really should have figured it out sooner.

    Edit: I should specify that the "lock" that I am talking about is not the LockService's lock. It is my own implementation that uses a tag identifier instead of the whole script.