Repository: pytorch
Issue: Sending tensors from short-lived multiprocessing process fails on Linux
Labels: module: multiprocessing,triaged

## 🐛 Bug

Sending a Tensor from a child process to a parent process through a `torch.multiprocessing.Queue` results in an exception if the child process exits before the parent process decodes the object. This only occurs on Linux.

## To Reproduce

Steps to reproduce the behavior:

1. Create a file with the code snippet shown below
2. Run the file
3. The exception below will occur

Code:

```
import torch.multiprocessing as mp
import torch as th

print(mp.get_all_sharing_strategies())
print(mp.get_sharing_strategy())
# mp.set_sharing_strategy("file_system")

def proc_fn(q):
    for x in range(333):
        q.put((th.randn(3, 5), None))
    q.put((None, StopIteration()))

q = mp.Queue(maxsize=100)
proc = mp.Process(target=proc_fn, args=(q,), daemon=True)
proc.start()

while True:
    x = q.get()
    if x[1] is not None:
        break
```

Output:

```
{'file_descriptor', 'file_system'}
file_descriptor
Traceback (most recent call last):
  File "foo.py", line 18, in <module>
    x = q.get()
  File "/opt/conda/lib/python3.7/multiprocessing/queues.py", line 113, in get
    return _ForkingPickler.loads(res)
  File "/opt/conda/lib/python3.7/site-packages/torch/multiprocessing/reductions.py", line 289, in rebuild_storage_fd
    fd = df.detach()
  File "/opt/conda/lib/python3.7/multiprocessing/resource_sharer.py", line 57, in detach
    with _resource_sharer.get_connection(self._id) as conn:
  File "/opt/conda/lib/python3.7/multiprocessing/resource_sharer.py", line 87, in get_connection
    c = Client(address, authkey=process.current_process().authkey)
  File "/opt/conda/lib/python3.7/multiprocessing/connection.py", line 492, in Client
    c = SocketClient(address)
  File "/opt/conda/lib/python3.7/multiprocessing/connection.py", line 619, in SocketClient
    s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
```

## Expected behavior

The parent process should be able to receive and decode the tensors, and then exit gracefully. This is what happens if I manually set `mp.set_sharing_strategy("file_system")`.

## Environment

```
PyTorch version: 1.9.1+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.11.1
Libc version: glibc-2.10

Python version: 3.7.3 (default, Mar 27 2019, 22:11:17)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-65-generic-x86_64-with-debian-buster-sid
Is CUDA available: True
CUDA runtime version: 11.3.109
GPU models and configuration: GPU 0: Tesla V100-SXM2-32GB
Nvidia driver version: 450.102.04
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.2.0
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] mypy==0.910
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.0
[pip3] pytorch-memlab==0.2.3
[pip3] torch==1.9.1
[pip3] torch-bst==0.0.0
[pip3] torchvision==0.10.0
[conda] mypy                      0.910                    pypi_0    pypi
[conda] mypy-extensions           0.4.3                    pypi_0    pypi
[conda] numpy                     1.21.0                   pypi_0    pypi
[conda] pytorch-memlab            0.2.3                    pypi_0    pypi
[conda] torch                     1.9.1                    pypi_0    pypi
[conda] torch-bst                 0.0.0                    pypi_0    pypi
[conda] torchvision               0.10.0                   pypi_0    pypi
```

 - PyTorch Version (e.g., 1.0): 1.9.1
 - OS (e.g., Linux): Linux
 - How you installed PyTorch (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): N/A
 - Python version: 3.7.3
 - CUDA/cuDNN version: 11.3.109
 - GPU models and configuration: NVIDIA Tesla V100
 - Any other relevant information: N/A

## Additional context

 * This does not occur on macOS, since `file_descriptor` is not a multiprocessing sharing strategy.
 * Occasionally this script gives an `EOFError` instead of a `FileNotFoundError`.


cc @VitalyFedyunin