Fix #3301

This cherry picks the change which has already been merged into master.