Thanks Huynh Thanh Tam for the initial patch and Claude Paroz for review. Backport of 39805686 from master