#284 release open fd on aborted download
This commit is contained in:
@@ -118,7 +118,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets:
|
|||||||
const failure = await archiveStaged(archive, stages, controller)
|
const failure = await archiveStaged(archive, stages, controller)
|
||||||
if (clientGone) {
|
if (clientGone) {
|
||||||
log(`Zip download for share ${share.key} cancelled by client`)
|
log(`Zip download for share ${share.key} cancelled by client`)
|
||||||
archive.abort()
|
teardownArchive(archive, res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (failure) {
|
if (failure) {
|
||||||
@@ -135,8 +135,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets:
|
|||||||
if (outcome !== 'done') {
|
if (outcome !== 'done') {
|
||||||
if (outcome === 'closed') log(`Zip download for share ${share.key} cancelled by client`)
|
if (outcome === 'closed') log(`Zip download for share ${share.key} cancelled by client`)
|
||||||
// 'error' was already logged by the archiver error listener
|
// 'error' was already logged by the archiver error listener
|
||||||
archive.abort()
|
teardownArchive(archive, res)
|
||||||
res.destroy()
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await fs.rm(options.stagingDir, { recursive: true, force: true }).catch(() => { /* best effort */ })
|
await fs.rm(options.stagingDir, { recursive: true, force: true }).catch(() => { /* best effort */ })
|
||||||
@@ -186,8 +185,19 @@ function abortDownload (archive: Archiver, res: Response, share: SharedLink, fai
|
|||||||
? `HTTP ${failure.status}`
|
? `HTTP ${failure.status}`
|
||||||
: (failure.error instanceof Error ? failure.error.message : String(failure.error))
|
: (failure.error instanceof Error ? failure.error.message : String(failure.error))
|
||||||
log(`Aborting zip download for share ${share.key}: failed to fetch asset ${failure.asset.id} from ${failure.url} (${detail})`)
|
log(`Aborting zip download for share ${share.key}: failed to fetch asset ${failure.asset.id} from ${failure.url} (${detail})`)
|
||||||
|
teardownArchive(archive, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tear down an aborted archive so it releases its resources. `abort()` alone
|
||||||
|
* is not enough; it kills the queue but leaves any in-flight entry paused,
|
||||||
|
* holding an open read fd on its tempfile forever #284
|
||||||
|
*/
|
||||||
|
function teardownArchive (archive: Archiver, res: Response) {
|
||||||
archive.abort()
|
archive.abort()
|
||||||
|
archive.unpipe(res)
|
||||||
res.destroy()
|
res.destroy()
|
||||||
|
archive.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -35,6 +35,33 @@ async function stagingDirs (): Promise<string[]> {
|
|||||||
return entries.filter(name => name.startsWith('ipp-zip-'))
|
return entries.filter(name => name.startsWith('ipp-zip-'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open fds pointing at deleted staging tempfiles. An aborted archiver used to
|
||||||
|
* keep the in-flight entry's read stream open forever; on tmpfs that pins the
|
||||||
|
* deleted file's space (issue #284). Linux-only introspection - returns []
|
||||||
|
* elsewhere, making the assertions no-ops.
|
||||||
|
*/
|
||||||
|
async function deletedStagingFds (): Promise<string[]> {
|
||||||
|
const fds = await fs.readdir('/proc/self/fd').catch(() => [] as string[])
|
||||||
|
const leaked: string[] = []
|
||||||
|
for (const fd of fds) {
|
||||||
|
const target = await fs.readlink('/proc/self/fd/' + fd).catch(() => '')
|
||||||
|
if (target.includes('ipp-zip-') && target.includes('(deleted)')) leaked.push(target)
|
||||||
|
}
|
||||||
|
return leaked
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wait for archiver's post-abort drain to release fds, then return leftovers. */
|
||||||
|
async function settledDeletedStagingFds (timeoutMs = 3000): Promise<string[]> {
|
||||||
|
const deadline = Date.now() + timeoutMs
|
||||||
|
let leaked = await deletedStagingFds()
|
||||||
|
while (leaked.length > 0 && Date.now() < deadline) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
leaked = await deletedStagingFds()
|
||||||
|
}
|
||||||
|
return leaked
|
||||||
|
}
|
||||||
|
|
||||||
function makeAsset (id: string): Asset {
|
function makeAsset (id: string): Asset {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
@@ -130,6 +157,7 @@ describe('downloadAssets abort handling', () => {
|
|||||||
expect(signals.length).toBeGreaterThan(0)
|
expect(signals.length).toBeGreaterThan(0)
|
||||||
expect(signals.every(s => s.aborted)).toBe(true)
|
expect(signals.every(s => s.aborted)).toBe(true)
|
||||||
expect(await stagingDirs()).toEqual([])
|
expect(await stagingDirs()).toEqual([])
|
||||||
|
expect(await settledDeletedStagingFds()).toEqual([])
|
||||||
}, 10_000)
|
}, 10_000)
|
||||||
|
|
||||||
it('resolves and cleans up when the client aborts while the zip is streaming', async () => {
|
it('resolves and cleans up when the client aborts while the zip is streaming', async () => {
|
||||||
@@ -148,6 +176,9 @@ describe('downloadAssets abort handling', () => {
|
|||||||
}
|
}
|
||||||
await downloadAssets(asResponse(res), share, [makeAsset('a1'), makeAsset('a2')])
|
await downloadAssets(asResponse(res), share, [makeAsset('a1'), makeAsset('a2')])
|
||||||
expect(await stagingDirs()).toEqual([])
|
expect(await stagingDirs()).toEqual([])
|
||||||
|
// The regression from issue #284's follow-up: the staging dir was removed
|
||||||
|
// but archiver still held an open fd on a deleted tempfile.
|
||||||
|
expect(await settledDeletedStagingFds()).toEqual([])
|
||||||
}, 10_000)
|
}, 10_000)
|
||||||
|
|
||||||
it('cleans up and destroys the response when an upstream fetch keeps failing', async () => {
|
it('cleans up and destroys the response when an upstream fetch keeps failing', async () => {
|
||||||
@@ -156,5 +187,6 @@ describe('downloadAssets abort handling', () => {
|
|||||||
await downloadAssets(asResponse(res), share, [makeAsset('a1')])
|
await downloadAssets(asResponse(res), share, [makeAsset('a1')])
|
||||||
expect(res.destroyed).toBe(true)
|
expect(res.destroyed).toBe(true)
|
||||||
expect(await stagingDirs()).toEqual([])
|
expect(await stagingDirs()).toEqual([])
|
||||||
|
expect(await settledDeletedStagingFds()).toEqual([])
|
||||||
}, 10_000)
|
}, 10_000)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user