Repository: pytorch
Issue: Multiprocessing with workers returning `torch.Tensor` and limiting of the number of tasks per worker (`maxtasksperchild=1`) hangs
Labels: module: multiprocessing,triaged

### 🐛 Describe the bug

When running the below code the python interpreter hangs:
```
from multiprocessing import get_context
from torch import randn

def worker(i):
    result = randn(1)
    print(f'Task {i} result is {result}')
    # Doesn't work
    return result
    # # This works
    # return result.detach().numpy()

if __name__ == '__main__':
    # Doesn't work
    with get_context('fork').Pool(1, maxtasksperchild=1) as exe:
    # # This works
    # with get_context('fork').Pool(1, maxtasksperchild=None) as exe:
        results = exe.map(worker, range(10), chunksize=1)
```

The printouts to the terminal show that all the workers have completed, so the problem is related to collecting the data returned from the workers.

Note that if we return a `numpy.array` instead of a `torch.Tensor` or omit the `maxtasksperchild` parameter (its default value is `None`) everything works without hanging.



### Versions

PyTorch version: 2.4.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.1 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.11.7 (main, Dec 15 2023, 18:12:31) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      39 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             16
On-line CPU(s) list:                0-15
Vendor ID:                          GenuineIntel
Model name:                         12th Gen Intel(R) Core(TM) i7-1260P
CPU family:                         6
Model:                              154
Thread(s) per core:                 2
Core(s) per socket:                 8
Socket(s):                          1
Stepping:                           3
BogoMIPS:                           4992.01
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni umip waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities
Virtualization:                     VT-x
Hypervisor vendor:                  Microsoft
Virtualization type:                full
L1d cache:                          384 KiB (8 instances)
L1i cache:                          256 KiB (8 instances)
L2 cache:                           10 MiB (8 instances)
L3 cache:                           18 MiB (1 instance)
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] flake8==6.0.0
[pip3] mypy==1.11.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] numpydoc==1.5.0
[pip3] torch==2.4.0
[pip3] torch-arima==0.0.4
[pip3] torchaudio==2.4.0
[pip3] torchvision==0.19.0
[pip3] triton==2.3.0
[conda] _anaconda_depends         2024.02             py311_mkl_1
[conda] blas                      1.0                         mkl
[conda] cpuonly                   2.0                           0    pytorch
[conda] cudatoolkit               11.8.0               h6a678d5_0
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344
[conda] mkl-service               2.4.0           py311h5eee18b_1
[conda] mkl_fft                   1.3.8           py311h5eee18b_0
[conda] mkl_random                1.2.4           py311hdb19cb5_0
[conda] numpy                     1.26.4          py311h08b1b3b_0
[conda] numpy-base                1.26.4          py311hf175353_0
[conda] numpydoc                  1.5.0           py311h06a4308_0
[conda] pytorch                   2.4.0              py3.11_cpu_0    pytorch
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] torch                     2.3.0                    pypi_0    pypi
[conda] torch-arima               0.0.4                    pypi_0    pypi
[conda] torchaudio                2.4.0                 py311_cpu    pytorch
[conda] torchvision               0.19.0                py311_cpu    pytorch
[conda] triton                    2.3.0                    pypi_0    pypi

cc @VitalyFedyunin