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

Method withFreshConfigForRefresh

internal/config/refreshable.go:97–129  ·  view source on GitHub ↗

withFreshConfigForRefresh runs fn, a credential-store read-modify-write (a token refresh, or a logout that must not race one), while holding both an in-process mutex and a cross-process advisory lock, and reloads the configuration from disk before running fn so it observes any credential another pro

(fn func(lockErr error) error)

Source from the content-addressed store, hash-verified

95// The reload refreshes the shared configuration in place, so any config changes not yet written to disk are discarded.
96// That is acceptable on the refresh path because mutations are persisted as they happen.
97func (c *AuthConfig) withFreshConfigForRefresh(fn func(lockErr error) error) error {
98 c.refreshMu.Lock()
99 defer c.refreshMu.Unlock()
100
101 // Best-effort cross-process lock; see the godoc. We proceed without it rather than fail the caller when the state
102 // directory cannot be created or the lock cannot be acquired. lockErr then lets fn decide what is unsafe or futile
103 // without serialized peers. The residual risk without the lock is two processes refreshing at once and spending a
104 // rotating token twice.
105 unlock := func() {}
106 ctx, cancel := context.WithTimeout(context.Background(), refreshLockTimeout)
107 defer cancel()
108
109 acquire := c.acquireRefreshLock
110 if acquire == nil {
111 acquire = flockRefreshLock
112 }
113
114 // Put the lock in the state directory, not the config directory. The config directory can be read-only: under nix
115 // home-manager and similar setups, managed config files are symlinked into the immutable nix store, so creating a
116 // lock file there always fails. The state directory is XDG's location for writable runtime state, home-manager
117 // leaves it writable, and gh already writes state there (state.yml, telemetry), so it is the safer bet.
118 u, lockErr := acquire(ctx, filepath.Join(ghConfig.StateDir(), refreshLockFile))
119 if lockErr == nil {
120 unlock = u
121 }
122 defer unlock()
123
124 if err := c.reload(); err != nil {
125 return fmt.Errorf("could not reload configuration: %w", err)
126 }
127
128 return fn(lockErr)
129}
130
131// reload re-reads the config from disk into the shared config, using the reloadConfig seam when set and the real
132// ghConfig.Reload otherwise. The production fallback passes fallbackConfig so a config file deleted between reads is

Calls 3

reloadMethod · 0.95
JoinMethod · 0.80
ErrorfMethod · 0.65