A small fix, to work around a race condition

The whole thing is very very bad, and should not get into production
anyway, but at least this version doesn't have a simple race condition
and doesn't open the file twice for recording anything. I strongly
recommend using a database for this.