process.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const path = require('path')
  2. const log = require('../logger').create('launcher')
  3. const env = process.env
  4. function ProcessLauncher (spawn, tempDir, timer, processKillTimeout) {
  5. const self = this
  6. let onExitCallback
  7. const killTimeout = processKillTimeout || 2000
  8. // Will hold output from the spawned child process
  9. const streamedOutputs = {
  10. stdout: '',
  11. stderr: ''
  12. }
  13. this._tempDir = tempDir.getPath(`/karma-${this.id.toString()}`)
  14. this.on('start', function (url) {
  15. tempDir.create(self._tempDir)
  16. self._start(url)
  17. })
  18. this.on('kill', function (done) {
  19. if (!self._process) {
  20. return process.nextTick(done)
  21. }
  22. onExitCallback = done
  23. self._process.kill()
  24. self._killTimer = timer.setTimeout(self._onKillTimeout, killTimeout)
  25. })
  26. this._start = function (url) {
  27. self._execCommand(self._getCommand(), self._getOptions(url))
  28. }
  29. this._getCommand = function () {
  30. return env[self.ENV_CMD] || self.DEFAULT_CMD[process.platform]
  31. }
  32. this._getOptions = function (url) {
  33. return [url]
  34. }
  35. // Normalize the command, remove quotes (spawn does not like them).
  36. this._normalizeCommand = function (cmd) {
  37. if (cmd.charAt(0) === cmd.charAt(cmd.length - 1) && '\'`"'.includes(cmd.charAt(0))) {
  38. cmd = cmd.substring(1, cmd.length - 1)
  39. log.warn(`The path should not be quoted.\n Normalized the path to ${cmd}`)
  40. }
  41. return path.normalize(cmd)
  42. }
  43. this._onStdout = function (data) {
  44. streamedOutputs.stdout += data
  45. }
  46. this._onStderr = function (data) {
  47. streamedOutputs.stderr += data
  48. }
  49. this._execCommand = function (cmd, args) {
  50. if (!cmd) {
  51. log.error(`No binary for ${self.name} browser on your platform.\n Please, set "${self.ENV_CMD}" env variable.`)
  52. // disable restarting
  53. self._retryLimit = -1
  54. return self._clearTempDirAndReportDone('no binary')
  55. }
  56. cmd = this._normalizeCommand(cmd)
  57. log.debug(cmd + ' ' + args.join(' '))
  58. self._process = spawn(cmd, args)
  59. let errorOutput = ''
  60. self._process.stdout.on('data', self._onStdout)
  61. self._process.stderr.on('data', self._onStderr)
  62. self._process.on('exit', function (code, signal) {
  63. self._onProcessExit(code, signal, errorOutput)
  64. })
  65. self._process.on('error', function (err) {
  66. if (err.code === 'ENOENT') {
  67. self._retryLimit = -1
  68. errorOutput = `Can not find the binary ${cmd}\n\tPlease set env variable ${self.ENV_CMD}`
  69. } else if (err.code === 'EACCES') {
  70. self._retryLimit = -1
  71. errorOutput = `Permission denied accessing the binary ${cmd}\n\tMaybe it's a directory?`
  72. } else {
  73. errorOutput += err.toString()
  74. }
  75. })
  76. self._process.stderr.on('data', function (errBuff) {
  77. errorOutput += errBuff.toString()
  78. })
  79. }
  80. this._onProcessExit = function (code, signal, errorOutput) {
  81. log.debug(`Process ${self.name} exited with code ${code} and signal ${signal}`)
  82. let error = null
  83. if (self.state === self.STATE_BEING_CAPTURED) {
  84. log.error(`Cannot start ${self.name}\n\t${errorOutput}`)
  85. error = 'cannot start'
  86. }
  87. if (self.state === self.STATE_CAPTURED) {
  88. log.error(`${self.name} crashed.\n\t${errorOutput}`)
  89. error = 'crashed'
  90. }
  91. if (error) {
  92. log.error(`${self.name} stdout: ${streamedOutputs.stdout}`)
  93. log.error(`${self.name} stderr: ${streamedOutputs.stderr}`)
  94. }
  95. self._process = null
  96. streamedOutputs.stdout = ''
  97. streamedOutputs.stderr = ''
  98. if (self._killTimer) {
  99. timer.clearTimeout(self._killTimer)
  100. self._killTimer = null
  101. }
  102. self._clearTempDirAndReportDone(error)
  103. }
  104. this._clearTempDirAndReportDone = function (error) {
  105. tempDir.remove(self._tempDir, function () {
  106. self._done(error)
  107. if (onExitCallback) {
  108. onExitCallback()
  109. onExitCallback = null
  110. }
  111. })
  112. }
  113. this._onKillTimeout = function () {
  114. if (self.state !== self.STATE_BEING_KILLED && self.state !== self.STATE_BEING_FORCE_KILLED) {
  115. return
  116. }
  117. log.warn(`${self.name} was not killed in ${killTimeout} ms, sending SIGKILL.`)
  118. self._process.kill('SIGKILL')
  119. // NOTE: https://github.com/karma-runner/karma/pull/1184
  120. // NOTE: SIGKILL is just a signal. Processes should never ignore it, but they can.
  121. // If a process gets into a state where it doesn't respond in a reasonable amount of time
  122. // Karma should warn, and continue as though the kill succeeded.
  123. // This a certainly suboptimal, but it is better than having the test harness hang waiting
  124. // for a zombie child process to exit.
  125. self._killTimer = timer.setTimeout(function () {
  126. log.warn(`${self.name} was not killed by SIGKILL in ${killTimeout} ms, continuing.`)
  127. self._onProcessExit(-1, null, '')
  128. }, killTimeout)
  129. }
  130. }
  131. ProcessLauncher.decoratorFactory = function (timer) {
  132. return function (launcher, processKillTimeout) {
  133. const spawn = require('child_process').spawn
  134. function spawnWithoutOutput () {
  135. const proc = spawn.apply(null, arguments)
  136. proc.stdout.resume()
  137. proc.stderr.resume()
  138. return proc
  139. }
  140. ProcessLauncher.call(launcher, spawnWithoutOutput, require('../temp_dir'), timer, processKillTimeout)
  141. }
  142. }
  143. module.exports = ProcessLauncher