backport of #4357 to master

backport of #4357 to master

Fixes #4341 in master

@karlitschek candidate for 5.0.10 ?

@MTGap @icewind1991 @blizzz @schiesbn @butonic THX