0

I need to upload a v8 heap dump into an AWS S3 bucket after it's generated however the file that is uploaded is either 0KB or 256KB. The file on the server is over 70MB in size so it appears that the request isn't waiting until the heap dump isn't completely flushed to disk. I'm guessing the readable stream that is getting piped into fs.createWriteStream is happening in an async manner and the await with the call to the function isn't actually waiting. I'm using the v3 version of the AWS NodeJS SDK. What am I doing incorrectly?

Code


async function createHeapSnapshot (fileName) {
    const snapshotStream = v8.getHeapSnapshot();
    // It's important that the filename end with `.heapsnapshot`,
    // otherwise Chrome DevTools won't open it.

    const fileStream = fs.createWriteStream(fileName);
    snapshotStream.pipe(fileStream);
}

async function pushHeapSnapshotToS3(fileName)
{
    const heapDump = fs.createReadStream(fileName);

    const s3Client = new S3Client();
    const putCommand = new PutObjectCommand(
        {
            Bucket: "my-bucket",
            Key: `heapdumps/${fileName}`,
            Body: heapDump
        }
    )
    
    return s3Client.send(putCommand);
}

app.get('/heapdump', asyncMiddleware(async (req, res) => {
    const currentDateTime = Date.now();
    const fileName = `${currentDateTime}.heapsnapshot`;

    await createHeapSnapshot(fileName);
    await pushHeapSnapshotToS3(fileName);
    res.send({
        heapdumpFileName: `${currentDateTime}.heapsnapshot`
    });
}));

1 Answer 1

0

Your guess is correct. The createHeapSnapshot() returns a promise, but that promise has NO connection at all to when the stream is done. Therefore, when the caller uses await on that promise, the promise is resolved long before the stream is actually done. async functions have no magic in them to somehow know when a non-promisified asynchronous operation like .pipe() is done. So, your async function returns a promise that has no connection at all to the stream functions.

Since streams don't have very much native support for promises, you can manually promisify the completion and errors of the streams:

function createHeapSnapshot (fileName) {
    return new Promise((resolve, reject) => {
        const snapshotStream = v8.getHeapSnapshot();
        // It's important that the filename end with `.heapsnapshot`,
        // otherwise Chrome DevTools won't open it.
    
        const fileStream = fs.createWriteStream(fileName);
        fileStream.on('error', reject).on('finish', resolve);

        snapshotStream.on('error', reject);        
        snapshotStream.pipe(fileStream);
    });
}

Alternatively, you could use the newer pipeline() function which does support promises (built-in promise support added in nodejs v15) and replaces .pipe() and has built-in error monitoring to reject the promise:

const { pipeline } = require('stream/promises');

function createHeapSnapshot (fileName) {
    const snapshotStream = v8.getHeapSnapshot();
    // It's important that the filename end with `.heapsnapshot`,
    // otherwise Chrome DevTools won't open it.

    return pipeline(snapshotStream, fs.createWriteStream(fileName))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.