mock-server/mockserver-node

`SIGTERM` handler overrides default behaviour

OliverJAsh opened this issue · 0 comments

Extracted from #44 (comment)

Full reduced test case: https://github.com/OliverJAsh/mockserver-test/tree/sigterm

package.json:

{
  "dependencies": {
    "concurrently": "^7.2.2",
    "express": "^4.18.1",
    "mockserver-node": "^5.13.2"
  }
}

main.js:

const ms = require("mockserver-node");
const express = require("express");

ms.start_mockserver({
    serverPort: 3030,
    verbose: true,
});

const app = express();
app.listen(8080);
yarn
concurrently --kill-others 'sleep 5' 'node main.js'

This command will never complete:

❯ concurrently --kill-others 'sleep 5' 'node main.js'
[1] Skipping {
[1]   "host": "oss.sonatype.org",
[1]   "path": "/content/repositories/releases/org/mock-server/mockserver-netty/5.13.2/mockserver-netty-5.13.2-jar-with-dependencies.jar",
[1]   "port": 443
[1] } as file already downloaded
[1] Running 'java -Dfile.encoding=UTF-8 -jar node_modules/mockserver-node/mockserver-netty-5.13.2-jar-with-dependencies.jar -serverPort 3030 -logLevel DEBUG'
[1] waiting for MockServer to start retries remaining: 110
[1] 2022-06-25 19:51:10 5.13.2 INFO using environment variables:
[1]
[1]   [
[1]
[1]   ]
[1]
[1]  and system properties:
[1]
[1]   [
[1]
[1]   ]
[1]
[1]  and command line options:
[1]
[1]   [
[1]   	logLevel=DEBUG,
[1]   	serverPort=3030
[1]   ]
[1]
[1] waiting for MockServer to start retries remaining: 109
[1] waiting for MockServer to start retries remaining: 108
[1] 2022-06-25 19:51:10 5.13.2 FINE logger level is DEBUG, change using:
[1]  - 'ConfigurationProperties.logLevel(String level)' in Java code,
[1]  - '-logLevel' command line argument,
[1]  - 'mockserver.logLevel' JVM system property or,
[1]  - 'mockserver.logLevel' property value in 'mockserver.properties'
[1] 2022-06-25 19:51:10 5.13.2 INFO 3030 started on port: 3030
[1] waiting for MockServer to start retries remaining: 107
[1] 2022-06-25 19:51:11 5.13.2 INFO 3030 retrieved 0 active expectations in json that match:
[1]
[1]   { }
[1]
[0] sleep 5 exited with code 0
--> Sending SIGTERM to other processes..
[1] Using port '3030' to stop MockServer and MockServer Proxy

This is due to this SIGTERM event handler:

mockserver-node/index.js

Lines 282 to 283 in 6a3200b

// stop mockserver when kill used
process.on('SIGTERM', exitHandler.bind(null, {exit: true, options: options}));

As I described in #44, as soon as we register a custom SIGTERM handler, the default behaviour of killing the parent process no longer applies.

I believe we can remove the custom SIGTERM handler (like we did for SIGINT in #44) because by default spawned child processes are killed when the parent receives SIGTERM. See https://stackoverflow.com/questions/44788013/node-child-processes-how-to-intercept-signals-like-sigint/44931266#44931266.