Python 3.7.2 在 asyncio.run() 关闭期间因未处理的异常而在构建期间测试失败

Python 3.7.2 tests failure during build from unhandled exception during asyncio.run() shutdown

我正在尝试在 Ubuntu 18.04 Docker 容器中构建 Python 3.7.2 并优化构建(--enable-optimizations--with-lto)。在此构建期间,测试(具体看起来像 test_sys_settrace)失败并显示

unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine

我不太清楚为什么会这样,搜索 unhandled exception during asyncio.run() shutdown 指出代码有问题,我在这里无法控制。我还应该注意,如果构建是针对 Python 3.6.8.

的,这可以正常工作,没有错误

接下来是我的相关部分 Dockerfile

FROM ubuntu:bionic

USER root
WORKDIR /root

SHELL [ "/bin/bash", "-c" ]

ARG PYTHON_VERSION_TAG=3.7.2

# Existing lsb_release causes issues with modern installations of Python3
# https://github.com/pypa/pip/issues/4924#issuecomment-435825490
# Set (temporarily) DEBIAN_FRONTEND to avoid interacting with tzdata
RUN apt-get -qq -y update && \
    apt-get -qq -y upgrade && \
    DEBIAN_FRONTEND=noninteractive apt-get -qq -y install \
        gcc \
        g++ \
        zlibc \
        zlib1g-dev \
        libssl-dev \
        libbz2-dev \
        libsqlite3-dev \
        libncurses5-dev \
        libgdbm-dev \
        libgdbm-compat-dev \
        liblzma-dev \
        libreadline-dev \
        uuid-dev \
        libffi-dev \
        tk-dev \
        wget \
        curl \
        git \
        make \
        sudo \
        bash-completion \
        tree \
        vim \
        software-properties-common && \
    mv /usr/bin/lsb_release /usr/bin/lsb_release.bak && \
    apt-get -y autoclean && \
    apt-get -y autoremove && \
    rm -rf /var/lib/apt-get/lists/*

ADD install_python.sh install_python.sh
RUN bash install_python.sh ${PYTHON_VERSION_TAG} && \
    rm -r install_python.sh Python-${PYTHON_VERSION_TAG}
# There is other stuff in the Dockerfile, such as setting up a non-root user, but not relevant here
CMD [ "/bin/bash" ]

并且对安装脚本进行了轻微编辑,使其仅包含相关部分 (install_python.sh)

#!/usr/bin/env bash

set -e

# This is being run as root and so sudo is not needed

CXX_VERSION="$(which gcc)"

function download_cpython () {
    # 1: the version tag
    printf "\n### Downloading CPython source as Python-.tgz\n"
    wget "https://www.python.org/ftp/python//Python-.tgz" &> /dev/null
    tar -xvzf "Python-.tgz" > /dev/null
    rm "Python-.tgz"
}

function build_cpython () {
    # 1: the prefix to be passed to configure
    #    c.f. https://docs.python.org/3/using/unix.html#python-related-paths-and-files
    # 2: the path to the version of gcc to be used
    # 3: the Python version being built

    # https://docs.python.org/3/using/unix.html#building-python
    # https://github.com/python/cpython/blob/3.7/README.rst
    printf "\n### ./configure\n"
    # --with-threads is removed in Python 3.7 (threading already on)
    ./configure --prefix="" \
        --exec_prefix="" \
        --with-cxx-main="" \
        --enable-optimizations \
        --with-lto \
        --enable-loadable-sqlite-extensions \
        --enable-ipv6 \
        CXX=""
    printf "\n### make -j4\n"
    make -j4
    printf "\n### make install\n"
    make install
}

function main() {
    # 1: the Python version tag

    PYTHON_VERSION_TAG=3.7.2

    if [[ $# -gt 0 ]]; then
        PYTHON_VERSION_TAG=""
    fi

    download_cpython "${PYTHON_VERSION_TAG}"
    cd Python-"${PYTHON_VERSION_TAG}"
    build_cpython /usr "${CXX_VERSION}" "${PYTHON_VERSION_TAG}"
}

main "$@" || exit 1

非常感谢对此问题的任何想法或反馈。

稍微冗长一点,docker build 的输出包含

0:21:41 load avg: 0.89 [335/416] test_sys_setprofile
0:21:42 load avg: 0.89 [336/416] test_sys_settrace
unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
^[[0m0:21:43 load avg: 0.89 [337/416] test_sysconfig
0:21:44 load avg: 0.89 [338/416] test_syslog
0:21:45 load avg: 0.89 [339/416] test_tarfile
# ...
# and then later
# ...
0:28:10 load avg: 1.41 [415/416] test_zipimport_support
0:28:12 load avg: 1.46 [416/416] test_zlib

Total duration: 28 min 14 sec
Tests result: FAILURE

编辑: 这也发生在 Python 3.7.3

虽然对于 Python 3.7 的版本为什么会发生这种情况我仍然没有一个好的答案,但我可以报告说这个问题似乎已在 Python 3.8.1. The Dockerfile and install_python.sh used (GitHub link for them here 中修复)基本上是与问题中的相同,只是标签更改为 3.8.1.

当在 ubuntu:bionic Docker 图像中构建时,测试报告成功。问题中的 RuntimeError 已解决

0:00:08 load avg: 6.49 [ 92/423] test_docxmlrpc passed
0:00:08 load avg: 6.49 [ 93/423] test_pickletools passed
0:00:08 load avg: 6.49 [ 94/423] test_contextlib_async passed
Task was destroyed but it is pending!
task: <Task pending name='Task-9' coro=<<async_generator_athrow without __name__>()>>
Task was destroyed but it is pending!
task: <Task pending name='Task-11' coro=<<async_generator_athrow without __name__>()>>
Task was destroyed but it is pending!
task: <Task pending name='Task-16' coro=<<async_generator_athrow without __name__>()>>
0:00:08 load avg: 6.49 [ 95/423] test_dynamic passed
0:00:08 load avg: 6.49 [ 96/423] test_userdict passed
0:00:08 load avg: 6.49 [ 97/423] test_sys_settrace passed
# ...
# and then later
# ...
0:02:42 load avg: 3.63 running: test_concurrent_futures (1 min 33 sec)
0:03:12 load avg: 3.09 running: test_concurrent_futures (2 min 3 sec)
0:03:26 load avg: 2.47 [423/423] test_concurrent_futures passed (2 min 17 sec)

== Tests result: SUCCESS ==

409 tests OK.

14 tests skipped:
    test_devpoll test_gdb test_ioctl test_kqueue test_msilib
    test_ossaudiodev test_startfile test_tix test_tk test_ttk_guionly
    test_winconsoleio test_winreg test_winsound test_zipfile64

Total duration: 3 min 26 sec
Tests result: SUCCESS

因为我看不出有什么理由不从 Python 3.7 升级到 Python 3.8(除非你使用的库还没有发布 Python 3.8 wheel yet) 我愿意说仅仅更新你的 Python runtime candidate 就足以 "fix" 将其标记为已解决。