It can happen if createLogsAndIpcServer (and spawnMaster) finish before writeFile and master.on('close') got called.

That's why I was using fs.writeFileSync.
Another solution could be simply calling createLogsAndIpcServer inside the writeFile callback