synchronize connection closing to avoid race