app: disallow a theoretical infinite loop if we run out of ids.

In practice, that's likely impossible to ever happen. This was just
itching my perfectionist self, who enjoys flawless design.
(cherry picked from commit 951393b34e6a33f862c91d38e5ca387d7c76b158)