ChildProcessWorker.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _child_process() {
  7. const data = require('child_process');
  8. _child_process = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _stream() {
  14. const data = require('stream');
  15. _stream = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _mergeStream() {
  21. const data = _interopRequireDefault(require('merge-stream'));
  22. _mergeStream = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _supportsColor() {
  28. const data = require('supports-color');
  29. _supportsColor = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _types = require('../types');
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. /**
  39. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  40. *
  41. * This source code is licensed under the MIT license found in the
  42. * LICENSE file in the root directory of this source tree.
  43. */
  44. const SIGNAL_BASE_EXIT_CODE = 128;
  45. const SIGKILL_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 9;
  46. const SIGTERM_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 15; // How long to wait after SIGTERM before sending SIGKILL
  47. const SIGKILL_DELAY = 500;
  48. /**
  49. * This class wraps the child process and provides a nice interface to
  50. * communicate with. It takes care of:
  51. *
  52. * - Re-spawning the process if it dies.
  53. * - Queues calls while the worker is busy.
  54. * - Re-sends the requests if the worker blew up.
  55. *
  56. * The reason for queueing them here (since childProcess.send also has an
  57. * internal queue) is because the worker could be doing asynchronous work, and
  58. * this would lead to the child process to read its receiving buffer and start a
  59. * second call. By queueing calls here, we don't send the next call to the
  60. * children until we receive the result of the previous one.
  61. *
  62. * As soon as a request starts to be processed by a worker, its "processed"
  63. * field is changed to "true", so that other workers which might encounter the
  64. * same call skip it.
  65. */
  66. class ChildProcessWorker {
  67. _child;
  68. _options;
  69. _request;
  70. _retries;
  71. _onProcessEnd;
  72. _onCustomMessage;
  73. _fakeStream;
  74. _stdout;
  75. _stderr;
  76. _exitPromise;
  77. _resolveExitPromise;
  78. constructor(options) {
  79. this._options = options;
  80. this._request = null;
  81. this._fakeStream = null;
  82. this._stdout = null;
  83. this._stderr = null;
  84. this._exitPromise = new Promise(resolve => {
  85. this._resolveExitPromise = resolve;
  86. });
  87. this.initialize();
  88. }
  89. initialize() {
  90. const forceColor = _supportsColor().stdout
  91. ? {
  92. FORCE_COLOR: '1'
  93. }
  94. : {};
  95. const child = (0, _child_process().fork)(
  96. require.resolve('./processChild'),
  97. [],
  98. {
  99. cwd: process.cwd(),
  100. env: {
  101. ...process.env,
  102. JEST_WORKER_ID: String(this._options.workerId + 1),
  103. // 0-indexed workerId, 1-indexed JEST_WORKER_ID
  104. ...forceColor
  105. },
  106. // Suppress --debug / --inspect flags while preserving others (like --harmony).
  107. execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
  108. // default to advanced serialization in order to match worker threads
  109. // @ts-expect-error: option does not exist on the node 12 types
  110. serialization: 'advanced',
  111. silent: true,
  112. ...this._options.forkOptions
  113. }
  114. );
  115. if (child.stdout) {
  116. if (!this._stdout) {
  117. // We need to add a permanent stream to the merged stream to prevent it
  118. // from ending when the subprocess stream ends
  119. this._stdout = (0, _mergeStream().default)(this._getFakeStream());
  120. }
  121. this._stdout.add(child.stdout);
  122. }
  123. if (child.stderr) {
  124. if (!this._stderr) {
  125. // We need to add a permanent stream to the merged stream to prevent it
  126. // from ending when the subprocess stream ends
  127. this._stderr = (0, _mergeStream().default)(this._getFakeStream());
  128. }
  129. this._stderr.add(child.stderr);
  130. }
  131. child.on('message', this._onMessage.bind(this));
  132. child.on('exit', this._onExit.bind(this));
  133. child.send([
  134. _types.CHILD_MESSAGE_INITIALIZE,
  135. false,
  136. this._options.workerPath,
  137. this._options.setupArgs
  138. ]);
  139. this._child = child;
  140. this._retries++; // If we exceeded the amount of retries, we will emulate an error reply
  141. // coming from the child. This avoids code duplication related with cleaning
  142. // the queue, and scheduling the next call.
  143. if (this._retries > this._options.maxRetries) {
  144. const error = new Error(
  145. `Jest worker encountered ${this._retries} child process exceptions, exceeding retry limit`
  146. );
  147. this._onMessage([
  148. _types.PARENT_MESSAGE_CLIENT_ERROR,
  149. error.name,
  150. error.message,
  151. error.stack,
  152. {
  153. type: 'WorkerError'
  154. }
  155. ]);
  156. }
  157. }
  158. _shutdown() {
  159. // End the temporary streams so the merged streams end too
  160. if (this._fakeStream) {
  161. this._fakeStream.end();
  162. this._fakeStream = null;
  163. }
  164. this._resolveExitPromise();
  165. }
  166. _onMessage(response) {
  167. // TODO: Add appropriate type check
  168. let error;
  169. switch (response[0]) {
  170. case _types.PARENT_MESSAGE_OK:
  171. this._onProcessEnd(null, response[1]);
  172. break;
  173. case _types.PARENT_MESSAGE_CLIENT_ERROR:
  174. error = response[4];
  175. if (error != null && typeof error === 'object') {
  176. const extra = error; // @ts-expect-error: no index
  177. const NativeCtor = globalThis[response[1]];
  178. const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
  179. error = new Ctor(response[2]);
  180. error.type = response[1];
  181. error.stack = response[3];
  182. for (const key in extra) {
  183. error[key] = extra[key];
  184. }
  185. }
  186. this._onProcessEnd(error, null);
  187. break;
  188. case _types.PARENT_MESSAGE_SETUP_ERROR:
  189. error = new Error(`Error when calling setup: ${response[2]}`);
  190. error.type = response[1];
  191. error.stack = response[3];
  192. this._onProcessEnd(error, null);
  193. break;
  194. case _types.PARENT_MESSAGE_CUSTOM:
  195. this._onCustomMessage(response[1]);
  196. break;
  197. default:
  198. throw new TypeError(`Unexpected response from worker: ${response[0]}`);
  199. }
  200. }
  201. _onExit(exitCode) {
  202. if (
  203. exitCode !== 0 &&
  204. exitCode !== null &&
  205. exitCode !== SIGTERM_EXIT_CODE &&
  206. exitCode !== SIGKILL_EXIT_CODE
  207. ) {
  208. this.initialize();
  209. if (this._request) {
  210. this._child.send(this._request);
  211. }
  212. } else {
  213. this._shutdown();
  214. }
  215. }
  216. send(request, onProcessStart, onProcessEnd, onCustomMessage) {
  217. onProcessStart(this);
  218. this._onProcessEnd = (...args) => {
  219. // Clean the request to avoid sending past requests to workers that fail
  220. // while waiting for a new request (timers, unhandled rejections...)
  221. this._request = null;
  222. return onProcessEnd(...args);
  223. };
  224. this._onCustomMessage = (...arg) => onCustomMessage(...arg);
  225. this._request = request;
  226. this._retries = 0; // eslint-disable-next-line @typescript-eslint/no-empty-function
  227. this._child.send(request, () => {});
  228. }
  229. waitForExit() {
  230. return this._exitPromise;
  231. }
  232. forceExit() {
  233. this._child.kill('SIGTERM');
  234. const sigkillTimeout = setTimeout(
  235. () => this._child.kill('SIGKILL'),
  236. SIGKILL_DELAY
  237. );
  238. this._exitPromise.then(() => clearTimeout(sigkillTimeout));
  239. }
  240. getWorkerId() {
  241. return this._options.workerId;
  242. }
  243. getStdout() {
  244. return this._stdout;
  245. }
  246. getStderr() {
  247. return this._stderr;
  248. }
  249. _getFakeStream() {
  250. if (!this._fakeStream) {
  251. this._fakeStream = new (_stream().PassThrough)();
  252. }
  253. return this._fakeStream;
  254. }
  255. }
  256. exports.default = ChildProcessWorker;