Merge race condition fix patch from Gareth.