add stack length check to avoid error while no middleware.