Skip to content

fix: prevent OOM on very deep DAGs #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"it-pipe": "^2.0.4",
"it-pushable": "^3.1.0",
"it-map": "^1.0.6",
"p-queue": "^7.3.0",
"multiformats": "^9.4.2",
"uint8arrays": "^3.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { pushable } from 'it-pushable'
import parallel from 'it-parallel'
import { pipe } from 'it-pipe'
import map from 'it-map'
import PQueue from 'p-queue'

/**
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
Expand All @@ -19,14 +20,15 @@ import map from 'it-map'
/**
* @param {Blockstore} blockstore
* @param {PBNode | Uint8Array} node
* @param {import('it-pushable').Pushable<Uint8Array | undefined>} queue
* @param {import('it-pushable').Pushable<Uint8Array>} queue
* @param {number} streamPosition
* @param {number} start
* @param {number} end
* @param {PQueue} walkQueue
* @param {ExporterOptions} options
* @returns {Promise<void>}
*/
async function walkDAG (blockstore, node, queue, streamPosition, start, end, options) {
async function walkDAG (blockstore, node, queue, streamPosition, start, end, walkQueue, options) {
// a `raw` node
if (node instanceof Uint8Array) {
queue.push(extractDataFromBlock(node, streamPosition, start, end))
Expand Down Expand Up @@ -100,19 +102,23 @@ async function walkDAG (blockstore, node, queue, streamPosition, start, end, opt
}),
async (source) => {
for await (const { link, block, blockStart } of source) {
/** @type {PBNode | Uint8Array} */
let child
switch (link.Hash.code) {
case dagPb.code:
child = await dagPb.decode(block)
child = dagPb.decode(block)
break
case raw.code:
child = block
break
default:
throw errCode(new Error(`Unsupported codec: ${link.Hash.code}`), 'ERR_NOT_UNIXFS')
queue.end(errCode(new Error(`Unsupported codec: ${link.Hash.code}`), 'ERR_NOT_UNIXFS'))
return
}

await walkDAG(blockstore, child, queue, blockStart, start, end, options)
walkQueue.add(async () => {
await walkDAG(blockstore, child, queue, blockStart, start, end, walkQueue, options)
})
}
}
)
Expand Down Expand Up @@ -141,14 +147,20 @@ const fileContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
return
}

const queue = pushable({
objectMode: true
// use a queue to walk the DAG instead of recursion to ensure very deep DAGs
// don't overflow the stack
const walkQueue = new PQueue({
concurrency: 1
})
const queue = pushable()

walkDAG(blockstore, node, queue, 0, offset, offset + length, options)
.catch(err => {
queue.end(err)
})
walkQueue.add(async () => {
await walkDAG(blockstore, node, queue, 0, offset, offset + length, walkQueue, options)
})

walkQueue.on('error', error => {
queue.end(error)
})

let read = 0

Expand Down