Third try to fix 1571. Move shutdown adjust timeout logics into utility function, improve comments and style.
Fix build errors.