backport of #3271