1

I'm using a GAS file as my server side for an app I'm building. I have doPost(e) and doGet(e) functions defined, for example:

function doPost(e) {
     var postData = e.postData.contents;
     var jsonRaw = JSON.parse(postData); 
     var name = jsonRaw.name;

     // make a long URLFetch call...
     
     textOutput = ContentService.createTextOutput('execution done for ' + name); 
     return textOutput         
   }

I do not have any "global" variables declared outside of a specific function's scope. My question is, if I have a long-running doPost running for user A and suddenly user B also makes a doPost request before user A's request completes, is it possible that the name variable gets reassigned and user A sees user B's name in the output?

Based on the answer here: Global Variable value not usable in multiple functions I believe I should be fine, but I wanted to confirm. Thanks!

policenauts
  • 512
  • 6
  • 18
  • The variables in an Apps Script function will be reassigned a new value if another instance of the code starts running before the previous instance has finished. You can try LockService, or if you want an alternative, Google Cloud Functions run in their own isolated secure execution context, and scale automatically. Your Apps Script code is making an HTTPS request, which can be done with a Google Cloud Function HTTP function. [https://cloud.google.com/functions/docs/concepts/exec](https://cloud.google.com/functions/docs/concepts/exec) – Alan Wells Jul 01 '20 at 23:58

2 Answers2

2

Local Variables

Local variables are not a shared resource. When calling a function the variables you defined inside the function will belong to that function's scope. This means that if you have a function that stores a parameter in a variable this variable will be valid only for this particular function execution.

Global Variables

Global variables are of course different since their values are known to all the functions in your Project. These are shared resource so you would want to lock the access to them if you need to modify their value.

Proof of Concept

You can verify this behavior creating a simple Web App that gets a query parameter, stores it in a variable and after 10 seconds returns it.

function doGet(e) {
  var param = e.queryString;
  Utilities.sleep(10000);
  return ContentService.createTextOutput(param);
}

If you call this method with different parameters within the 10 seconds window, you should observe that the value returned by the first GET request is the query parameter of the second one. But it's not.

References

Lock Service

Apps Script Web Apps

Alessandro
  • 2,848
  • 1
  • 8
  • 16
  • Thanks, I should have just tested this myself! But I was hoping to also hear it from someone who's had more direct experience with this. I replicated your test and got the same (different) results. – policenauts Jul 02 '20 at 14:29
0

Perhaps you should be using LockService

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank so much - is this required though in my case for doPost and doGet? Is a block-scoped variable considered a shared resource that can be modified concurrently? – policenauts Jul 01 '20 at 21:30
  • It's just a way to keep multiple users from running a script at the same time. Also note Google Apps Script is limited to less than 30 simultaneous users. – Cooper Jul 01 '20 at 21:32
  • Thanks for the info about the 30 simultaneous users, I'll certainly move to a proper server side provider if I begin to approach that. I guess I'm still not clear though if Lock Service is necessary for my use case? – policenauts Jul 01 '20 at 21:38