That example code was intended to run on a single node alone. If you use it on multiple nodes, there are at least two problems:

Incorrect world_size and rank: Say you run this on two different nodes. Then each would try to spawn two processes. So that you will have four process group instances, but the world_size is still two, and rank is 0, 1, 0, 1, instead of 0, 1, 2, 3, which would mess up the rendezvous algorithm. To fix this, you need to pass in correct rank and world_size for each example(rank, world_size) invcation.
Can you change the MASTER_ADDR to use the same IP instead of master is using localhost and others are using master ip?