Fix a segfault cleaning up persistent sockets

This is a fix for #3074 where cleaning up dead persistent sockets
sometimes caused a segfault, because the dead socket was removed
from the persistent resource store before being closed. Removing
an object from the persistent resource store can end up deleting
the object, which is what happened in the segfault case.