Detect circular middleware stacks and throw comprehensible exception.

#706 ported to 2.4.1 codebase.