Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WAL mode: *-shm and *-wal files are not properly cleaned up #376

Closed
Prinzhorn opened this issue Apr 15, 2020 · 1 comment
Closed

WAL mode: *-shm and *-wal files are not properly cleaned up #376

Prinzhorn opened this issue Apr 15, 2020 · 1 comment

Comments

@Prinzhorn
Copy link
Contributor

This is about as minimal of an example as I could extract from my code base. After terminating the process wal.db-shm and wal.db-wal are still there. The order of the close calls matters.

The WAL file is created when the first connection to the database is opened and is normally removed when the last connection to the database closes. However, if the last connection does not shutdown cleanly, the WAL file will remain in the filesystem and will be automatically cleaned up the next time the database is opened.

https://sqlite.org/tempfiles.html

I assume for readonly: true the connection is not properly cleaned up? That's why closing the readonly connection last does not clean up. The code contains two places that each individually change the behavior.

const Database = require('better-sqlite3');
const write = new Database('./wal.db');
// <-----------------------
// If you remove readonly then the files are properly removed.
// <-----------------------
const read = new Database('./wal.db', { fileMustExist: true, readonly: true });

write.pragma('journal_mode = WAL');
read.pragma('journal_mode = WAL');

write.exec('CREATE TABLE my_favorite_numbers (value TEXT)');
write.exec('INSERT INTO my_favorite_numbers VALUES (3)');

process.on('exit', () => {
  console.log('Exiting');

  // <------------------------------
  // If you switch these two lines then the files are properly removed.
  // <------------------------------
  write.close();
  read.close();
});
process.on('SIGHUP', () => process.exit(128 + 1));
process.on('SIGINT', () => process.exit(128 + 2));
process.on('SIGTERM', () => process.exit(128 + 15));

// Keep running
setTimeout(function() {}, 100000);
@Prinzhorn
Copy link
Contributor Author

Prinzhorn commented Apr 15, 2020

However, if the last client did not call sqlite3_close() before it shut down, or if the last client to disconnect was a read-only client, then the final cleanup operation does not occur and the shm and wal files may still exist on disk even when the database is not in use.

https://sqlite.org/draft/walformat.html

Oof.

I can control the order, so not a big deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant