Ensure an error is raised on infinite recursion

Closes #8727