Skip to content

feat: install CLI updates with a lock and record the result - #807

Open
zimeg wants to merge 1 commit into
zimeg-feat-auto-update-recordfrom
zimeg-feat-auto-update-install
Open

zimeg wants to merge 1 commit into
zimeg-feat-auto-update-recordfrom
zimeg-feat-auto-update-install

Conversation

@zimeg

@zimeg zimeg commented Nov 18, 2026

Copy link
Copy Markdown
Member

Changelog

N/A, groundwork for the auto-update experiment.

Summary

This pull request adds update.AutoUpdate, which installs a CLI update while holding a lock and records the result for the next command to report.

  • AutoUpdate takes an OS file lock on autoupdate.lock in the Slack config directory with github.com/gofrs/flock. If another update holds it, it returns right away without installing or recording anything, so two terminals finishing commands at the same moment install once.
  • It installs through the existing download, backup, swap and verify path, then records the versions it went from and to, both with a v prefix, plus the error when the install fails. The install error is returned so it reaches the log.
  • The OS frees the lock when the process exits, even after a crash, so there's no stale-lock timeout. The lock file stays on disk after unlocking, since removing it could let another update lock a new file at the same path while this one still holds the old.
  • Downloads of the CLI time out after 10 minutes, so a stalled download can't hold the lock forever.
  • gofrs/flock was already an indirect dependency and is now a direct one.
  • Stacked on feat: record the result of a background CLI update #806. Nothing calls AutoUpdate yet: the hidden command that runs it in the background comes next.

Preview

N/A, nothing changes for developers yet.

Testing

N/A, nothing calls AutoUpdate yet. The unit tests record the new version after installing, record and return the error when the install fails, skip without installing or recording while a second lock holder has the lock, hold the lock during the install and release it afterwards, and check the download timeout.

Notes

  • The lock is a real file on disk rather than through clients.Fs, since an OS lock needs a file descriptor. The tests use a temporary directory as the config directory for this reason.
  • The 10-minute download timeout also applies to slack upgrade, which had no timeout before.
  • GOOS=windows go build ./... compiles: flock uses LockFileEx there.

Requirements

🤖 Generated with Claude Code

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@zimeg zimeg added this to the Next Release milestone Nov 18, 2026
@zimeg zimeg added enhancement M-T: A feature request for new functionality semver:patch Use on pull requests to describe the release version increment labels Nov 18, 2026
@zimeg zimeg self-assigned this Nov 18, 2026
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.95%. Comparing base (6ade942) to head (1c65529).

Files with missing lines Patch % Lines
internal/update/background.go 86.95% 3 Missing ⚠️
Additional details and impacted files
@@                        Coverage Diff                        @@
##           zimeg-feat-auto-update-record     #807      +/-   ##
=================================================================
- Coverage                          80.15%   79.95%   -0.20%     
=================================================================
  Files                                245      246       +1     
  Lines                              18949    18790     -159     
=================================================================
- Hits                               15189    15024     -165     
- Misses                              3760     3766       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zimeg
zimeg marked this pull request as ready for review November 18, 2026 19:59
@zimeg
zimeg requested a review from a team as a code owner November 18, 2026 19:59
@zimeg
zimeg requested a review from mwbrooks November 18, 2026 20:05
@cchensh

cchensh commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Looks good overall. One design point before approving: the lock only guards AutoUpdate, while the foreground self-update in update.go:91 (dependency.InstallUpdate, used by slack upgrade and the upgrade prompt) still installs without it. A foreground upgrade and a background one can still swap the binary at the same time. Could InstallUpdate, or the foreground path, take the same autoupdate.lock (blocking or skipping)? Then "only one update installs at a time" holds for every path.

Nits: From/To get a v prefix while the CLI prints versions without one elsewhere (cli.go:79-80), so the future report would need to strip it. And lock.Unlock() errors are dropped without a debug log.

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Appreciate this one @zimeg, the tests that check the lock is held during the install and released after make the flow easy to trust.

}
// The lock is an OS file lock, so it's released if this process exits early. The lock file
// stays on disk, since removing it could let another update lock a new file at the same path.
lock := flock.New(filepath.Join(configDir, autoUpdateLockFileName))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Since the lock lives in the config dir, two commands with different --config-dir values could both swap the same binary, and slack upgrade installs without taking it. Maybe lock next to the binary and take it in slack upgrade too, here or in a follow-up?


// newDownloadClient returns the HTTP client that downloads the CLI
func newDownloadClient() *http.Client {
return &http.Client{Timeout: downloadTimeout}

@mwbrooks mwbrooks Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Timeout covers reading the whole body too, so a working slack upgrade download slower than about 50 KB/s fails at 10 minutes. A ResponseHeaderTimeout plus a stall deadline might catch the hung case without cutting off slow connections.

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Thanks @zimeg! Checking the lock from inside the install stub is a really clean way to prove the critical section.
One blocking comment on a back-to-back reinstall, the rest are optional or can move to the next PR 🙏

  • if err := dependency.InstallUpdate(ctx); err != nil {
    : 🔭 suggestion (non-blocking): The interactive self-update still calls InstallUpdate without the lock, so once the background command lands it can race an AutoUpdate in another terminal. Worth taking the same lock here, in this PR or the next.

_ = lock.Unlock()
}()

cli := NewCLIDependency(clients, currentVersion)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧠 issue (blocking): The lock stops overlapping installs, but not one that starts right after another finishes. A process still on the old version takes the freed lock, installs the same target again, and backupBinary files the new binary under backups/<old version>, replacing the real backup. Could we check under the lock whether the target is already installed (or already recorded) and skip, with a test for that sequential case?


// downloadTimeout limits how long a download of the CLI can take, so a stalled download
// doesn't hold the update lock or keep a background update running
const downloadTimeout = 10 * time.Minute

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 suggestion (non-blocking): This timeout also bounds slack upgrade, and http.Client.Timeout includes reading the body, so a slow link that used to finish now fails at 10 minutes. Passing ctx into download with http.NewRequestWithContext and letting the background command set the deadline would leave the interactive path as it was.

}

// newDownloadClient returns the HTTP client that downloads the CLI
func newDownloadClient() *http.Client {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪞 suggestion (non-blocking): The package already builds clients with api.NewHTTPClient (update.go). Using it here with TotalTimeOut: 10 * time.Minute keeps one way to make a client.

}
}

func Test_newDownloadClient(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 suggestion (non-blocking): This checks the client's field, but it would still pass if download stopped using newDownloadClient. Calling download against an httptest server would cover the real path.

if err != nil {
return err
}
// The lock is an OS file lock, so it's released if this process exits early. The lock file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 suggestion (non-blocking): The doc and inline comments here mostly restate the names. The lock-file note is a real invariant, so a test that the file stays after unlock would hold it better than prose.

assert.Equal(t, "4.12.0", c.releaseInfo.Version)
locked, err := flock.New(lockPath).TryLock()
require.NoError(t, err)
assert.False(t, locked, "the lock is held while installing")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Asserting the lock is held from inside the install stub proves the critical section, not just the outcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement M-T: A feature request for new functionality semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants