diff --git a/src/shared/transfer/overlay/engine/transfer/hash-threads.js b/src/shared/transfer/overlay/engine/transfer/hash-threads.js index b92a53a1..64bd8192 100644 --- a/src/shared/transfer/overlay/engine/transfer/hash-threads.js +++ b/src/shared/transfer/overlay/engine/transfer/hash-threads.js @@ -27,6 +27,11 @@ const SMALL_BLOCK_BYTES = 4 * 1024 * 1024 // the worker thread. const MAX_STARTS = 3 +// How often a pass waiting for its turn reads its cancel signal, which is a plain boolean. +const TURN_CANCEL_POLL_MS = 50 + +const cancelled = () => codedError('publish aborted', 'ECANCELLED') + /** The threads went away during a pass; the pass is repeated on the worker thread. */ export class HashThreadsStopped extends Error {} @@ -133,7 +138,7 @@ export function createHashThreads({ blockSize, onUnavailable = null, entry = ENT if (!(err instanceof HashThreadsStopped)) throw err await drop(t) // close() took the threads away: the overlay is going down, and the pass ends with it. - if (closed) throw codedError('publish aborted', 'ECANCELLED') + if (closed) throw cancelled() report(err) return null } finally { @@ -145,10 +150,27 @@ export function createHashThreads({ blockSize, onUnavailable = null, entry = ENT blockSize, // Runs pass(threads) with the threads to itself: the content hash is one stream, so passes // take turns. Resolves null when the threads are unavailable or stopped during the pass. - run(pass) { - const result = queue.then(() => runPass(pass)) + // A pass waiting for its turn rejects as cancelled once signal.aborted is set: the pass ahead + // can take minutes, and it never reads another pass's signal. Once it has the threads, the + // cancel is the pass's own to honour. + run(pass, signal = null) { + let waiting = true + const result = queue.then(() => { + waiting = false + if (signal?.aborted) throw cancelled() + return runPass(pass) + }) queue = result.catch(() => {}) - return result + if (!signal) return result + return new Promise((resolve, reject) => { + const poll = setInterval(() => { + if (!waiting) return clearInterval(poll) + if (!signal.aborted) return + clearInterval(poll) + reject(cancelled()) + }, TURN_CANCEL_POLL_MS) + result.then(resolve, reject).finally(() => clearInterval(poll)) + }) }, // Ends the threads at once. A pass in flight sees them stop and rejects as cancelled. async close() { diff --git a/src/shared/transfer/overlay/engine/transfer/prepare-threaded.js b/src/shared/transfer/overlay/engine/transfer/prepare-threaded.js index 88db3c78..8e094b40 100644 --- a/src/shared/transfer/overlay/engine/transfer/prepare-threaded.js +++ b/src/shared/transfer/overlay/engine/transfer/prepare-threaded.js @@ -179,5 +179,5 @@ async function runPass(threads, job) { // or null when the threads are unavailable or stopped during the pass. Rejects as the // worker-thread pass does: ECANCELLED on job.signal, a read error as it is. export function prepareOnThreads(hashThreads, job) { - return hashThreads.run((threads) => runPass(threads, job)) + return hashThreads.run((threads) => runPass(threads, job), job.signal) } diff --git a/test/integration/prepare-hash-threads.test.js b/test/integration/prepare-hash-threads.test.js index 37bf043d..581e5806 100644 --- a/test/integration/prepare-hash-threads.test.js +++ b/test/integration/prepare-hash-threads.test.js @@ -2,6 +2,7 @@ // every file, and the worker-thread pass whenever the threads cannot carry a file. import test from 'brittle' import { tmpStore, tmpDir, fs, path } from './overlay-engine-helpers.js' +import { scaled } from '../helpers/bare-timing.js' import { FileIndex } from '../../src/shared/transfer/overlay/engine/store/file-index.js' import { prepareForServe, hostReadBlock } from '../../src/shared/transfer/overlay/engine/transfer/prepare.js' import { createHashThreads } from '../../src/shared/transfer/overlay/engine/transfer/hash-threads.js' @@ -28,9 +29,9 @@ function writeFile(dir, name, size, fill = 'random') { // The threads behind a counter, so a test can assert that a file really went through them. function counted(hashThreads) { const seen = { runs: 0, carried: 0 } - const run = async (pass) => { + const run = async (pass, signal) => { seen.runs++ - const result = await hashThreads.run(pass) + const result = await hashThreads.run(pass, signal) if (result) seen.carried++ return result } @@ -137,6 +138,31 @@ test('cancel ends a threaded prepare with ECANCELLED and leaves the threads usab t.alike(unavailable, [], 'a cancel is not a thread failure') }) +// REGRESSION (FIX-HASH-TURN-CANCEL: passes take turns on the threads, and a cancelled one that was +// still waiting for its turn stayed until the file ahead of it was hashed to its end.) +test('REGRESSION (FIX-HASH-TURN-CANCEL): a prepare cancelled while it waits for the threads ends at once', { timeout: scaled(15000) }, async (t) => { + const { index, dir, hashThreads, threads, seen } = await setup(t) + const filePath = writeFile(dir, 'waiting', 2 * BLOCK + 7) + let finishAhead + const ahead = hashThreads.run(() => new Promise((resolve) => { finishAhead = () => resolve('ahead') })) + const signal = { aborted: false } + const waiting = prepareForServe(index, filePath, { hashThreads: threads, signal }) + signal.aborted = true + try { + await waiting + t.fail('did not reject') + } catch (err) { + t.is(err.code, 'ECANCELLED', 'cancelled while the pass ahead still holds the threads') + } + finishAhead() + t.is(await ahead, 'ahead', 'the pass ahead is not disturbed') + + const expected = await prepareForServe(index, filePath) + const actual = await prepareForServe(index, filePath, { hashThreads: threads }) + t.is(actual.contentHash, expected.contentHash, 'the next file is hashed correctly') + t.is(seen.carried, 1, 'and on the threads, which the cancelled pass never took') +}) + test('the cancel error carries the code the publish path reads', async (t) => { const { index, dir, threads } = await setup(t) const filePath = writeFile(dir, 'coded', 2 * BLOCK)