Application hangs for me due to race condition
talkingscott opened this issue · 3 comments
When I run the code as is, it never exits main, because the latch.await() is never satisfied. Using redis-cli monitor command, I see that the publish occurs before the subscription.
1581214604.709415 [0 127.0.0.1:61178] "PUBLISH" "chat" "Hello from Redis!" 1581214604.733783 [0 127.0.0.1:61179] "PSUBSCRIBE" "chat"
I confirmed that the container is alive and running before the send. I did not try to find a way to be notified when the subscribe is actually made. A sleep before the send was enough to change the behavior on my machine, but that's certainly not a true fix.
Some info about my setup:
MBP A1398 running OS X 10.12.6
Java 1.8.0_131
Spring Boot 2.2.4
Eclipse 2019-12 (I am running the app via Run As...)
Redis 3.2.0 (installed with brew)
It's tricky I guess. This is in no way a "real" app - sending yourself a message and exiting when you receive it is an extremely artificial use case. I guess we could mention the possibility of the race condition in the guide and suggest the same workaround. What else could we do? FWIW it works consistently for me.
I fixed it by re-implementing the listener. The latch is artificial, so it makes more sense just to count incoming messages and assert that there are more than 0.
Nice. So even if a send occurs before the subscribe, there will be another send coming up.
1581283613.138494 [0 127.0.0.1:58684] "PUBLISH" "chat" "Hello from Redis!"
1581283613.163155 [0 127.0.0.1:58685] "PSUBSCRIBE" "chat"
1581283613.647710 [0 127.0.0.1:58684] "PUBLISH" "chat" "Hello from Redis!"