dredd icon indicating copy to clipboard operation
dredd copied to clipboard

Dredd doesn't kill server when started from bash script

Open ericgriffis opened this issue 9 years ago • 7 comments

Describe your problem

I need to start my Unicorn+Sinatra server with an environment variable. As suggested in https://github.com/apiaryio/dredd/issues/554, I set the environment variable in a shell script and used that to start my server with dredd. However, dredd doesn't kill the server on exit, leaving me to find the process and kill it each time.

I created a minimal example to reproduce: https://github.com/ericgriffis/dredd_sinatra_example

The first time I run dredd (using yml config), it runs fine. The second time, I see errors when dredd tries to start the server since the server from the previous run is still alive:

E, [2017-05-26T13:39:52.590851 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)
E, [2017-05-26T13:39:52.590935 #90324] ERROR -- : retrying in 0.5 seconds (4 tries left)
E, [2017-05-26T13:39:53.092481 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)
E, [2017-05-26T13:39:53.092546 #90324] ERROR -- : retrying in 0.5 seconds (3 tries left)
E, [2017-05-26T13:39:53.597173 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)
E, [2017-05-26T13:39:53.597233 #90324] ERROR -- : retrying in 0.5 seconds (2 tries left)
E, [2017-05-26T13:39:54.102149 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)
E, [2017-05-26T13:39:54.102207 #90324] ERROR -- : retrying in 0.5 seconds (1 tries left)
E, [2017-05-26T13:39:54.607209 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)
E, [2017-05-26T13:39:54.607288 #90324] ERROR -- : retrying in 0.5 seconds (0 tries left)
E, [2017-05-26T13:39:55.108813 #90324] ERROR -- : adding listener failed addr=0.0.0.0:8081 (in use)

What command line options do you use?

N/A

What is in your dredd.yml?

From repo above:

dry-run: null
hookfiles: null
language: nodejs
sandbox: false
server: scripts/start_test_server.sh
server-wait: 3
init: false
custom: {}
names: false
only: []
reporter: []
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
color: true
level: info
timestamp: false
silent: false
path: []
hooks-worker-timeout: 5000
hooks-worker-connect-timeout: 1500
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 100
hooks-worker-term-timeout: 5000
hooks-worker-term-retry: 500
hooks-worker-handler-host: 127.0.0.1
hooks-worker-handler-port: 61321
config: ./dredd.yml
blueprint: swagger.yml
endpoint: 'http://127.0.0.1:8081'

Script script/start_test_server.sh (again from repo above):

#!/bin/bash
export TEST=true
bundle exec unicorn -p 8081

What's your dredd --version output?

dredd v3.5.0 (Darwin 15.4.0; x64)

Does dredd --level=debug uncover something?

Dredd seems to think its killing the server just fine:

verbose: Dredd instance run finished.
verbose: Exiting Dredd process with status '0'.
debug: Using configured custom exit() method to terminate the Dredd process.
debug: The backend server process has already terminated

However, lsof -i tcp:8081 reveals the process is still running, and subsequent runs of dredd show errors (included above) when attempting to start the server since the port is still in use.

Can you send us failing test in a Pull Request?

I'm willing to contribute but am unsure of what to test. Since the workaround in https://github.com/apiaryio/dredd/issues/554 apparently works for some people, I'm unsure if this issue is OS-specific or framework-specific or something else.

I would also be able to solve the same problem using https://github.com/apiaryio/dredd/pull/542, which was closed in favor of the workaround which doesn't work for me, and also apparently due to Windows incompatibility. I mention this since I'm open to other workarounds for this issue and am willing to contribute but am unsure of the best course or where to start.

ericgriffis avatar May 26 '17 17:05 ericgriffis

Thanks for this issue! The workaround you mention should definitely work and it worked before. My hunch is that the behavior got broken with introduction of https://github.com/apiaryio/dredd/pull/761.

For that reason, I think this should be a matter of adding a test to test/integration/child-process-test.coffee, where the tested process would be a bash script spawning a server-like process. Feel free to leave the AppVeyor (Windows) tests broken for now, I can add an equivalent test with a Batch file later.

honzajavorek avatar Jun 02 '17 11:06 honzajavorek

By the way, this is an excellent bug report 👍

honzajavorek avatar Jun 02 '17 11:06 honzajavorek

Has there been any movement on resolving this bug?

2-cents: Identical error using using npm run test-server.

Test run 1: Works Test run 2: Fails because the node server is still running from test run 1, Error: connect ECONNREFUSED 127.0.0.1:300. The test run 1 server is killed. Test run 3: Works

rmharrison avatar Oct 26 '17 23:10 rmharrison

Hello @rmharrison, there is no progress unfortunately. Would you be willing to look into this? Good starting point would be to write a failing test case (as @honzajavorek pointed out in his comment above).

michalholasek avatar Oct 27 '17 08:10 michalholasek

+1 to fix that!

sireliah avatar Nov 06 '17 09:11 sireliah

Hey if the above doesn't work for you and you're using babal-node to run the server, compile first and run normally with node! I spent ages trying to work out why Dredd wouldn't terminate the server and this ended up being the issue. 🙃🔫

noisyscanner avatar Mar 16 '18 18:03 noisyscanner

This can be used as a neat workaround (npm task), if anyone is still struggling with this. Tweak the sleep time, and watch for sleep being unix command (if you are on windows you might want something different, cross-platform, there are plenty of packages out there). Note that this will launch the server first, and kill everything when dredd is done while returning return code of the tool first to finish execution, in this case dredd:

"some:task": "./node_modules/.bin/concurrently -k -s \"first\" \"npm start\" \"sleep 3s && ./node_modules/.bin/dredd\""

From the concurrently docs: Concurrently

-k, --kill-others      kill other processes if one exits or dies
-s, --success     Return exit code of zero or one based on the success or failure of the "first" child to terminate, the "last child", or succeed only if "all" child processes succeed. [choices: "first", "last", "all"] [default: "all"]

uro5h avatar Jan 30 '19 19:01 uro5h