Repository: pytorch
Issue: Segfault in TCPStore and FileStore compare_set()
Labels: oncall: distributed

### 🐛 Describe the bug

I get a reliable crash when trying to use store.compare_set().  It appears to affect both TCPStore and FileStore.  My test suggests that compare_set is interpreting strings that are actually numbers incorrectly.  Here is the python code:
```
import torch                                                                                                                                                                                            
import torch.distributed as dist                                                                                                                                                                        
                                                                                                                                                                                                        
def main():                                                                                                                                                                                             
  torch.distributed.init_process_group(backend='nccl')                                                                                                                                                  
  #torch.distributed.init_process_group(backend='gloo')                                                                                                                                                 
  if dist.is_initialized():                                                                                                                                                                             
    print('enabled')                                                                                                                                                                                    
  else:                                                                                                                                                                                                 
    print('not enabled')                                                                                                                                                                                
  # Same issue with FileStore                                                                                                                                                                           
  server_store = dist.TCPStore("127.0.0.1", 1234, 1, True)                                                                                                                                              
  client_store = dist.TCPStore("127.0.0.1", 1234, 1, False)                                                                                                                                             
  server_store.set('good', 't0')                                                                                                                                                                        
  server_store.set('bad', '0')                                                                                                                                                                          
                                                                                                                                                                                                        
  # WORKS                                                                                                                                                                                               
  v = client_store.get("good")                                                                                                                                                                          
  print(v, type(v))                                                                                                                                                                                     
  w = int(v[1:]) + 1                                                                                                                                                                                    
  sw = f't{w}'                                                                                                                                                                                          
  client_store.compare_set("good", v, sw)                                                                                                                                                               
                                                                                                                                                                                                        
  # SEGFAULTS                                                                                                                                                                                           
  if True:                                                                                                                                                                                              
      v = client_store.get("bad")                                                                                                                                                                       
      print(v, type(v))                                                                                                                                                                                 
      w = int(v) + 1                                                                                                                                                                                    
      sw = f'{w}'                                                                                                                                                                                       
      client_store.compare_set("bad", v, sw)                                                                                                                                                            
                                                                                                                                                                                                        
if __name__ == "__main__":                                                                                                                                                                              
    main()                                                                                      ```                                                                                                        
```

Here is the run:
```
(hf) [jlquinn@cccxc561 madlad400-multigpu-translate]$ torchrun --nproc-per-node 1 storetest2.py
enabled
b't0' <class 'bytes'>
b'0' <class 'bytes'>
[2024-04-12 18:31:09,911] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: -11) local_rank: 0 (pid: 3657275) of binary: /dccstor/jlquinn01/miniforge3/envs/hf/bin/python
Traceback (most recent call last):
  File "/dccstor/jlquinn01/miniforge3/envs/hf/bin/torchrun", line 8, in <module>
    sys.exit(main())
  File "/dccstor/jlquinn01/miniforge3/envs/hf/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py", line 347, in wrapper
    return f(*args, **kwargs)
  File "/dccstor/jlquinn01/miniforge3/envs/hf/lib/python3.10/site-packages/torch/distributed/run.py", line 812, in main
    run(args)
  File "/dccstor/jlquinn01/miniforge3/envs/hf/lib/python3.10/site-packages/torch/distributed/run.py", line 803, in run
    elastic_launch(
  File "/dccstor/jlquinn01/miniforge3/envs/hf/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 135, in __call__
    return launch_agent(self._config, self._entrypoint, list(args))
  File "/dccstor/jlquinn01/miniforge3/envs/hf/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 268, in launch_agent
    raise ChildFailedError(
torch.distributed.elastic.multiprocessing.errors.ChildFailedError: 
=========================================================
storetest2.py FAILED
---------------------------------------------------------
Failures:
  <NO_OTHER_FAILURES>
---------------------------------------------------------
Root Cause (first observed failure):
[0]:
  time      : 2024-04-12_18:31:09
  host      : cccxc561.pok.ibm.com
  rank      : 0 (local_rank: 0)
  exitcode  : -11 (pid: 3657275)
  error_file: <N/A>
  traceback : Signal 11 (SIGSEGV) received by PID 3657275
=========================================================
(hf) [jlquinn@cccxc561 madlad400-multigpu-translate]$ 
```

### Versions

```
(hf) [jlquinn@cccxc561 madlad400-multigpu-translate]$ python collect_env.py 
Collecting environment information...
PyTorch version: 2.2.2+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Red Hat Enterprise Linux release 8.9 (Ootpa) (x86_64)
GCC version: (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20)
Clang version: Could not collect
CMake version: version 3.26.5
Libc version: glibc-2.28

Python version: 3.10.14 (main, Mar 21 2024, 16:24:04) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-4.18.0-513.11.1.el8_9.x86_64-x86_64-with-glibc2.28
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-SXM4-80GB
Nvidia driver version: 535.54.03
cuDNN version: Could not collect
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
Byte Order:          Little Endian
CPU(s):              128
On-line CPU(s) list: 0-127
Thread(s) per core:  1
Core(s) per socket:  64
Socket(s):           2
NUMA node(s):        2
Vendor ID:           AuthenticAMD
CPU family:          25
Model:               1
Model name:          AMD EPYC 7763 64-Core Processor
Stepping:            1
CPU MHz:             2450.000
CPU max MHz:         3529.0520
CPU min MHz:         1500.0000
BogoMIPS:            4900.40
Virtualization:      AMD-V
L1d cache:           32K
L1i cache:           32K
L2 cache:            512K
L3 cache:            32768K
NUMA node0 CPU(s):   0-63
NUMA node1 CPU(s):   64-127
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd amd_ppin brs arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.2.2
[pip3] triton==2.2.0
[conda] mkl                       2023.1.0         h213fc3f_46344  
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] torch                     2.2.2                    pypi_0    pypi
[conda] triton                    2.2.0                    pypi_0    pypi
(hf) [jlquinn@cccxc561 madlad400-multigpu-translate]$ 
```

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang