'unexpected error DEADLINE_EXCEEDED google spreadsheet onOpen() trigger

I have a google spreadsheet addon, Formula Tracer Sidebar, published on the Google workspace marketplace.

Every once in a while, the following error entry is in the gcloud log explorer.

Sample log entry

{
"insertId": "***",
"jsonPayload": {
  "serviceContext": {
    "service": "***"
  },
  "context": {
    "reportLocation": {
      "functionName": "[unknown function]",
      "filePath": "[unknown file]"
    }
  },
  "message": "We're sorry, the JavaScript engine reported an unexpected error. Error code DEADLINE_EXCEEDED."
},
"resource": {
  "type": "app_script_function",
  "labels": {
    "invocation_type": "simple trigger",
    "project_id": "formula-tracer-side-bar",
    "function_name": "onOpen"
  }
},
"timestamp": "2022-04-05T18:14:51.408Z",
"severity": "ERROR",
"labels": {
  "script.googleapis.com/user_key": "***",
  "script.googleapis.com/project_key": "***",
  "script.googleapis.com/deployment_id": "***",
  "script.googleapis.com/process_id": "***"
},
"logName": "***",
"receiveTimestamp": "2022-04-05T18:14:51.690091557Z"
}

exception occurred in the onOpen trigger where the the addon menu items are added

The resource.labels values:

"invocation_type": "simple trigger",
"function_name": "onOpen"

onOpen:

function onOpen(e){
  let menu = SpreadsheetApp.getUi().createAddonMenu();
  menu.addItem('Trace precedents', 'tracePrecedents');
  menu.addItem('Trace dependents', 'traceDependents');

  menu.addToUi();.
}

I have never experienced or actually see the error nor know how to reproduce it. I can only can such log files entries.

I found some a bit older posts with similar errors not identical though, suggesting this is because of the V8 run time bug. for example: here and here.

This article by Google Calendar support gives a kind of general explanation and solution (resend request). Assuming this is a similar situation, maybe even the exact same V8 bug, I'm thinking of the following implementation

  1. wrap add menu with try-catch ans extract to separate function _addmenus()
  2. check _addmenus() return value. if not success sleep and try again
  3. max attempts = 10

New onOpen:

function onOpen(e) {
  let done = false;
  const maxTimes = 10;
  const timeout = 200; //ms
  let i = 1;

  //loop to try add menus
  while(!done && i <= maxTimes){
    //add menus
    done = _addmenus();

    //if failed - sleep before next try
    if (!done && i < maxTimes){
      Utilities.sleep(timeout);
    }

    i++;
  }

  //done loop - log failure
  if (!done){
    console.error("failed to add FT menu items");
    const msg = `Unexpected error while loading spreadsheet addon. `
            +`Please try to reload the spreadsheet. `
            +`If the problem continute, wait a few minutes and try again.`;
    SpreadsheetApp.getUi().alert(msg);
 }
}

function _addmenus() {
  let success = false;
  try {

    let menu = SpreadsheetApp.getUi().createAddonMenu();
    menu.addItem('Trace precedents', 'tracePrecedents');
    menu.addItem('Trace dependents', 'traceDependents');
    menu.addToUi();
    success = true;

  } catch (e) {
    console.error("SS onOpen", e);
  }

  return success;
}

I do not even know if the user is actually aware of the exception (that is, gets some error feedback) or that the result is just addon menus items are not being added to the spreadsheet, but I'd like to show a message with SpreadsheetApp.getUi().alert(msg);. Maybe this will also fail. but it is a try to handle the issue.

Is this a good way to handle this situation? Is it OK to sleep while onOpen()?

Update:

I reported a bug in Goolge's Issue Tracker here



Solution 1:[1]

Not sure how and if this will actually solve this, only time will tell.

Google recommends to handle such case like suggested above using sleep and try again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OJNSim