Skip to content

Commit 0534f8f

Browse files
authored
fix(provisionersdk): use mtime instead of atime for session cleanup (#10893)
See #10892 - Updates provisionersdk session cleanup to use mtime instead of atime. - Also runs go mod tidy.
1 parent f28df8e commit 0534f8f

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ require (
9999
github.com/creack/pty v1.1.18
100100
github.com/dave/dst v0.27.2
101101
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
102-
github.com/djherbis/times v1.6.0
103102
github.com/elastic/go-sysinfo v1.11.0
104103
github.com/fatih/color v1.16.0
105104
github.com/fatih/structs v1.1.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WA
278278
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
279279
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
280280
github.com/dhui/dktest v0.3.16 h1:i6gq2YQEtcrjKbeJpBkWjE8MmLZPYllcjOFbTZuPDnw=
281-
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
282-
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
283281
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
284282
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
285283
github.com/docker/cli v23.0.5+incompatible h1:ufWmAOuD3Vmr7JP2G5K3cyuNC4YZWiAsuDEvFVVDafE=
@@ -1212,7 +1210,6 @@ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBc
12121210
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12131211
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12141212
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1215-
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12161213
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12171214
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12181215
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

provisionersdk/cleanup.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"path/filepath"
66
"time"
77

8-
"github.com/djherbis/times"
98
"github.com/spf13/afero"
109
"golang.org/x/xerrors"
1110

@@ -27,13 +26,9 @@ func CleanStaleSessions(ctx context.Context, workDirectory string, fs afero.Fs,
2726
if fi.IsDir() && isValidSessionDir(dirName) {
2827
sessionDirPath := filepath.Join(workDirectory, dirName)
2928

30-
accessTime := fi.ModTime() // fallback to modTime if accessTime is not available (afero)
31-
if fi.Sys() != nil {
32-
timeSpec := times.Get(fi)
33-
accessTime = timeSpec.AccessTime()
34-
}
29+
modTime := fi.ModTime() // fallback to modTime if modTime is not available (afero)
3530

36-
if accessTime.Add(staleSessionRetention).After(now) {
31+
if modTime.Add(staleSessionRetention).After(now) {
3732
continue
3833
}
3934

provisionersdk/cleanup_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ import (
1919

2020
const workDirectory = "/tmp/coder/provisioner-34/work"
2121

22+
var now = time.Date(2023, time.June, 3, 4, 5, 6, 0, time.UTC)
23+
2224
func TestStaleSessions(t *testing.T) {
2325
t.Parallel()
2426

25-
prepare := func() (afero.Fs, time.Time, slog.Logger) {
26-
fs := afero.NewMemMapFs()
27-
now := time.Date(2023, time.June, 3, 4, 5, 6, 0, time.UTC)
27+
prepare := func() (afero.Fs, slog.Logger) {
28+
tempDir := t.TempDir()
29+
fs := afero.NewBasePathFs(afero.NewOsFs(), tempDir)
2830
logger := slogtest.Make(t, nil).
2931
Leveled(slog.LevelDebug).
3032
Named("cleanup-test")
31-
return fs, now, logger
33+
return fs, logger
3234
}
3335

3436
t.Run("all sessions are stale", func(t *testing.T) {
@@ -37,7 +39,7 @@ func TestStaleSessions(t *testing.T) {
3739
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
3840
defer cancel()
3941

40-
fs, now, logger := prepare()
42+
fs, logger := prepare()
4143

4244
// given
4345
first := provisionersdk.SessionDir(uuid.NewString())
@@ -62,7 +64,7 @@ func TestStaleSessions(t *testing.T) {
6264
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
6365
defer cancel()
6466

65-
fs, now, logger := prepare()
67+
fs, logger := prepare()
6668

6769
// given
6870
first := provisionersdk.SessionDir(uuid.NewString())
@@ -86,7 +88,7 @@ func TestStaleSessions(t *testing.T) {
8688
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
8789
defer cancel()
8890

89-
fs, now, logger := prepare()
91+
fs, logger := prepare()
9092

9193
// given
9294
first := provisionersdk.SessionDir(uuid.NewString())
@@ -104,9 +106,9 @@ func TestStaleSessions(t *testing.T) {
104106
})
105107
}
106108

107-
func addSessionFolder(t *testing.T, fs afero.Fs, sessionName string, accessTime time.Time) {
109+
func addSessionFolder(t *testing.T, fs afero.Fs, sessionName string, modTime time.Time) {
108110
err := fs.MkdirAll(filepath.Join(workDirectory, sessionName), 0o755)
109111
require.NoError(t, err, "can't create session folder")
110-
fs.Chtimes(filepath.Join(workDirectory, sessionName), accessTime, accessTime)
112+
require.NoError(t, fs.Chtimes(filepath.Join(workDirectory, sessionName), now, modTime), "can't chtime of session dir")
111113
require.NoError(t, err, "can't set times")
112114
}

0 commit comments

Comments
 (0)