MCPcopy Create free account
hub / github.com/cli/cli / Lock

Function Lock

internal/flock/flock.go:30–45  ·  view source on GitHub ↗

Lock blocks until it acquires an exclusive lock on the given path, retrying TryLock with small sleep gaps while another process holds the lock. It stops waiting when ctx is done and returns ErrTimeout in that case, so a stuck or long-held lock cannot block the caller indefinitely. Any error other th

(ctx context.Context, path string)

Source from the content-addressed store, hash-verified

28// contention is returned immediately. On success it returns the locked file and
29// an unlock function, with the same semantics as TryLock.
30func Lock(ctx context.Context, path string) (f *os.File, unlock func(), err error) {
31 for {
32 f, unlock, err = TryLock(path)
33 if err == nil {
34 return f, unlock, nil
35 } else if !errors.Is(err, ErrLocked) {
36 return nil, nil, err
37 }
38
39 select {
40 case <-ctx.Done():
41 return nil, nil, ErrTimeout
42 case <-time.After(lockRetryInterval):
43 }
44 }
45}

Callers 5

TestLockAcquiresWhenFreeFunction · 0.92
flockRefreshLockFunction · 0.92

Calls 1

TryLockFunction · 0.70