Here’s the output of gdb (with above script, except I forgot to type requires_grad=True there):
(gdb) r test.py
Starting program: /home/mtgd/cpp-test/env/bin/python test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7ffff4bc8700 (LWP 129784)]
[New Thread 0x7ffff43c7700 (LWP 129785)]
[New Thread 0x7fffefbc6700 (LWP 129786)]
[Thread 0x7fffefbc6700 (LWP 129786) exited]
[Thread 0x7ffff43c7700 (LWP 129785) exited]
[Thread 0x7ffff4bc8700 (LWP 129784) exited]
[Detaching after fork from child process 129787]
/usr/lib/../share/gcc-9.2.0/python/libstdcxx/v6/xmethods.py:731: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
  refcounts = ['_M_refcount']['_M_pi']
[New Thread 0x7fffefbc6700 (LWP 129790)]
[New Thread 0x7ffff43c7700 (LWP 129791)]
^C    <----- hangs here
Thread 1 "python" received signal SIGINT, Interrupt.
0x00007ffff7a81c45 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0