Thanks Thomas Recouvreux for the report and Tim Graham for the tests and review. Backport of ae635cc36 from master.