title: Python 3.7.2 tests failure during build from unhandled exception during asyncio.run() shutdown
body: <p>I'm trying to build <a href="https://www.python.org/downloads/release/python-372/" rel="noreferrer">Python 3.7.2</a> in a Ubuntu 18.04 Docker container with the build being optimized (<code>--enable-optimizations</code> and <code>--with-lto</code>). During this build, the tests (specifically it looks like <code>test_sys_settrace</code>) fail with</p>
  
  <pre class="lang-none prettyprint-override"><code>unhandled exception during asyncio.run() shutdown
  task: &lt;Task finished coro=&lt;&lt;async_generator_athrow without __name__&gt;()&gt; exception=RuntimeError("can't send non-None value to a just-started coroutine")&gt;
  RuntimeError: can't send non-None value to a just-started coroutine
  unhandled exception during asyncio.run() shutdown
  task: &lt;Task finished coro=&lt;&lt;async_generator_athrow without __name__&gt;()&gt; exception=RuntimeError("can't send non-None value to a just-started coroutine")&gt;
  RuntimeError: can't send non-None value to a just-started coroutine
  </code></pre>
  
  <p>It isn't very clear to me why this is happening, and searching on <code>unhandled exception during asyncio.run() shutdown</code> points to issues with the code, which I don't have control over here. I should also note that this works fine with no errors if the build is for Python 3.6.8.</p>
  
  <p>What follows it the relevant parts of my <code>Dockerfile</code></p>
  
  <pre class="lang-none prettyprint-override"><code>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 &amp;&amp; \
      apt-get -qq -y upgrade &amp;&amp; \
      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 &amp;&amp; \
      mv /usr/bin/lsb_release /usr/bin/lsb_release.bak &amp;&amp; \
      apt-get -y autoclean &amp;&amp; \
      apt-get -y autoremove &amp;&amp; \
      rm -rf /var/lib/apt-get/lists/*
  
  ADD install_python.sh install_python.sh
  RUN bash install_python.sh ${PYTHON_VERSION_TAG} &amp;&amp; \
      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" ]
  </code></pre>
  
  <p>and the install script lightly edited down to only include relevant parts (<code>install_python.sh</code>)</p>
  
  <pre class="lang-bsh prettyprint-override"><code>#!/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-${1}.tgz\n"
      wget "https://www.python.org/ftp/python/${1}/Python-${1}.tgz" &amp;&gt; /dev/null
      tar -xvzf "Python-${1}.tgz" &gt; /dev/null
      rm "Python-${1}.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="${1}" \
          --exec_prefix="${1}" \
          --with-cxx-main="${2}" \
          --enable-optimizations \
          --with-lto \
          --enable-loadable-sqlite-extensions \
          --enable-ipv6 \
          CXX="${2}"
      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="${1}"
      fi
  
      download_cpython "${PYTHON_VERSION_TAG}"
      cd Python-"${PYTHON_VERSION_TAG}"
      build_cpython /usr "${CXX_VERSION}" "${PYTHON_VERSION_TAG}"
  }
  
  main "$@" || exit 1
  </code></pre>
  
  <p>Any thoughts or feedback on this issue would be very much appreciated.</p>
  
  <p>To be slightly verbose, the output from <code>docker build</code> contains</p>
  
  <pre class="lang-none prettyprint-override"><code>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: &lt;Task finished coro=&lt;&lt;async_generator_athrow without __name__&gt;()&gt; exception=RuntimeError("can't send non-None value to a just-started coroutine")&gt;
  RuntimeError: can't send non-None value to a just-started coroutine
  unhandled exception during asyncio.run() shutdown
  task: &lt;Task finished coro=&lt;&lt;async_generator_athrow without __name__&gt;()&gt; exception=RuntimeError("can't send non-None value to a just-started coroutine")&gt;
  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
  </code></pre>
  
  <p><strong>Edit:</strong> This is also happening with <a href="https://www.python.org/downloads/release/python-373/" rel="noreferrer">Python 3.7.3</a></p>