Unable to obtain lock on KV
- Dominant language
- Go
- Stars
- 30.1k
- Forks
- 4.6k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 39
Description
#### Unable to obtain lock on consul KV pair, getting error : " Existing key does not match lock use"
I'm trying to write a code which will create a new KV pair if it doesnt exist, if exists it will update the value.
Below code is used for the same:
#### Reproduction Steps
Steps to reproduce this issue, eg:
1. Running consul in dev mode on mac
2. Using the below code first i'm creating a new key pair in local consul
```sh
func acquireTask(taskId string) {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
panic(err)
}
opts := &api.LockOptions{
Key: "tasks/"+taskId,
Value: []byte("set by sender 1"),
LockTryOnce: true,
SessionOpts: &api.SessionEntry{
Behavior: "release",
TTL: "10s",
},
}
firstLock, err := client.LockOpts(opts)
if err != nil {
panic(err)
}
locked, err := firstLock.Lock(nil)
if err != nil {
panic(err)
}
fmt.Printf("=====> result for first lock is %v", locked)
defer firstLock.Unlock()
}
```
3. Now next go to consul ui and edit the key value pair that u have created and save it
4. Rerun the above code to get a lock, u will get the below error
```panic: Existing key does not match lock use```
5. lock.go line 207 is throwing the error
```sh
if pair != nil && pair.Flags != LockFlagValue {
return nil, ErrLockConflict
}```
consul logs show the below:
```sh 2021-03-30T13:05:38.053-0500 [DEBUG] agent.http: Request finished: method=PUT url=/v1/session/create from=127.0.0.1:59982 latency=326.547µs
2021-03-30T13:05:38.053-0500 [DEBUG] agent.http: Request finished: method=GET url=/v1/kv/tasks/unassigned_1249?wait=15000ms from=127.0.0.1:59982 latency=121.301µs ```
So did some debugging and found that:
### LockFlagValue is getting changed when i update the key value from the UI.
// LockFlagValue is a magic flag we set to indicate a key
// is being used for a lock. It is used to detect a potential
// conflict with a semaphore.
I'm struck here, Since i dont have any other process running to acquire a lock or trying to create a semaphore parallely when i run my program.
Contributor guide
Research direction
Start with lock.go line 207 and trace the LockOpts path, LockFlagValue handling, and the Consul KV update behavior. Reproduce the sequence in a local dev-mode Consul: acquire the lock, edit the key in the UI, and acquire it again while inspecting the stored pair. Done requires a confirmed explanation of the changed lock metadata and a clearly scoped behavior or fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- databases, distributed-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100