Thanks to Ali Lotfi for the initial report and patch and Tim Graham for the review. Backport of c1b6a8a9 from master