ChildProcessWorker.js 7.9 KB

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