Fix #4258 and improve unittest

@tanghus @DeepDiver1975 

Fix #4258