<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">PyTorch programs that use autograd crash on exit with Python 3.9.0a6. The underlying cause is the same as <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="615936766" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/38228" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/38228/hovercard" href="https://github.com/pytorch/pytorch/issues/38228">#38228</a>, but changes to the implementation of <code class="notranslate">PyEval_RestoreThread</code> means this happens much more frequently in Python 3.9.0a6 than with previous Python versions.</p>
<p dir="auto">In previous Python releases, threads that called <code class="notranslate">PyEval_RestoreThread</code> after the interpreter shutdown would deadlock. Now they call <code class="notranslate">pthread_exit(0)</code>, which triggers the issue in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="615936766" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/38228" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/38228/hovercard" href="https://github.com/pytorch/pytorch/issues/38228">#38228</a>.</p>
<p dir="auto">The implementation of <code class="notranslate">PyEval_RestoreThread</code> has changed between alpha releases of 3.9.0 and may change in the future, so we might not want to rush any fixes.</p>
<p dir="auto">The crash happens on exit with a message like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="terminate called without an active exception
Aborted"><pre class="notranslate"><code class="notranslate">terminate called without an active exception
Aborted
</code></pre></div>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
torch.set_num_threads(1)  # optional
x = torch.randn(10, requires_grad=True)
x.sum().backward()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-s1">torch</span>.<span class="pl-en">set_num_threads</span>(<span class="pl-c1">1</span>)  <span class="pl-c"># optional</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">10</span>, <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">x</span>.<span class="pl-en">sum</span>().<span class="pl-en">backward</span>()</pre></div>
<p dir="auto">Tested on PyTorch master (<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/324dc1623e2f91892038fb1b151450a7c6529dd9/hovercard" href="https://github.com/pytorch/pytorch/commit/324dc1623e2f91892038fb1b151450a7c6529dd9"><tt>324dc16</tt></a>) and Python 3.9.0a6.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gchanan/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gchanan">@gchanan</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zou3519/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zou3519">@zou3519</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/bdhirsh/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/bdhirsh">@bdhirsh</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jbschlosser/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jbschlosser">@jbschlosser</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/albanD/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/albanD">@albanD</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gqchen/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gqchen">@gqchen</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/pearu/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/pearu">@pearu</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nikitaved/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/nikitaved">@nikitaved</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/soulitzer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/soulitzer">@soulitzer</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ssnl/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ssnl">@ssnl</a></p>
<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">A simple code segfaults in nightly builds with python 3.9 but works with python 3.8</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Content of <code class="notranslate">Dockerfile</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="FROM python:3.9-slim-buster
# works with 3.8
USER root
WORKDIR /root
RUN apt-get update &amp;&amp; apt-get install git build-essential --yes
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install --pre torch==1.8.0.dev20210102+cpu  -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
RUN python3 -m pip install Pillow
RUN git clone https://github.com/pytorch/vision/
ENV PYTHONPATH=./vision
COPY test-backbone.py /
RUN python /test-backbone.py"><pre class="notranslate"><code class="notranslate">FROM python:3.9-slim-buster
# works with 3.8
USER root
WORKDIR /root
RUN apt-get update &amp;&amp; apt-get install git build-essential --yes
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install --pre torch==1.8.0.dev20210102+cpu  -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
RUN python3 -m pip install Pillow
RUN git clone https://github.com/pytorch/vision/
ENV PYTHONPATH=./vision
COPY test-backbone.py /
RUN python /test-backbone.py
</code></pre></div>
<p dir="auto">Content of <code class="notranslate">test-backbone.py</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from torchvision.models import resnet50
import torch


if __name__ == &quot;__main__&quot;:
    backbone = resnet50(pretrained=True)

    img = torch.randn((2, 3, 256, 288))
    features = backbone(img)
    loss = features.sum()
    loss.backward()
    print(&quot;exiting&quot;)"><pre class="notranslate"><code class="notranslate">from torchvision.models import resnet50
import torch


if __name__ == "__main__":
    backbone = resnet50(pretrained=True)

    img = torch.randn((2, 3, 256, 288))
    features = backbone(img)
    loss = features.sum()
    loss.backward()
    print("exiting")
</code></pre></div>
<p dir="auto">Run <code class="notranslate">docker build  -t py39:v0 .</code></p>
<p dir="auto">Logs:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Step 11/11 : RUN python /test-backbone.py
 ---&gt; Running in 31a88ed05b3d
Downloading: &quot;https://download.pytorch.org/models/resnet50-19c8e357.pth&quot; to /root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth
100.0%
exiting
Segmentation fault (core dumped)
The command '/bin/sh -c python /test-backbone.py' returned a non-zero code: 139
"><pre class="notranslate"><code class="notranslate">Step 11/11 : RUN python /test-backbone.py
 ---&gt; Running in 31a88ed05b3d
Downloading: "https://download.pytorch.org/models/resnet50-19c8e357.pth" to /root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth
100.0%
exiting
Segmentation fault (core dumped)
The command '/bin/sh -c python /test-backbone.py' returned a non-zero code: 139

</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gchanan/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gchanan">@gchanan</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zou3519/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zou3519">@zou3519</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/bdhirsh/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/bdhirsh">@bdhirsh</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jbschlosser/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jbschlosser">@jbschlosser</a></p>