Avoid an infinite loop in InitRootHandlers destructor

If there is a problem with the list of open files from ROOT such that casting an element to TFile fails the code no longer gets stuck in an infinite loop.
The problem actually happened in the Tier 0 probably due to a threading problem in ROOT.

This is a backport of #6213