{"record":{"id":"485eca3cefeded59","repo":"Tencent/WeKnora","slug":"redis-lock-ownership-lost","errorCode":null,"errorMessage":"redis lock ownership lost","messagePattern":"redis lock ownership lost","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/common/redislock/token_lock.go","lineNumber":19,"sourceCode":"// Package redislock provides token-owned Redis locks with atomic renewal and\n// release. Callers must treat loss of ownership as loss of exclusive access.\npackage redislock\n\nimport (\n\t\"context\"\n\t\"crypto/rand\"\n\t\"encoding/hex\"\n\t\"errors\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/redis/go-redis/v9\"\n)\n\nconst releaseTimeout = 5 * time.Second\n\n// ErrLockOwnershipLost means exclusive ownership can no longer be guaranteed.\nvar ErrLockOwnershipLost = errors.New(\"redis lock ownership lost\")\n\ntype ownershipContextKey struct{}\n\nvar (\n\treleaseScript = redis.NewScript(`\nif redis.call('GET', KEYS[1]) == ARGV[1] then\n\treturn redis.call('DEL', KEYS[1])\nend\nreturn 0\n`)\n\trenewScript = redis.NewScript(`\nif redis.call('GET', KEYS[1]) == ARGV[1] then\n\treturn redis.call('PEXPIRE', KEYS[1], ARGV[2])\nend\nreturn 0\n`)\n)\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/common/redislock/token_lock.go#L1-L37","documentation":"Sentinel ErrLockOwnershipLost in redislock: exclusive ownership of the token-owned Redis lock can no longer be guaranteed — either the final release found the token no longer owned the key, or a renewal attempt failed mid-critical-section. The owner context is cancelled so the callback must stop touching shared state.","triggerScenarios":"Thrown at internal/common/redislock/token_lock.go:19 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Abort the protected operation; treat all work done after loss of ownership as unsafe and redo it under a fresh lock","Increase the lease duration or lower the renewal interval if ownership is lost due to slow callbacks"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}