Skip to content
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

[BACKPORT v7.x] Ensure the worker processes do not crash when parsing invalid syntax #212

Merged
merged 1 commit into from
Apr 7, 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
14 changes: 13 additions & 1 deletion lib/parallel-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,19 @@ function transformString(string, options, buildOptions) {
let pool = getWorkerPool();
_logger.info('transformString is parallelizable');
let serializedObj = { babel : serialize(options.babel), 'cacheKey': options.cacheKey };
return pool.exec('transform', [string, serializedObj]);
return pool
.exec('transform', [string, serializedObj])
.then((result) => {
if (result.error) {
// when the worker has an error it still resolves, but it has `error`
// and `stack` properties instead of `code` + `metadata`
//
// throw an error to properly fail the "top level" process as needed
throw new Error(result.error + result.stack);
}

return result;
});
} else {
if (JOBS <= 1) {
_logger.info('JOBS <= 1, skipping worker, using main thread');
Expand Down
19 changes: 13 additions & 6 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ const ParallelApi = require('./parallel-api');

// transpile the input string, using the input options
function transform(string, options) {
return new Promise(resolve => {
let result = transpiler.transform(string, ParallelApi.deserialize(options));
return Promise.resolve().then(() => {
try {
let result = transpiler.transform(string, ParallelApi.deserialize(options));

resolve({
code: result.code,
metadata: result.metadata
});
return {
code: result.code,
metadata: result.metadata
};
} catch (error) {
return {
error: error.message,
stack: error.stack,
};
}
});
}

Expand Down