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