Diagnostic reference
Lock timeout: another process was holding the lock for too long
Fix the Apps Script LockService error Lock timeout: another process was holding the lock for too long. Use tryLock(), a finally block, and shorter locks.
Updated December 6, 2026
Apps Script error
Exception: Lock timeout: another process was holding the lock for too long.
What it means
waitLock(timeoutInMillis) throws this error when another execution still holds the same lock after the timeout. Usually one run holds the lock for too long because it does slow work inside it, such as UrlFetchApp or GmailApp calls, or it never releases the lock because an earlier line threw before releaseLock(). tryLock() waits the same way but returns false instead of throwing, so the script can decide what to do.
Related error messages
- Exception: Lock timeout: another process was holding the lock for too long.
- There are too many LockService operations against the same script.
Before and after
The code before and after the fix.
function appendRateRow() {
const lock = LockService.getScriptLock();
lock.waitLock(1000);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Rates');
if (!sheet) throw new Error('Missing Rates tab.');
const rates = UrlFetchApp.fetch('https://api.example.com/rates').getContentText();
sheet.appendRow([new Date(), rates]);
lock.releaseLock();
}function appendRateRow() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Rates');
if (!sheet) throw new Error('Missing Rates tab.');
const rates = UrlFetchApp.fetch('https://api.example.com/rates').getContentText();
const lock = LockService.getScriptLock();
if (!lock.tryLock(30000)) {
Logger.log('Another run is still writing. Skipping this run.');
return;
}
try {
sheet.appendRow([new Date(), rates]);
SpreadsheetApp.flush();
} finally {
lock.releaseLock();
}
}How to fix it
- Do slow work, such as UrlFetchApp, GmailApp, or DriveApp calls, before taking the lock. Hold the lock only for the read-modify-write step that must not overlap.
- Release the lock in a finally block so an error never leaves later runs waiting until the first run ends.
- Use tryLock(timeoutInMillis) and handle false: skip the run, log it, or throw an error that says which row or record needs attention.
- Wait long enough for one normal run to finish. 1 second is often too short when several form submissions or edits arrive together; 10 to 30 seconds is common.
- Call SpreadsheetApp.flush() before releaseLock() so the next run reads the rows this run wrote.
Things to check
- Time spent waiting for a lock counts toward the 6-minute execution limit, or 30 seconds for a simple trigger.
- getScriptLock() blocks every user of the script; getDocumentLock() blocks only runs on the same document, and getUserLock() only runs by the same user. Pick the narrowest lock that still protects the data.
- A skipped run does nothing. If every submission must be handled, log what was skipped so you can process it later.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.