Home > OS >  Crashing before calling unlink() on file created by mkstemp()
Crashing before calling unlink() on file created by mkstemp()

Time:11-16

I am doing something like the following

auto tempFd = mkstemp(filePathTemplate);

// 1) write to temp file, on error, unlink()
// 2) sync temp file, on error, unlink()
// 3) close temp file, on error, unlink()
// 4) rename to implement an atomic write, on error unlink()

Here, what happens if the program crashes anywhere before step (4) has completely finished, but before the unlink executes? Will temporary files keep building up indefinitely if such crashes are frequent? If so, is there any way to avoid such files building up if the program keeps crashing in a loop?

Thanks!

CodePudding user response:

The file will either be deleted or not. If it's not deleted it will persist till you either delete, or you reboot the system if you happen to write the file a tmpfs (memory) backed directory.

You write a log (fsync; local or remote) it that you are about to create a temporary file. When you start your program, you process the log and delete any temporary files you don't care about. If you crashes are frequent, and if you automatically restart your process, it will behave a like a fork storm (kill the box with load). Consider exponential delays, or if you cannot do that, implement a "dead letter" concept where troubled is moved aside.

Alternatively, create your temporary files in a special directory and remove whatever files in that directory. You may be interested in the program tmpreadper which you could invoke from a crontab. This will fail if you have sufficiently high rate of failure.

Consider using sqlite3.

  • Related