Cherry-picked #664 fix to 2.1 branch.