Repository: pytorch
Issue: profiler crashes the program when trying to stop on different threads without context manager
Labels: oncall: distributed,module: multithreading,oncall: profiler

## 🐛 Bug

The profiler crashes the program when attempting to stop profilers on different threads.

## To Reproduce

```python

import threading
import time

from torch.profiler import ProfilerActivity, profile


def basic_add():
    p1 = profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA])
    p1.start()
    x = torch.rand(10, 10).to_sparse() + torch.rand(10, 10).to_sparse()
    p1.stop()

def basic_add2():
    p2 = profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA])
    p2.start()
    x = torch.rand(10, 10).to_sparse() + torch.rand(10, 10).to_sparse()
    p2.stop()

def main():
    threads = []
    for i in range(2):
        if i == 0:
            threads.append(threading.Thread(target=basic_add))
        else:
            threads.append(threading.Thread(target=basic_add2))
        threads[i].start()
    for i in range(2):
        threads[i].join()



if __name__ == "__main__":
    main()

```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->


```
Thread 18 "python" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffef2324700 (LWP 32575)]
0x00007fffbdd3ad7c in libkineto::ActivityProfiler::processTraceInternal(libkineto::ActivityLogger&) () from /fsx/users/gcramer/work/pytorch/torch/lib/libtorch_cpu.so
```

## Expected behavior

We can stop the profilers.

## Environment

Collecting environment information...
PyTorch version: 1.10.0a0+gite133919
Is debug build: False
CUDA used to build PyTorch: 11.1
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.21.1

Python version: 3.6 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 11.1.105
GPU models and configuration: 
GPU 0: A100-SXM4-40GB
GPU 1: A100-SXM4-40GB
GPU 2: A100-SXM4-40GB
GPU 3: A100-SXM4-40GB
GPU 4: A100-SXM4-40GB
GPU 5: A100-SXM4-40GB
GPU 6: A100-SXM4-40GB
GPU 7: A100-SXM4-40GB

Nvidia driver version: 450.119.03
cuDNN version: Probably one of the following:
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A


## Additional context

<!-- Add any other context about the problem here. -->


cc @pietern @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @SciPioneer @H-Huang @cbalioglu @gcramer23 @ilia-cher @robieta @chaekit @gdankel @bitfort @ngimel @orionr @nbcsm @guotuofeng @guyang3532 @gaoteng-git