| … | … | @@ -0,0 +1,96 @@ |
| 1 | // ★ Two overlapping resolves must not fight over the registry store's lock. |
| 2 | // |
| 3 | // ⛔ MEASURED ON A HANDSET, 2026-09-15. After an OTA staged a release, the next check threw: |
| 4 | // |
| 5 | // ota: File descriptor could not be locked @ FDLock._resume |
| 6 | // (…/node_modules/pgh/node_modules/fd-lock/index.js:62) < FDLock._open |
| 7 | // < open (ready-resource) < FDLock.ready |
| 8 | // |
| 9 | // `makeGetLatestRelease` returns a function that runs a FULL resolveRelease per call — open a |
| 10 | // Corestore on `storeDir`, read, cleanup. RocksDB's lock is EXCLUSIVE, so two overlapping calls on |
| 11 | // one storeDir means the second loses and throws. On the phone the two callers are the release watch |
| 12 | // firing and the settings page asking for update status, and they overlap by construction. |
| 13 | // |
| 14 | // ★ THE FIX ALREADY EXISTS ONE LAYER DOWN and says why, in NetworkFetcher.ready(): |
| 15 | // |
| 16 | // "Memoize the in-flight promise. Without this, concurrent callers … each run |
| 17 | // new Corestore(this._storeDir) on the same path. The device-file FDLock admits only one; the |
| 18 | // loser throws 'File descriptor could not be locked' …" |
| 19 | // |
| 20 | // ⚠ THE IN-FLIGHT PROMISE, NEVER THE RESULT. A cached entry would go stale and a poller would stop |
| 21 | // seeing new releases — which is the opposite failure and a worse one, because it is silent. |
| 22 | |
| 23 | import test from 'brittle' |
| 24 | import { makeGetLatestRelease } from '../lib/resolve-release.js' |
| 25 | |
| 26 | // A resolver whose "store" admits exactly one holder at a time, which is what RocksDB does. |
| 27 | function exclusiveStore () { |
| 28 | const state = { held: false, opens: 0, peak: 0, live: 0 } |
| 29 | return { |
| 30 | state, |
| 31 | async open () { |
| 32 | state.opens++ |
| 33 | if (state.held) throw new Error('File descriptor could not be locked') |
| 34 | state.held = true |
| 35 | state.live++ |
| 36 | state.peak = Math.max(state.peak, state.live) |
| 37 | await new Promise((r) => setTimeout(r, 30)) |
| 38 | return { entry: { name: 'app', version: '1.0.0' }, cleanup: async () => { state.held = false; state.live-- } } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | test('★★★ concurrent calls do not open the store twice', async (t) => { |
| 44 | const store = exclusiveStore() |
| 45 | const get = makeGetLatestRelease({ registryKeyHex: 'ab'.repeat(32), _resolve: store.open }) |
| 46 | |
| 47 | const [a, b, c] = await Promise.all([get('app'), get('app'), get('app')]) |
| 48 | |
| 49 | t.is(store.state.peak, 1, 'never more than one holder — the lock is exclusive and this respects it') |
| 50 | t.alike(a, b, 'every caller gets the same answer') |
| 51 | t.alike(b, c) |
| 52 | }) |
| 53 | |
| 54 | test('⛔ a LATER call still resolves afresh — the promise is memoised, not the result', async (t) => { |
| 55 | const store = exclusiveStore() |
| 56 | const get = makeGetLatestRelease({ registryKeyHex: 'ab'.repeat(32), _resolve: store.open }) |
| 57 | |
| 58 | await get('app') |
| 59 | await get('app') |
| 60 | |
| 61 | // ⚠ Caching the ENTRY would make a poller blind to new releases — silent, and worse than the error |
| 62 | // this fixes. |
| 63 | t.is(store.state.opens, 2, 'two sequential calls are two resolves') |
| 64 | }) |
| 65 | |
| 66 | test('⛔ a failure is not memoised — the next call retries', async (t) => { |
| 67 | let n = 0 |
| 68 | const get = makeGetLatestRelease({ |
| 69 | registryKeyHex: 'ab'.repeat(32), |
| 70 | _resolve: async () => { |
| 71 | n++ |
| 72 | if (n === 1) throw new Error('swarm down') |
| 73 | return { entry: { name: 'app', version: '2.0.0' }, cleanup: async () => {} } |
| 74 | } |
| 75 | }) |
| 76 | |
| 77 | await t.exception(() => get('app'), /swarm down/) |
| 78 | const second = await get('app') |
| 79 | t.is(second.version, '2.0.0', 'a transient failure does not poison every later call') |
| 80 | }) |
| 81 | |
| 82 | test('different names do not block each other', async (t) => { |
| 83 | const seen = [] |
| 84 | const get = makeGetLatestRelease({ |
| 85 | registryKeyHex: 'ab'.repeat(32), |
| 86 | _resolve: async ({ name }) => { |
| 87 | seen.push(name) |
| 88 | await new Promise((r) => setTimeout(r, 10)) |
| 89 | return { entry: { name }, cleanup: async () => {} } |
| 90 | } |
| 91 | }) |
| 92 | const [x, y] = await Promise.all([get('one'), get('two')]) |
| 93 | t.is(x.name, 'one') |
| 94 | t.is(y.name, 'two') |
| 95 | t.is(seen.length, 2) |
| 96 | }) |