Repository: pytorch
Issue: Tensor operation hangs when used with multiprocessing
Labels: module: multiprocessing,triaged,module: determinism,shadow review

### 🐛 Describe the bug

The bug is basically some strange interaction between Tensors and python's multiprocessing.

Minimum code:

```python
import multiprocessing as mp
import torch
def f(c): return c[None]-c[:,None]
p = mp.Pool()
print(p.apply_async(f, [torch.randn(105, 3)]).get(2).shape)
a = torch.tensor(torch.randn(476, 3).numpy().tolist())

print(a) # ------------------------ if comment out this line, then it doesn't time out, and everything works fine

p = mp.Pool()
print(p.apply_async(f, [torch.randn(104, 3)]).get(2).shape) # works
print(p.apply_async(f, [torch.randn(105, 3)]).get(2).shape) # times out
```

Output:

```
torch.Size([105, 105, 3])
tensor([[-0.5029, -0.4826,  0.7539],
        [-0.3531, -1.0151,  1.6901],
        [ 0.4097, -1.2270,  0.4938],
        ...,
        [ 1.0566,  0.1112, -1.1541],
        [-0.4986, -0.9533,  0.0470],
        [-0.3708,  0.8196, -0.7386]])
torch.Size([104, 104, 3])
---------------------------------------------------------------------------
TimeoutError                              Traceback (most recent call last)
Input In [1], in <cell line: 10>()
      8 p = mp.Pool()
      9 print(p.apply_async(f, [torch.randn(104, 3)]).get(2).shape)
---> 10 print(p.apply_async(f, [torch.randn(105, 3)]).get(2).shape)

File ~/anaconda3/envs/torch/lib/python3.8/multiprocessing/pool.py:767, in ApplyResult.get(self, timeout)
    765 self.wait(timeout)
    766 if not self.ready():
--> 767     raise TimeoutError
    768 if self._success:
    769     return self._value

TimeoutError: 
```

So, for some reason when I print out the tensor `a`, multiprocessing hangs when I do an operation with a 105x3 tensor, but does not when I do the same operation with a 104x3 tensor. However, when I don't print out the tensor `a`, multiprocessing does not hang for both 104x3 and 105x3 tensors.

This was originally observed on a JupyterLab environment, but I have tested the code on the vanilla python interpreter. Still same issue.

### Versions

Collecting environment information...
PyTorch version: 1.10.0
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Linux Mint 20.3 (x86_64)
GCC version: (Ubuntu 8.4.0-3ubuntu2) 8.4.0
Clang version: 8.0.1-9 (tags/RELEASE_801/final)
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-122-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 470.129.06
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.21.2
[pip3] numpy-stl==2.17.1
[pip3] pytorch-sphinx-theme==0.0.19
[pip3] torch==1.10.0
[pip3] torchaudio==0.10.0
[pip3] torchvision==0.11.1
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0            py38h95df7f1_0    conda-forge
[conda] mkl_fft                   1.3.1            py38h8666266_1    conda-forge
[conda] mkl_random                1.2.2            py38h1abd341_0    conda-forge
[conda] numpy                     1.21.2           py38h20f2e39_0  
[conda] numpy-base                1.21.2           py38h79a1101_0  
[conda] numpy-stl                 2.17.1                   pypi_0    pypi
[conda] pytorch                   1.10.0          py3.8_cuda11.3_cudnn8.2.0_0    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] pytorch-sphinx-theme      0.0.19                   pypi_0    pypi
[conda] torch                     1.10.0                   pypi_0    pypi
[conda] torchaudio                0.10.0               py38_cu113    pytorch
[conda] torchvision               0.10.0                   pypi_0    pypi


cc @VitalyFedyunin @mruberry @kurtamohler @ezyang