Fix #8570 and add test.

(cherry picked from commit 496351eee5e0bdcf1aa05f72c20646f325aef2f1)
ref: #8571