mock-server/mockserver-node

`SIGINT` handler overrides default behaviour

OliverJAsh opened this issue · 3 comments

I have a Node process which looks like this:

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

const app = express();
app.listen(8080);

mockserver.start_mockserver({ serverPort: 1080, verbose: true });

When I run the Node process and then I try to kill the process using ctrl-c, it kills the mock server but not the Express server. The parent Node process continues to run and no matter how many times I run ctrl-c it does not quit.

I believe this is because of this line:

process.on('SIGINT', exitHandler.bind(null, {exit: false, options: options}));

As soon as we register a custom SIGINT handler, the default behaviour of killing the parent process no longer applies.

At first I thought we could fix this by calling process.exit ourselves, but this would prevent other SIGINT handlers from being called which could be significant.

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

The SIGTERM handler is also causing me problems, .e.g if I try to run the above script with concurrently, it never exits:

$ concurrently --kill-others-on-fail --success first --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.11.2/mockserver-netty-5.11.2-jar-with-dependencies.jar",
[1]   "port": 443
[1] } as file already downloaded
[1] (node:24554) [DEP0128] DeprecationWarning: Invalid 'main' field in '/Users/oliverash/Development/mock-server-express-sigint-bug/node_modules/mockserver-node/package.json' of 'launch_and_set_expectations.js'. Please either fix that or report it to the module author
[1] (Use `node --trace-deprecation ...` to show where the warning was created)
[1] Running 'java -Dfile.encoding=UTF-8 -jar node_modules/mockserver-node/mockserver-netty-5.11.2-jar-with-dependencies.jar -serverPort 1080 -logLevel DEBUG'
[1] waiting for MockServer to start retries remaining: 110
[1] waiting for MockServer to start retries remaining: 109
[1] waiting for MockServer to start retries remaining: 108
[1] waiting for MockServer to start retries remaining: 107
[1] 2021-06-23 22:37:12 5.11.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] 2021-06-23 22:37:12 5.11.2 INFO 1080 started on port: 1080
[1] waiting for MockServer to start retries remaining: 106
[1] 2021-06-23 22:37:12 5.11.2 INFO 1080 retrieved active expectations in json that match:
[1]
[1]   { }
[1]
[0] sleep 5 exited with code 0
--> Sending SIGTERM to other processes..
[1] Using port '1080' to stop MockServer and MockServer Proxy

Also I think there is an issue with the way uncaughtExceptions are handled. I believe we should be exiting the node process here https://github.com/mock-server/mockserver-node/blob/master/index.js#L279

- process.on('uncaughtException', exitHandler.bind(null, {exit: false, options: options}));
+ process.on('uncaughtException', exitHandler.bind(null, {exit: true, options: options}));

We patched it on our end.

It looks like we fixed SIGINT but not SIGTERM #75