Salvaged from #40526; re-verified on main, tightened, tested. Co-authored-by: xxxigm <xxxigm@users.noreply.github.com>