Partial forwardport of 469f1e362b from stable/1.8.x as the issue was already fixed in master.
469f1e362b