overleaf/services/filestore/app/js/SafeExec.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

const lodashOnce = require('lodash.once')
2019-12-18 05:01:59 -05:00
const childProcess = require('child_process')
const Settings = require('@overleaf/settings')
2019-12-18 05:01:59 -05:00
const { ConversionsDisabledError, FailedCommandError } = require('./Errors')
// execute a command in the same way as 'exec' but with a timeout that
// kills all child processes
//
// we spawn the command with 'detached:true' to make a new process
// group, then we can kill everything in that process group.
2019-12-18 05:01:59 -05:00
module.exports = safeExec
module.exports.promises = safeExecPromise
// options are {timeout: number-of-milliseconds, killSignal: signal-name}
function safeExec(command, options, callback) {
if (!Settings.enableConversions) {
2019-12-18 05:01:59 -05:00
return callback(
new ConversionsDisabledError('image conversions are disabled')
)
}
2019-12-18 05:01:59 -05:00
const [cmd, ...args] = command
2019-12-18 05:01:59 -05:00
const child = childProcess.spawn(cmd, args, { detached: true })
let stdout = ''
let stderr = ''
2019-12-18 05:01:59 -05:00
let killTimer
2020-08-10 12:01:12 -04:00
const cleanup = lodashOnce(function (err) {
if (killTimer) {
clearTimeout(killTimer)
}
callback(err, stdout, stderr)
})
2019-12-18 05:01:59 -05:00
if (options.timeout) {
2020-08-10 12:01:12 -04:00
killTimer = setTimeout(function () {
try {
// use negative process id to kill process group
2019-12-18 05:01:59 -05:00
process.kill(-child.pid, options.killSignal || 'SIGTERM')
} catch (error) {
cleanup(
2020-04-30 08:20:40 -04:00
new FailedCommandError('failed to kill process after timeout', {
command,
options,
pid: child.pid
})
)
}
}, options.timeout)
}
2020-08-10 12:01:12 -04:00
child.on('close', function (code, signal) {
2019-12-18 05:01:59 -05:00
if (code || signal) {
return cleanup(
new FailedCommandError(command, code || signal, stdout, stderr)
)
}
2019-12-18 05:01:59 -05:00
cleanup()
})
2020-08-10 12:01:12 -04:00
child.on('error', (err) => {
2019-12-18 05:01:59 -05:00
cleanup(err)
})
2020-08-10 12:01:12 -04:00
child.stdout.on('data', (chunk) => {
2019-12-18 05:01:59 -05:00
stdout += chunk
})
2020-08-10 12:01:12 -04:00
child.stderr.on('data', (chunk) => {
2019-12-18 05:01:59 -05:00
stderr += chunk
})
}
2019-12-18 05:01:59 -05:00
function safeExecPromise(command, options) {
return new Promise((resolve, reject) => {
safeExec(command, options, (err, stdout, stderr) => {
if (err) {
reject(err)
}
resolve({ stdout, stderr })
})
})
}