Fix race condition during file load. Fixes #1931