fix #8853

See discussion in #8853; current recursion check fails for staged function. Not the best solution, but seems to fix the problem without affecting performance.