process.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.slice(1, -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. self._onProcessExit(-1, null, errorOutput)
  76. })
  77. self._process.stderr.on('data', function (errBuff) {
  78. errorOutput += errBuff.toString()
  79. })
  80. }
  81. this._onProcessExit = function (code, signal, errorOutput) {
  82. if (!self._process) {
  83. // Both exit and error events trigger _onProcessExit(), but we only need one cleanup.
  84. return
  85. }
  86. log.debug(`Process ${self.name} exited with code ${code} and signal ${signal}`)
  87. let error = null
  88. if (self.state === self.STATE_BEING_CAPTURED) {
  89. log.error(`Cannot start ${self.name}\n\t${errorOutput}`)
  90. error = 'cannot start'
  91. }
  92. if (self.state === self.STATE_CAPTURED) {
  93. log.error(`${self.name} crashed.\n\t${errorOutput}`)
  94. error = 'crashed'
  95. }
  96. if (error) {
  97. log.error(`${self.name} stdout: ${streamedOutputs.stdout}`)
  98. log.error(`${self.name} stderr: ${streamedOutputs.stderr}`)
  99. }
  100. self._process = null
  101. streamedOutputs.stdout = ''
  102. streamedOutputs.stderr = ''
  103. if (self._killTimer) {
  104. timer.clearTimeout(self._killTimer)
  105. self._killTimer = null
  106. }
  107. self._clearTempDirAndReportDone(error)
  108. }
  109. this._clearTempDirAndReportDone = function (error) {
  110. tempDir.remove(self._tempDir, function () {
  111. self._done(error)
  112. if (onExitCallback) {
  113. onExitCallback()
  114. onExitCallback = null
  115. }
  116. })
  117. }
  118. this._onKillTimeout = function () {
  119. if (self.state !== self.STATE_BEING_KILLED && self.state !== self.STATE_BEING_FORCE_KILLED) {
  120. return
  121. }
  122. log.warn(`${self.name} was not killed in ${killTimeout} ms, sending SIGKILL.`)
  123. self._process.kill('SIGKILL')
  124. // NOTE: https://github.com/karma-runner/karma/pull/1184
  125. // NOTE: SIGKILL is just a signal. Processes should never ignore it, but they can.
  126. // If a process gets into a state where it doesn't respond in a reasonable amount of time
  127. // Karma should warn, and continue as though the kill succeeded.
  128. // This a certainly suboptimal, but it is better than having the test harness hang waiting
  129. // for a zombie child process to exit.
  130. self._killTimer = timer.setTimeout(function () {
  131. log.warn(`${self.name} was not killed by SIGKILL in ${killTimeout} ms, continuing.`)
  132. self._onProcessExit(-1, null, '')
  133. }, killTimeout)
  134. }
  135. }
  136. ProcessLauncher.decoratorFactory = function (timer) {
  137. return function (launcher, processKillTimeout) {
  138. const spawn = require('child_process').spawn
  139. function spawnWithoutOutput () {
  140. const proc = spawn.apply(null, arguments)
  141. proc.stdout.resume()
  142. proc.stderr.resume()
  143. return proc
  144. }
  145. ProcessLauncher.call(launcher, spawnWithoutOutput, require('../temp_dir'), timer, processKillTimeout)
  146. }
  147. }
  148. module.exports = ProcessLauncher