Fixing keepalive race condition (for #361)