//go:build linux package isolation import ( "errors" "os" "path/filepath" "syscall" ) // The kernel releases flock when the agent exits, including on a crash. Keep // the file on disk: unlinking a locked inode would let another request open a // different inode and acquire a second lock for the same version. func acquirePythonInstallLock(buildRoot, version string) (func(), error) { lockPath := filepath.Join(buildRoot, ".install-"+version+".flock") fd, err := syscall.Open(lockPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0600) if err != nil { return nil, err } file := os.NewFile(uintptr(fd), lockPath) info, err := file.Stat() if err != nil || !info.Mode().IsRegular() { _ = file.Close() return nil, errors.New("Python installation lock path is unsafe") } if err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB); err != nil { _ = file.Close() if errors.Is(err, syscall.EWOULDBLOCK) || errors.Is(err, syscall.EAGAIN) { return nil, errors.New("Python version installation is already running") } return nil, err } return func() { _ = file.Close() }, nil }