Fix #29582 and maybe more. Maybe fix #29116 (cherry picked from commit df1268ca08aaacae54c775a8eec34006dfe365e0)