{"record":{"id":"419fcd76a3b7e590","repo":"hashicorp/terraform","slug":"failed-to-lock-cos-state-s","errorCode":null,"errorMessage":"Failed to lock cos state: %s","messagePattern":"Failed to lock cos state: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/cos/backend_state.go","lineNumber":115,"sourceCode":"\t}\n\n\texists := false\n\tfor _, candidate := range ws {\n\t\tif candidate == name {\n\t\t\texists = true\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif !exists {\n\t\tlog.Printf(\"[DEBUG] workspace %v not exists\", name)\n\n\t\t// take a lock on this state while we write it\n\t\tlockInfo := statemgr.NewLockInfo()\n\t\tlockInfo.Operation = \"init\"\n\t\tlockId, err := c.Lock(lockInfo)\n\t\tif err != nil {\n\t\t\treturn nil, diags.Append(fmt.Errorf(\"Failed to lock cos state: %s\", err))\n\t\t}\n\n\t\t// Local helper function so we can call it multiple places\n\t\tlockUnlock := func(e error) error {\n\t\t\tif err := stateMgr.Unlock(lockId); err != nil {\n\t\t\t\treturn fmt.Errorf(unlockErrMsg, err, lockId)\n\t\t\t}\n\t\t\treturn e\n\t\t}\n\n\t\t// Grab the value\n\t\tif err := stateMgr.RefreshState(); err != nil {\n\t\t\terr = lockUnlock(err)\n\t\t\treturn nil, diags.Append(err)\n\t\t}\n\n\t\t// If we have no state, we have to create an empty state\n\t\tif v := stateMgr.State(); v == nil {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/cos/backend_state.go#L97-L133","documentation":"Raised in Backend.StateMgr() (cos/backend_state.go:115) while initializing a brand-new workspace. Terraform takes a lock on the freshly-created state before writing the empty initial state; if c.Lock() fails the error is wrapped here. The underlying cause is surfaced in the %s.","triggerScenarios":"StateMgr() for a workspace that does not yet exist; c.Lock(lockInfo) fails because another process holds the COS lock tag or lock file, or because tag/COS permissions are insufficient.","commonSituations":"Two concurrent `terraform init` runs creating the same new workspace; a crashed prior run left a lock file/tag; the tag client or COS client lacks permission to create the lock tag.","solutions":["Wait for any in-progress run on that workspace to finish, then re-run `terraform init`.","Run `terraform force-unlock <lock-id>` to clear a stale lock, using the ID from the error.","Manually delete the leftover lock file object and the tencentcloud-terraform-lock tag, then retry.","Verify the configured credentials have COS object and Tag service permissions."],"exampleFix":"// after seeing 'Failed to lock cos state'\nterraform force-unlock <lock-id-from-error>\nterraform init","handlingStrategy":"retry","validationCode":"// Check for an existing lock before initializing a new workspace\nfunc workspaceLocked(c *remoteClient, lockFile string) bool {\n    exists, _, _, _ := c.getObject(lockFile)\n    return exists\n}","typeGuard":null,"tryCatchPattern":"// Retry StateMgr-driven lock acquisition with backoff for transient contention\nfor attempt := 0; attempt < 5; attempt++ {\n    _, diags := b.StateMgr(name)\n    if !diags.HasErrors() { break }\n    if !strings.Contains(diags.Err().Error(), \"Failed to lock cos state\") { return diags }\n    time.Sleep(time.Duration(attempt+1) * time.Second)\n}","preventionTips":["Avoid concurrent `terraform init` for the same new workspace.","Clear stale locks with `terraform force-unlock` before retrying.","Ensure credentials have COS object and Tag permissions for locking."],"tags":["cos","tencentcloud","lock","state","init","workspace"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}