{"record":{"id":"b08b3efc7d94912c","repo":"juicedata/juicefs","slug":"insert-update-failed","errorCode":null,"errorMessage":"insert/update failed","messagePattern":"insert/update failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/meta/sql_lock.go","lineNumber":91,"sourceCode":"\t\t\t\t\t\treturn syscall.EAGAIN\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttypec = 'R'\n\t\t\t} else if len(locks) > 0 {\n\t\t\t\treturn syscall.EAGAIN\n\t\t\t}\n\t\t\tvar n int64\n\t\t\tif ok {\n\t\t\t\tif flk.Ltype != typec {\n\t\t\t\t\tn, err = s.MustCols(\"inode\", \"owner\", \"sid\").Cols(\"Ltype\").Update(&flock{Ltype: typec}, &flock{Inode: inode, Owner: owner, Sid: m.sid})\n\t\t\t\t} else {\n\t\t\t\t\tn = 1\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tn, err = s.InsertOne(&flock{Inode: inode, Owner: owner, Ltype: typec, Sid: m.sid})\n\t\t\t}\n\t\t\tif err == nil && n == 0 {\n\t\t\t\terr = fmt.Errorf(\"insert/update failed\")\n\t\t\t}\n\t\t\tif err == nil {\n\t\t\t\tm.genLog(ctx, s, time.Now().UnixNano(), \"FLOCK(%d,%d,%c)\", inode, owner_, typec)\n\t\t\t}\n\t\t\treturn err\n\t\t}, inode))\n\n\t\tif !block || err != syscall.EAGAIN {\n\t\t\tbreak\n\t\t}\n\t\tif ltype == F_WRLCK {\n\t\t\ttime.Sleep(time.Millisecond * 1)\n\t\t} else {\n\t\t\ttime.Sleep(time.Millisecond * 10)\n\t\t}\n\t\tif ctx.Canceled() {\n\t\t\treturn syscall.EINTR\n\t\t}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/juicedata/juicefs/blob/c9a67b23e8e08ec23ec331aa6f1675e2319e921c/pkg/meta/sql_lock.go#L73-L109","documentation":"When a client tries to acquire (or convert) a POSIX flock, sql_lock.go inserts a new `flock` row or updates an existing one; the write is expected to affect exactly 1 row. If xorm reports 0 affected rows despite no SQL error, JuiceFS raises this error because the lock state was not persisted and the flock cannot be considered held.","triggerScenarios":"Calling flock()/Flock (flockAc/Flock through Meta.Flock) when the `Update` matched no row and the `InsertOne` returned RowsAffected=0 — e.g. the flock row was concurrently deleted by another session releasing the lock between the update attempt and insert.","commonSituations":"Heavy lock contention between multiple JuiceFS clients/mounts on the same volume where the flock row is created and dropped concurrently; database connection races during client session cleanup (ClearSession removing flocks of a dying sid).","solutions":["Retry the flock operation; transient races between competing lockers are the usual cause.","Check for stale sessions (`juicefs status <meta-url>`) and ensure old clients exited cleanly; clear dead sessions with `juicefs destroy` if needed.","Inspect database connectivity/latency — slow transactions widen the race window; tune the DB or reduce clients.","Upgrade JuiceFS if on an older version; lock handling in sql_lock.go has been hardened over time."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for i := 0; i < 3; i++ {\n    if err := flock(fd, LOCK_EX); err == nil { break }\n    time.Sleep(backoff(i))\n}","preventionTips":["Ensure all clients shut down cleanly so flocks are released, not timed out","Clean stale sessions before debugging lock errors (juicefs status/destroy)","Keep metadata DB latency low; contention widens race windows"],"tags":["database","locking","flock","concurrency","metadata"],"backgroundTag":"database-write-failed","analyzedSha":"c9a67b23e8e08ec23ec331aa6f1675e2319e921c","analyzedAt":"2026-09-06T17:55:48.476Z","contentChangedAt":"2026-09-06T17:55:48.476Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}