Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:15+00:00 2026-06-16T00:14:15+00:00

I am using LockService to avoid duplicated actions, however I cannot make tryLock to

  • 0

I am using LockService to avoid duplicated actions, however I cannot make tryLock to fail during my testing.

Supposedly this code should write an error in ScriptProperties when running more than one time almost simultaneously, but it does not so far.

A second App instance should fail after tryLock for 1 second, while the first instance is sleeping for 15 seconds, right?

Any suggestions?

function doGet() {
  testingLockService(1000, 15000);
  return;
}
function testingLockService(trying, sleeping) {
  var lock = LockService.getPrivateLock();
  var hasMutex = lock.tryLock(trying);
  if (hasMutex == false) { ScriptProperties.setProperty("LockService",new Date().toString()+" tryLock failed"); return; }
  Utilities.sleep(sleeping);
  lock.releaseLock();
  return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:14:15+00:00Added an answer on June 16, 2026 at 12:14 am

    Interesting question. After having a little play with this I think the locking is working, it just appears it isn’t because Google Apps Script does not seem to allow concurrent get requests, but rather queues them up. By moving your lock test to the server side it then works.

    This is much easier to debug if you have your get request return something to the user rather than put it in a script property.

    The following code will demonstrate the get requests being queued up. To test: make two concurrent requests, and look at the timestamps coming back, interesting you’ll notice the second request will not have a start timestamp before the end timestamp of the first request, no matter how close together you make them. So the second request can perfectly validly get the lock. Here’s the code:

    function doGet() {
      var app = UiApp.createApplication();
    
      var tS = new Date();
      var gotLock = testingLockService(0, 5000);
      var tF = new Date();
    
      var label = app.createLabel(gotLock ? 'Got the lock, and slept' : "Didn't get the lock");
      app.add(label);
    
      var label = app.createLabel('tS ' + tS.getTime());
      app.add(label);
      var label = app.createLabel('tF ' + tF.getTime());
      app.add(label);
      var label = app.createLabel('t delta ' + (tF - tS));
      app.add(label);
    
      return app;
    }
    
    function testingLockService(trying, sleeping) {
      var lock = LockService.getPrivateLock();
      var hasMutex = lock.tryLock(trying);
      if (!hasMutex) { return false; }
      Utilities.sleep(sleeping);
      lock.releaseLock();
      return true;
    }
    

    Now, to prove the locking does work, simply move the locking code to the server side. Again, to test, have two browser windows open and and click on both buttons. This time you will see the second request fail to get the lock and return immediately.

    function doGet() {
      var app = UiApp.createApplication();
      var serverHandler = app.createServerHandler('doClick');
      var button = app.createButton().setText("click me").addClickHandler(serverHandler);
      app.add(button);
      return app;
    }
    
    function doClick() {
      var app = UiApp.getActiveApplication();
      // code from here on is identical to previous example
      var tS = new Date();
      var gotLock = testingLockService(0, 5000);
      var tF = new Date();
    
      var label = app.createLabel(gotLock ? 'Got the lock, and slept' : "Didn't get the lock");
      app.add(label);
    
      var label = app.createLabel('tS ' + tS.getTime());
      app.add(label);
      var label = app.createLabel('tF ' + tF.getTime());
      app.add(label);
      var label = app.createLabel('t delta ' + (tF - tS));
      app.add(label);
    
      return app;
    }
    
    function testingLockService(trying, sleeping) {
      var lock = LockService.getPrivateLock();
      var hasMutex = lock.tryLock(trying);
      if (!hasMutex) { return false; }
      Utilities.sleep(sleeping);
      lock.releaseLock();
      return true;
    }
    

    Hopefully that has answered your question on the locking. Though it raises questions in my mind about the get request queueing. Is it only requests from the same user? I would love to hear from someone else if they have any more info on that, although, maybe that belongs in a question on its own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this code, the following execution yields strange results: C 100 R W The
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using RestKit 0.10.1, I have objects served similar to this json format: {objects: [
using xmltextreader, how would I load a hashtable. XML: <base><user name=john>2342343</user><user name=mark>239099393</user></base> This was
Using ASP.NET MVC 4 RTW, how do I log MVC controller actions and parameters
This is my code: public class ListasCompra extends ListActivity { private ArrayList<Lista> listaCompras =
Using string functions and a whole lot of if-else turns this into a real
Using Play 2.1-RC1 I can't write simple test. Here's the action code: def echoTestTagFromXml
Using MongoDB C# Driver (official form 10gen), I'm doing the following code: using (database.RequestStart())

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.