Repository: pytorch
Issue: interaction with psychopy during imports, script exits with: free(): invalid pointer. Aborted (core dumped)
Labels: triaged

### 🐛 Describe the bug

I'm using `xvfb-run` to run code that uses pytorch and psychopy (which requires an attached display).

When I run script with no errors that imports `torch` and `psychopy`, the script will run until the end, and then exit with an error if psychopy was imported first.

---
This version exits with an error:
```python
from psychopy import visual
import torch

import time
print("begin")
time.sleep(1)
print("end")
```

```shell
$ xvfb-run python scrap.py
begin
end
free(): invalid pointer
Aborted (core dumped)
```
---
This version works fine:
```python
import torch
from psychopy import visual

import time
print("begin")
time.sleep(1)
print("end")
```

```shell
$ xvfb-run python script.py
begin
end
```
---
To reproduce easily, you can setup a fresh virtualenv and install the following frozen requirements:
```shell
python3 -m venv tmp_venv
source tmp_venv/bin/activate
pip install -r frozen_requirements.txt --no-deps
```

Here's the necessary contents for `frozen_requirements.txt`:
```shell
arabic-reshaper==2.1.3
astunparse==1.6.3
attrs==21.4.0
cachetools==5.0.0
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.12
cryptography==36.0.2
cycler==0.11.0
decorator==4.4.2
esprima==4.0.1
et-xmlfile==1.1.0
fonttools==4.32.0
freetype-py==2.2.0
future==0.18.2
gevent==21.12.0
gitdb==4.0.9
GitPython==3.1.27
glfw==2.5.3
google-api-core==2.7.2
google-auth==2.6.5
google-cloud==0.34.0
google-cloud-speech==2.13.1
googleapis-common-protos==1.56.0
greenlet==1.1.2
idna==3.3
imageio==2.16.2
imageio-ffmpeg==0.4.7
jedi==0.18.1
json-tricks==3.15.5
kiwisolver==1.4.2
markdown-it-py==2.0.1
matplotlib==3.5.1
mdurl==0.1.1
moviepy==1.0.3
msgpack==1.0.3
msgpack-numpy==0.4.7.1
numpy==1.22.3
opencv-python==4.5.5.64
openpyxl==3.0.9
packaging==21.3
pandas==1.4.2
parso==0.8.3
Pillow==9.1.0
proglog==0.1.9
proto-plus==1.20.3
protobuf==3.20.0
PsychoPy==2022.1.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pyglet==1.5.23
pyparsing==3.0.8
python-bidi==0.4.2
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
requests==2.27.1
rsa==4.8
scipy==1.8.0
six==1.16.0
smmap==5.0.0
SpeechRecognition==3.8.1
torch==1.11.0
tqdm==4.64.0
typing-extensions==4.1.1
urllib3==1.26.9
wxPython==4.1.1
zope.event==4.5.0
zope.interface==5.4.0
```

Please let me know if I can provide more useful info about this. Thanks!

### Versions

Collecting environment information...
PyTorch version: 1.11.0+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 10.0.0-4ubuntu1
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.8.10 (default, Mar 15 2022, 12:22:08)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-100-generic-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: NVIDIA TITAN RTX
GPU 1: NVIDIA TITAN RTX

Nvidia driver version: 465.19.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] msgpack-numpy==0.4.7.1
[pip3] numpy==1.22.3
[pip3] torch==1.11.0
[conda] Could not collect