Fixes #2384 (instead of cherry-picking, I re-applied the change to the release branch, to skip several intermediate commits that did not work out)