Try reapplying fix for #14251
This re-applies 2e23e1c7 and 73273be4, fixing #14251 (closed) and #13904 (closed). I have split up the previously-monolithic patches into multiple commits. I suspect that this won't work as-is since it was previously reverted; unfortunately I wasn't very specific in describing the reason why. CI will tell us soon enough.