libfetch: fix races in the cache connection code.

Tested by @Gottox.