Skip to content

Commit 2133709

Browse files
committed
Fix kubelet stuck issue due to hung fs #125298
- kubelet is stuck when restarted with hung fs - timeout the context when checking for getvfsstats Fix go formatting
1 parent 54dff2b commit 2133709

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

fs/fs.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package fs
2020

2121
import (
2222
"bufio"
23+
"context"
2324
"fmt"
2425
"os"
2526
"os/exec"
@@ -29,6 +30,7 @@ import (
2930
"strconv"
3031
"strings"
3132
"syscall"
33+
"time"
3234

3335
zfs "github.com/mistifyio/go-zfs"
3436
mount "github.com/moby/sys/mountinfo"
@@ -716,16 +718,41 @@ func (i *RealFsInfo) GetDirUsage(dir string) (UsageInfo, error) {
716718
}
717719

718720
func getVfsStats(path string) (total uint64, free uint64, avail uint64, inodes uint64, inodesFree uint64, err error) {
719-
var s syscall.Statfs_t
720-
if err = syscall.Statfs(path, &s); err != nil {
721-
return 0, 0, 0, 0, 0, err
722-
}
723-
total = uint64(s.Frsize) * s.Blocks
724-
free = uint64(s.Frsize) * s.Bfree
725-
avail = uint64(s.Frsize) * s.Bavail
726-
inodes = uint64(s.Files)
727-
inodesFree = uint64(s.Ffree)
728-
return total, free, avail, inodes, inodesFree, nil
721+
// timeout the context with, default is 2sec
722+
timeout := 2
723+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
724+
defer cancel()
725+
726+
type result struct {
727+
total uint64
728+
free uint64
729+
avail uint64
730+
inodes uint64
731+
inodesFree uint64
732+
err error
733+
}
734+
735+
resultChan := make(chan result, 1)
736+
737+
go func() {
738+
var s syscall.Statfs_t
739+
if err = syscall.Statfs(path, &s); err != nil {
740+
total, free, avail, inodes, inodesFree = 0, 0, 0, 0, 0
741+
}
742+
total = uint64(s.Frsize) * s.Blocks
743+
free = uint64(s.Frsize) * s.Bfree
744+
avail = uint64(s.Frsize) * s.Bavail
745+
inodes = uint64(s.Files)
746+
inodesFree = uint64(s.Ffree)
747+
resultChan <- result{total: total, free: free, avail: avail, inodes: inodes, inodesFree: inodesFree, err: err}
748+
}()
749+
750+
select {
751+
case <-ctx.Done():
752+
return 0, 0, 0, 0, 0, ctx.Err()
753+
case res := <-resultChan:
754+
return res.total, res.free, res.avail, res.inodes, res.inodesFree, res.err
755+
}
729756
}
730757

731758
// Devicemapper thin provisioning is detailed at

0 commit comments

Comments
 (0)