@bobfrankston/rmfmail 1.2.211 → 1.2.213

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -413,6 +413,13 @@ const SCHEMA = `
413
413
  CREATE INDEX IF NOT EXISTS idx_message_folders_folder_uid ON message_folders(folder_id, uid);
414
414
  `;
415
415
 
416
+ /** Park the thread for `ms` without spinning. The DB is opened in a
417
+ * constructor (no async available) and the write-access probe below needs to
418
+ * wait out a transient lock; Atomics.wait is the only true sync sleep. */
419
+ function sleepSync(ms: number): void {
420
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
421
+ }
422
+
416
423
  export class MailxDB {
417
424
  private db: DatabaseSync;
418
425
 
@@ -421,11 +428,81 @@ export class MailxDB {
421
428
  * handle opened) and reject any write at the SQLite level. */
422
429
  public readonly readOnly: boolean;
423
430
 
431
+ /** Set when a WRITER connection came back read-only anyway — the OS refused
432
+ * read-write access at open and SQLite silently downgraded the handle (see
433
+ * openWritable). Every write for the life of the process will throw. The
434
+ * daemon reads this right after construction and raises a sticky fatal
435
+ * banner; nothing else should ever swallow it. */
436
+ public writeDenied = false;
437
+
438
+ /** Open the database read-WRITE and VERIFY that we actually got write
439
+ * access.
440
+ *
441
+ * SQLite silently downgrades to a read-only handle when the OS refuses the
442
+ * read-write open (a transient sharing/permission denial — AV or backup
443
+ * scan, a previous instance still exiting, resume-from-sleep). Nothing
444
+ * throws at open: reads all work, and then EVERY write for the life of the
445
+ * process fails with "attempt to write a readonly database". On 2026-08-02
446
+ * that ran an entire boot as a zombie session — is_replied backfill,
447
+ * membership collapse, addAccount for all four accounts, flags (★), sync:
448
+ * all dead, surfaced only as a per-account banner ("outlook: attempt to
449
+ * write a readonly database") that read like an account problem.
450
+ *
451
+ * So: probe with a real write inside a transaction we then ROLL BACK, and
452
+ * reopen with backoff while the verdict is read-only. It has to be an
453
+ * actual page write — under WAL, `BEGIN IMMEDIATE` alone takes its lock in
454
+ * the -shm wal-index and SUCCEEDS on a read-only database (measured), so a
455
+ * lock-only probe reports healthy on exactly the handle we're hunting.
456
+ * SQLITE_BUSY is the opposite verdict — it PROVES the handle is
457
+ * write-capable and merely contended, so keep it. */
458
+ private openWritable(dbPath: string): DatabaseSync {
459
+ const backoffMs = [0, 100, 300, 1000, 2000];
460
+ let lastErr = "";
461
+ for (let attempt = 0; attempt < backoffMs.length; attempt++) {
462
+ if (backoffMs[attempt]) sleepSync(backoffMs[attempt]);
463
+ const db = new DatabaseSync(dbPath);
464
+ try {
465
+ db.exec("BEGIN IMMEDIATE");
466
+ db.exec("CREATE TABLE _mailx_write_probe(x)");
467
+ db.exec("ROLLBACK"); // probe table never reaches the file
468
+ if (attempt > 0) console.log(` [db] write access acquired on attempt ${attempt + 1}`);
469
+ return db;
470
+ } catch (e: any) {
471
+ lastErr = e?.message || String(e);
472
+ if (!/readonly/i.test(lastErr)) {
473
+ // Busy/locked — another writer holds the lock this instant.
474
+ // That is a write-capable handle; take it.
475
+ try { db.exec("ROLLBACK"); } catch { /* no txn was opened */ }
476
+ return db;
477
+ }
478
+ console.error(` [db] opened READ-ONLY (attempt ${attempt + 1}/${backoffMs.length}): ${lastErr}`);
479
+ db.close();
480
+ }
481
+ }
482
+ // Out of retries. Return a handle anyway — reading mail beats no app at
483
+ // all — but flag it so the daemon can say so, loudly and stickily.
484
+ console.error(` [db] FATAL: ${dbPath} is READ-ONLY after ${backoffMs.length} attempts — no change will be saved (${lastErr})`);
485
+ this.writeDenied = true;
486
+ return new DatabaseSync(dbPath);
487
+ }
488
+
424
489
  constructor(dbDir: string, opts: { readOnly?: boolean; skipMigrations?: boolean } = {}) {
425
490
  fs.mkdirSync(dbDir, { recursive: true });
426
491
  const dbPath = path.join(dbDir, "mailx.db");
427
492
  this.readOnly = !!opts.readOnly;
428
- this.db = new DatabaseSync(dbPath);
493
+ this.db = this.readOnly ? new DatabaseSync(dbPath) : this.openWritable(dbPath);
494
+ if (this.writeDenied) {
495
+ // Asked for a writer, got a read-only handle (openWritable already
496
+ // shouted about it). Everything below — journal_mode, the schema,
497
+ // every migration and backfill — is a write, so running it would
498
+ // throw or bury the real message under a dozen derived errors.
499
+ // Set up as a reader and let the daemon surface the banner: the
500
+ // user can still READ mail while the app says, once and plainly,
501
+ // that nothing is being saved.
502
+ this.db.exec("PRAGMA foreign_keys = ON");
503
+ this.db.exec("PRAGMA busy_timeout = 5000");
504
+ return;
505
+ }
429
506
  if (opts.skipMigrations && !opts.readOnly) {
430
507
  // Write-capable connection that does NOT run schema/migrations —
431
508
  // used by the sync worker, which opens its own writer AFTER the
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-store",
3
- "version": "0.1.78",
3
+ "version": "0.1.80",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",