{"record":{"id":"77dd61ba7e87789c","repo":"prestodb/presto","slug":"failed-to-acquire-lock","errorCode":null,"errorMessage":"Failed to acquire lock","messagePattern":"Failed to acquire lock","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java","lineNumber":1827,"sourceCode":"                                    }\n                                    else if (newState.equals(ACQUIRED)) {\n                                        acquired.set(true);\n                                    }\n                                    else {\n                                        throw new RuntimeException(String.format(\"Failed to acquire lock: %s\", newState.name()));\n                                    }\n                                    return null;\n                                })));\n                }\n            }\n            finally {\n                if (!acquired.get()) {\n                    unlock(metastoreContext, lockId);\n                }\n            }\n\n            if (!acquired.get()) {\n                throw new RuntimeException(\"Failed to acquire lock\");\n            }\n\n            return lockId;\n        }\n        catch (TException e) {\n            throw new PrestoException(HIVE_METASTORE_ERROR, e);\n        }\n        catch (Exception e) {\n            throw propagate(e);\n        }\n    }\n\n    @Override\n    public void unlock(MetastoreContext metastoreContext, long lockId)\n    {\n        try {\n            retry()\n                    .stopOnIllegalExceptions()","sourceCodeStart":1809,"sourceCodeEnd":1845,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java#L1809-L1845","documentation":"After the lock polling loop finishes, if the lock was never transitioned to ACQUIRED, ThriftHiveMetastore unlocks (if a lockId exists) and throws RuntimeException(\"Failed to acquire lock\"). The metastore never granted the lock within the allowed attempts, so the operation is aborted for safety.","triggerScenarios":"lockAcquire loop exhausts its retry/timeout budget while checkLock keeps returning WAITING (or the loop ends without ACQUIRED) during operations like dropPartition that need an exclusive table lock.","commonSituations":"Heavy concurrent DDL/DML on the same Hive table keeping the lock permanently WAITING, too-short lock timeout configuration, a hung session holding the lock without heartbeating, metastore under load responding slowly so retries time out.","solutions":["Identify and stop/complete the competing job holding the metastore lock on the table.","Increase lock acquisition timeout/retry settings (e.g. hive.lock.numretries, check interval) for long operations.","Check for and clean up stale locks left by dead sessions in the metastore lock manager.","Serialize conflicting operations on the table (avoid running big partition drops concurrently with writes).","Catch this RuntimeException and retry with backoff in orchestration scripts."],"exampleFix":"// before: concurrent drop while writer holds lock\nexecutor.submit(() -> metastore.dropPartition(context, db, table, parts, false));\n\n// after: wait for conflicting jobs, then retry with backoff\nawaitNoActiveWrites(db, table);\nretryWithBackoff(() -> metastore.dropPartition(context, db, table, parts, false));","handlingStrategy":"retry","validationCode":"// pre-check lock availability before running the operation\nif (metastoreLockHeld(db, table)) { throw new IllegalStateException(\"Table \" + db + \".\" + table + \" is locked; retry later\"); }","typeGuard":null,"tryCatchPattern":"try {\n    metastore.dropPartition(context, db, table, parts, deleteData);\n} catch (RuntimeException e) {\n    if (\"Failed to acquire lock\".equals(e.getMessage())) {\n        // exponential backoff and retry\n    } else throw e;\n}","preventionTips":["Serialize conflicting operations on the same table in orchestration.","Increase lock acquisition timeout configuration.","Detect and clear locks abandoned by dead sessions.","Keep metastore responsive (capacity, DB health) so lock checks complete in time."],"tags":["hive","metastore","locking","concurrency"],"backgroundTag":"lock-acquisition-failed","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}