Skip to content

Object reference

Lock

A LockService.Lock keeps two trigger executions from editing the same Sheet, Doc, or property at the same time. Acquire it with tryLock(timeoutInMillis) and release it in a finally block.

LockService.Lock

Start here

Get this object first

Call LockService.getScriptLock() to coordinate all runs of the script project, getDocumentLock() to coordinate runs on the active document, or getUserLock() to coordinate runs for the current user.

Code.gs
function checkScriptLockAvailable() {
  const lock = LockService.getScriptLock();
  if (!lock.tryLock(5000)) {
    Logger.log('Another run holds the script lock.');
    return;
  }
  try {
    Logger.log('Has lock: ' + lock.hasLock());
  } finally {
    lock.releaseLock();
  }
}

Then use it

Common actions

Choose an action, then open its example only when you need the code.

Skip a trigger run when another execution holds the lock

Waits up to 2 seconds with tryLock(2000). If an earlier trigger run still holds the lock, it logs a message and returns without doing the work.

LockService.Lock
Show example code
Code.gs
function runIfScriptLockIsFree() {
  const lock = LockService.getScriptLock();
  if (!lock.tryLock(2000)) {
    Logger.log('Skipping run because another execution is active.');
    return;
  }
  try {
    Logger.log('Lock acquired for exclusive work.');
  } finally {
    lock.releaseLock();
  }
}

Guard a property update with waitLock and finally

Waits up to 10 seconds with waitLock(10000), adds one to a run counter in script properties, and always calls releaseLock() in finally. If the wait runs out, waitLock throws an error.

LockService.LockPropertiesService.Properties
Show example code
Code.gs
function incrementRunCounterWithLock() {
  const lock = LockService.getScriptLock();
  lock.waitLock(10000);
  try {
    const props = PropertiesService.getScriptProperties();
    const nextCount = Number(props.getProperty('runCount') || '0') + 1;
    props.setProperty('runCount', String(nextCount));
    Logger.log('Run count: ' + nextCount);
  } finally {
    lock.releaseLock();
  }
}

Check whether the current execution holds the lock

Logs hasLock() before acquiring the script lock, while holding it, and after releasing it.

LockService.Lock
Show example code
Code.gs
function logLockStateTransition() {
  const lock = LockService.getScriptLock();
  Logger.log('Before tryLock: ' + lock.hasLock());
  if (lock.tryLock(1000)) {
    try {
      Logger.log('Inside try block: ' + lock.hasLock());
    } finally {
      lock.releaseLock();
    }
  }
  Logger.log('After releaseLock: ' + lock.hasLock());
}

Technical reference

Exact names and signatures

Open method details when you need them.

At a glance

Service
LockService
Reference symbol
LockService.Lock
Object
Lock
Runtime
From signature data

Methods and signatures

Browse 4 methods

hasLock

From signature data Google source
  • hasLock(): boolean

releaseLock

From signature data Google source
  • releaseLock(): void

tryLock

From signature data Google source
  • tryLock(timeoutInMillis: number): boolean

waitLock

From signature data Google source
  • waitLock(timeoutInMillis: number): void

Need the whole workflow?

Return to a task guide

Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.

Browse Script utilities tasks