more reliable way of saving debug index file

fixes #1884