test/test_static_runtime.py: test_fork_wait_4 sometimes deadlocks
Labels: module: tests, triaged, module: multithreading

### 🐛 Describe the bug

Test `test_fork_wait_4` from `test/test_static_runtime.py` sometimes deadlocks itself.

Test spawns children threads, which in turn spawn grandchildren threads. Those children threads wait for grandchildren before exiting.

If there are more children threads than available threads in threadpool, due to scheduling sometimes it may happen that all threads in threadpool are occupied by children threads waiting for grandchildren threads, and no grandchildren threads may be started due to no free threads in threadpool.

If it doesn't reproduce for you due to having more than 10 threads available in threadpool, increasing children and grandchildren threads count may help to trigger issue, like this:

<details>
<summary>New test diff</summary>

```
diff --git a/test/test_static_runtime.py b/test/test_static_runtime.py
index 032e6776407..16d628d4a8f 100644
--- a/test/test_static_runtime.py
+++ b/test/test_static_runtime.py
@@ -323,6 +323,36 @@ class TestStaticModule(TestCase):
         output_test.wait()
         torch.testing.assert_close(output_test.value(), output_ref)
 
+    """
+    Test Case: To test fork/wait operation in a graph on
+    multiple nested fork/wait operations with a lot of threads
+    """
+    def test_fork_wait_5(self):
+        input = torch.ones(3, 3)
+        num_forks = 100
+        num_child_forks = 100
+        torch_graph = torch.jit.script(fork_wait_graph4)
+        static_runtime_module = StaticModule(torch_graph)
+        output_ref = torch_graph(input, num_forks, num_child_forks)
+        output_test = static_runtime_module(input, num_forks, num_child_forks)
+        torch.testing.assert_close(output_test, output_ref)
+
+    """
+    Test Case: To test fork/wait operation in a graph with multiple
+    nested fork/wait operations on runAsync API returning future
+    """
+    def test_fork_wait_5_async(self):
+        input = torch.ones(3, 3)
+        num_forks = 100
+        num_child_forks = 100
+        torch_graph = torch.jit.script(fork_wait_graph4)
+        static_runtime_module = StaticModule(torch_graph)
+        output_ref = torch_graph(input, num_forks, num_child_forks)
+        output_test = static_runtime_module.runAsync(
+            (input, num_forks, num_child_forks), {})
+        output_test.wait()
+        torch.testing.assert_close(output_test.value(), output_ref)
+
     """
     Test Case: To test exception handling in fork/wait
     operation. Add.Tensor op is called for tensors with

```
</details>

Reproduces on current main branch.

### Versions

Collecting environment information...
PyTorch version: 2.1.0a0+gitefc7c36
Is debug build: True
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Gentoo Linux (x86_64)
GCC version: (Gentoo 12.3.1_p20230526 p2) 12.3.1 20230526
Clang version: 16.0.6
CMake version: version 3.26.5
Libc version: glibc-2.37

Python version: 3.11.4 (main, Aug 17 2023, 22:32:09) [GCC 11.3.1 20230427] (64-bit runtime)
Python platform: Linux-4.18.0-477.21.1.el8_8.x86_64-x86_64-11th_Gen_Intel-R-_Core-TM-_i7-1165G7_@_2.80GHz-with-glibc2.37
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):                          8
On-line CPU(s) list:             0-7
Vendor ID:                       GenuineIntel
Model name:                      11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
CPU family:                      6
Model:                           140
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
Stepping:                        1
CPU(s) scaling MHz:              54%
CPU max MHz:                     4700.0000
CPU min MHz:                     400.0000
BogoMIPS:                        5606.40
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       192 KiB (4 instances)
L1i cache:                       128 KiB (4 instances)
L2 cache:                        5 MiB (4 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-7
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:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
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] mypy-extensions==1.0.0
[pip3] numpy==1.25.2
[pip3] torch==2.1.0a0+gitefc7c36
[conda] Could not collect

cc @VitalyFedyunin @mruberry