Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry writing to cgroup files on EINTR error #2258

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion libcontainer/cgroups/fscommon/fscommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ package fscommon

import (
"io/ioutil"
"os"
"syscall"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func WriteFile(dir, file, data string) error {
Expand All @@ -17,7 +20,7 @@ func WriteFile(dir, file, data string) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(path, []byte(data), 0700); err != nil {
if err := retryingWriteFile(path, []byte(data), 0700); err != nil {
return errors.Wrapf(err, "failed to write %q to %q", data, path)
}
return nil
Expand All @@ -34,3 +37,24 @@ func ReadFile(dir, file string) (string, error) {
data, err := ioutil.ReadFile(path)
return string(data), err
}

func retryingWriteFile(filename string, data []byte, perm os.FileMode) error {
for {
err := ioutil.WriteFile(filename, data, perm)
if isInterruptedWriteFile(err) {
logrus.Infof("interrupted while writing %s to %s", string(data), filename)
continue
}
return err
}
}

func isInterruptedWriteFile(err error) bool {
if patherr, ok := err.(*os.PathError); ok {
errno, ok2 := patherr.Err.(syscall.Errno)
if ok2 && errno == syscall.EINTR {
return true
}
}
return false
}
35 changes: 35 additions & 0 deletions libcontainer/cgroups/fscommon/fscommon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build linux

package fscommon

import (
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"
)

func TestWriteCgroupFileHandlesInterrupt(t *testing.T) {
memoryCgroupMount, err := cgroups.FindCgroupMountpoint("", "memory")
if err != nil {
t.Fatal(err)
}

cgroupName := fmt.Sprintf("test-eint-%d", time.Now().Nanosecond())
cgroupPath := filepath.Join(memoryCgroupMount, cgroupName)
if err := os.MkdirAll(cgroupPath, 0755); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(cgroupPath)

for i := 0; i < 100000; i++ {
limit := 1024*1024 + i
if err := WriteFile(cgroupPath, "memory.limit_in_bytes", strconv.Itoa(limit)); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

t.Skip() on cgroup v2 env

Copy link
Contributor

Choose a reason for hiding this comment

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

added the skip in the beginning of the test

t.Fatalf("Failed to write %d on attempt %d: %+v", limit, i, err)
}
}
}