【问题标题】:Python giving OSError: [Errno 27] File too large on very small files with enough disk space/memory availablePython 给出 OSError: [Errno 27] File too large on very small files with enough disk space/memory available
【发布时间】:2020-07-18 17:44:00
【问题描述】:

我有一个应用程序,它创建几个非常小的临时文件作为单元测试或特定功能的一部分。 我开始经常遇到“随机”错误OSError: [Errno 27] File too large。随机我的意思是问题有时会在重新启动时消失,或者如果我在一段时间后重新运行测试。我手动检查了 Mac 上的临时文件夹是否已清理并且有足够的内存/空间来创建这样的小文件。 (几个 GB 可用)此上下文中的小文件例如大小为 16384、58330、26502(以字节为单位)甚至更小。 Shutil.copyfile 用于创建这些文件,但在执行 os.link 时也会出现相同的错误,这应该占用磁盘上的最小空间。我用 os.link 替换了 shutil.copfile(如果可能)来测试它是否解决了问题,但效果是一样的。 在 Mac OS 上,当我进行开发时,经常在密集运行大量测试的随机时间后抛出此错误。但是,在 docker 镜像中运行时,错误一直存在。

错误sn-ps:

    @pytest.fixture(scope="module")
    def simulate_mirror(fixtures):
        """
        Thjs fixture creates a file/directory structure that simulates an offline PyPI mirror
        Used to test the `mirror://` bandersnatch integration for scanning the whole PyPI repository
        """
        from aura import mirror as amirror

        with tempfile.TemporaryDirectory(prefix="aura_test_mirror_") as mirror:
            pmirror = Path(mirror)
            assert pmirror.is_dir()
            os.mkdir(pmirror / "json")

            for pkg, pkg_files in MIRROR_FILES.items():
                # copy the package JSON metadata
                os.link(
                    fixtures.path(f"mirror/{pkg}.json"),
                    pmirror / "json" / pkg
                )

                for p in pkg_files:
                    os.makedirs(pmirror / p["path"])
                    os.link(
                        fixtures.path(f"mirror/{p['name']}"),
>                       os.fspath(pmirror / p["path"] / p["name"])
                    )
E                   OSError: [Errno 27] File too large: '/analyzer/tests/files/mirror/wheel-0.34.2-py2.py3-none-any.whl' -> '/tmp/aura_test_mirror_a6o5p8fn/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl'

tests/conftest.py:204: OSError
    def test_apip():
        # Test package taken from pip tests
        # https://github.com/pypa/pip/tree/master/tests/data/packages
        whl = Path(__file__).parent /'files' / 'simplewheel-1.0-py2.py3-none-any.whl'

        venv_dir = tempfile.mkdtemp(suffix="_pytest_aura_apip")
        # print(f'Virtualenv created in {venv_dir}')
        try:
            # Create virtualenv
            venv.create(
                env_dir=venv_dir,
>               with_pip=True,
                # symlinks=True
            )

tests/test_apip.py:56:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.7/venv/__init__.py:390: in create
    builder.create(env_dir)
/usr/local/lib/python3.7/venv/__init__.py:66: in create
    self.setup_python(context)
/usr/local/lib/python3.7/venv/__init__.py:233: in setup_python
    copier(context.executable, path)
/usr/local/lib/python3.7/venv/__init__.py:176: in symlink_or_copy
    shutil.copyfile(src, dst)
/usr/local/lib/python3.7/shutil.py:122: in copyfile
    copyfileobj(fsrc, fdst)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

fsrc = <_io.BufferedReader name='/usr/local/bin/python'>, fdst = <_io.BufferedWriter name='/tmp/tmpw5gbckwv_pytest_aura_apip/bin/python'>, length = 16384

    def copyfileobj(fsrc, fdst, length=16*1024):
        """copy data from file-like object fsrc to file-like object fdst"""
        while 1:
            buf = fsrc.read(length)
            if not buf:
                break
>           fdst.write(buf)
E           OSError: [Errno 27] File too large

/usr/local/lib/python3.7/shutil.py:82: OSError

使用venv.create 创建虚拟环境时有时也会抛出这些错误。我也总是在 docker 映像中收到 sqlite3.OperationalError: disk I/O error,这可能与同一问题有关。 更多技术信息: Mac OS Catalina,完全升级,通过 brew 重新安装 python 到最新的 3.7.7 + 重新创建了所有 virtualenv 并重新安装了所有依赖项。基于其他 SO 问题 (File too Large python),我已经检查了文件系统是否支持在限制范围内的文件大小以及目录中允许的最大文件数。 包含问题的最新提交(包括因错误而失败的 dockerfile):

https://github.com/RootLUG/aura/commit/b4c730693e8f7fd36ab2acc78997694002c4e345

触发错误的代码位置:

https://github.com/RootLUG/aura/blob/dev/tests/conftest.py#L181

https://github.com/RootLUG/aura/blob/dev/tests/test_apip.py#L54

来自单元测试的 Travis 日志:

https://travis-ci.org/github/RootLUG/aura/builds/671722230

【问题讨论】:

    标签: python docker filesize oserror


    【解决方案1】:

    它可以在 Docker 之外工作吗?我不熟悉他们如何在 Mac 上执行此操作,但至少在 Windows 上,我相信 Docker 在具有自身磁盘空间限制的 VM 中运行,因此可能 Docker 空间不足,这就是您遇到的问题。可能值得研究一下。

    【讨论】:

    • 不,它没有。有时错误出现在 docker (Mac virtualenv) 之外,但它的可预测性较差,因为有时它们会在重新启动或几分钟后重新运行测试时消失。 Docker 映像总是在我的系统和 travis 中产生错误,如所述。我也很难说,从过去的经验来看,我的 Linux 机器使用了 tempfs,它是一个驻留在内存中而不是磁盘上的虚拟文件系统,因此挂载目录或文件系统可能有些花哨。不幸的是,我不知道如何调试或进一步检查问题
    • 也许值得注意的是我尝试更改 fsize ulimit 但没有任何效果
    • 嗯。不知道还能告诉你什么,对此感到抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2016-03-02
    • 2022-10-06
    相关资源
    最近更新 更多