Backend: Improve robustness of reentrancy check.

The reentrancy protection was broken after the first detection.