diff --git a/@xen-orchestra/backups/_forkStreamUnpipe.js b/@xen-orchestra/backups/_forkStreamUnpipe.js index c3acb8501..72f31e01d 100644 --- a/@xen-orchestra/backups/_forkStreamUnpipe.js +++ b/@xen-orchestra/backups/_forkStreamUnpipe.js @@ -3,6 +3,8 @@ const eos = require('end-of-stream') const { PassThrough } = require('stream') +const { debug } = require('@xen-orchestra/log').createLogger('xo:backups:forkStreamUnpipe') + // create a new readable stream from an existing one which may be piped later // // in case of error in the new readable stream, it will simply be unpiped @@ -11,18 +13,23 @@ exports.forkStreamUnpipe = function forkStreamUnpipe(stream) { const { forks = 0 } = stream stream.forks = forks + 1 + debug('forking', { forks: stream.forks }) + const proxy = new PassThrough() stream.pipe(proxy) eos(stream, error => { if (error !== undefined) { + debug('error on original stream, destroying fork', { error }) proxy.destroy(error) } }) - eos(proxy, _ => { - stream.forks-- + eos(proxy, error => { + debug('end of stream, unpiping', { error, forks: --stream.forks }) + stream.unpipe(proxy) if (stream.forks === 0) { + debug('no more forks, destroying original stream') stream.destroy(new Error('no more consumers for this stream')) } })