Understanding the LockService

43 views
Skip to first unread message

Andrew Apell

unread,
Sep 24, 2019, 3:43:44 PM9/24/19
to Google Apps Script Community

If my code has this:

lock.waitLock(30000)
//SomeCode
lock.releaseLock()


Does the lock get released immediately after the SomeCode has finished running or does everything have to wait for the entire 30 seconds to run out?

ChiefChippy2

unread,
Sep 24, 2019, 3:55:32 PM9/24/19
to Google Apps Script Community
lock.waitLock(30000) would try to set a lock, and if
-it manages to set a lock within 30 seconds, it will not return an exception and so the statement will continue
->When the statement is done it releases the lock instantly ( or basically "unlocks the lock" )
-it does NOT manage to set a lock within 30 seconds, it returns an exception and the statement below won't happen

I suppose this can be called "Shared Resource State" - if there is a lock, it is "occupied" and you will have to wait until it is "vacant" to use it ( and you should make it "occupied" until you are done, and you shouldn't keep it occupied when you don't need it anymore). 

I wonder what happens if you never release the lock?

Andrew Apell

unread,
Oct 11, 2019, 3:50:28 AM10/11/19
to Google Apps Script Community
The lock is automatically released when the script terminates, but for efficiency it is best to release it as soon as you no longer need exclusive access to a section of code

Reply all
Reply to author
Forward
0 new messages