فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A07): bound stalled IndexedDB upgrades and safely retry startup
* fix(A07): bound blocked IndexedDB upgrade waits and allow safe retry * test(A07): simulate blocked IndexedDB migration, explicit timeout and safe retry
davoodmehraban89 committed
Oct 15, 2026 at 20:48 UTC
b52223cc88dac5c40b1cb9952ca22d6c50bd4a62
2 files changed
+54
-5
src/core/StorageService.js
+21
-5
@@ -58,6 +58,7 @@ class StorageServiceImpl {
58
constructor() {
59
this.db = null;
60
this._opening = null;
61
+ this.blockedTimeoutMs = 12000;
62
}
63
64
// ---------- باز کردن پایگاه داده ----------
@@ -68,6 +69,15 @@ class StorageServiceImpl {
69
70
this._opening = new Promise((resolve, reject) => {
71
const req = indexedDB.open(DB_NAME, DB_VERSION);
72
+ let resolved = false;
73
+ let blockedTimer = null;
74
+ const fail = error => {
75
+ if (resolved) return;
76
+ resolved = true;
77
+ if (blockedTimer !== null) clearTimeout(blockedTimer);
78
+ this._opening = null;
79
+ reject(error);
80
+ };
81
82
req.onupgradeneeded = (e) => {
83
const db = e.target.result;
@@ -83,10 +93,19 @@ class StorageServiceImpl {
93
};
94
95
req.onblocked = () => {
86
- console.warn('IndexedDB: ارتقای پایگاه داده منتظر بسته شدن پنجرههای دیگر است');
96
+ console.warn('IndexedDB: نسخه دیگری از پایگاه داده هنوز باز است');
97
+ if (blockedTimer !== null) return;
98
+ blockedTimer = setTimeout(() => {
99
+ fail(new Error('ارتقای پایگاه داده متوقف شده است؛ پنجرههای دیگر برنامه را ببندید و دوباره تلاش کنید'));
100
+ }, this.blockedTimeoutMs);
101
};
102
103
req.onsuccess = () => {
104
+ if (blockedTimer !== null) clearTimeout(blockedTimer);
105
+ // An older blocked request may complete after its timeout. Close it:
106
+ // only a fresh explicit retry may take ownership of that DB handle.
107
+ if (resolved) { req.result.close(); return; }
108
+ resolved = true;
109
this.db = req.result;
110
this.db.onversionchange = () => {
111
this.db.close();
@@ -97,10 +116,7 @@ class StorageServiceImpl {
116
resolve(this.db);
117
};
118
100
- req.onerror = () => {
101
- this._opening = null;
102
- reject(req.error);
103
- };
119
+ req.onerror = () => fail(req.error || new Error('باز کردن پایگاه داده ناموفق بود'));
120
});
121
122
return this._opening;
tests/a07-indexeddb-blocked-startup.test.mjs
new
+33
@@ -0,0 +1,33 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import 'fake-indexeddb/auto';
4
+import { StorageService } from '../src/core/StorageService.js';
5
+
6
+const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
7
+
8
+test('A07: blocked v6-to-v7 upgrade times out rather than hanging at initial splash, then safely retries', async () => {
9
+ const older = await new Promise((resolve, reject) => {
10
+ const request = indexedDB.open('finora_pro_db', 6);
11
+ request.onsuccess = () => resolve(request.result);
12
+ request.onerror = () => reject(request.error);
13
+ });
14
+ StorageService.blockedTimeoutMs = 25;
15
+ await assert.rejects(StorageService.init(), /ارتقای پایگاه داده متوقف شده/);
16
+ assert.equal(StorageService._opening, null);
17
+ assert.equal(StorageService.db, null);
18
+
19
+ // Closing the stale handle may complete the first request; its on-success
20
+ // must close the late v7 connection instead of retaining it as the active DB.
21
+ older.close();
22
+ await pause(10);
23
+ const current = await StorageService.init();
24
+ assert.equal(current.version, 7);
25
+ assert.equal(StorageService.db, current);
26
+ assert.equal((await StorageService.getAll('audit_events')).length, 0);
27
+});
28
+
29
+test('A07: repeated init calls share the same successful opening transaction', async () => {
30
+ const [a, b] = await Promise.all([StorageService.init(), StorageService.init()]);
31
+ assert.equal(a, b);
32
+ assert.equal(a.version, 7);
33
+});