hbenl / mocha-explorer-launcher-scripts

Launcher scripts for the Mocha Explorer extension for VS Code
MIT License
0 stars 2 forks source link

Node process doesn't seem to exit with custom launcher script #5

Open WillsB3 opened 2 years ago

WillsB3 commented 2 years ago

Hi,

Firstly, thank you for your work on Mocha Test Explorer for VSCode and these associated repos.

Apologies for cluttering up your GitHub issues - Not sure if there's a better place to ask this. If there is please close and point me there :).

I have written a simple custom launcher script which does some preprocessing to the environment variables used as part of the test run but I'm having a problem where debugging (using vscodes step-through debugging tools) works fine the first time but then stops working on subsequent attempts.

I've narrowed down the issue and it seems that when the Mocha debugging test run finishes the remote process never closes. This causes the next run to not be able to attach to the Node debugger as the port is already in use by the instance of the remote process created by the first run.

Here's the pertinent bits of my custom launcher code:

const { fork, spawn } = require('child_process')
const log = (msg) => process.send(msg)

process.once('message', async (workerArgs) => {
  log('Received workerArgs')

  process.chdir(workerArgs.cwd)

  // …
  // perform environment variable modifications. When this is complete, `launchWorker()` is called.
  // …

  function launchWorker() {
    const execArgv = workerArgs.debuggerPort
      ? [`--inspect=${workerArgs.debuggerPort}`]
      : []

    console.info(`working going to listen on ${workerArgs.debuggerPort}`)
    const childProc = fork(workerArgs.workerScript, [], {
      execArgv,
      stdio: 'inherit',
    })

    // forward the request to the worker script
    childProc.send(workerArgs)
    // forward the results from the worker script on to our parent
    childProc.on('message', (msg) => process.send(msg))
  }
})

On subsequent debugging attempts using this custom launcher script I see the following log output in the Mocha Tests output channel in VSCode:

working going to listen on 9229
forking worker
child closed
Starting inspector on 127.0.0.1:9229 failed: address already in use

I can also see the process still running in vscode, even though the tests have finished being debugged/run.

Screenshot 2022-01-18 at 12 48 15

I can further validate that the process is still listening on the debugger port:

Screenshot 2022-01-18 at 12 55 45

If I kill the process and retry the debugging it will work again for a single time.

Am I missing something in my custom launcher script or does the problem lie somewhere else?

Many thanks

hbenl commented 2 years ago

I can't spot any problems in your launcher script. This could be caused by the tests doing something that prevents node from stopping the process, e.g. running a server or attaching a callback to a promise that is never resolved. The easiest way to fix this would be to add the exit option to your mocha configuration - that will force the process to exit after the test run has finished.

hbenl commented 2 years ago

Not sure if there's a better place to ask this

No, this is exactly the right place for this question :)