backport r10240 and r10242