Thanks for reporting the bug. Please try the attached patch, which I'd like to install into the emacs-26 branch if it fixes your problem.