The first commit is specific to 3.0, others commits are 4.0 backport.
Thanks to @thesamesam for the report and 4.0 commit list.
Fixes #28441 (closed)