Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfenv function calls are competing to handle the same temporary file #40

Closed
AkihiroKitada opened this issue Mar 25, 2019 · 2 comments · Fixed by #41
Closed

cfenv function calls are competing to handle the same temporary file #40

AkihiroKitada opened this issue Mar 25, 2019 · 2 comments · Fixed by #41

Comments

@AkihiroKitada
Copy link

Please suppose that there are multiple cfenv function calls in the same host at the same time - in the specific cases, for examples, Concourse CI kicks multiple cfenv function calls in the same virtual machine.

Sometimes one of the cfenv function calls fails with the following error.

ENOENT: no such file or directory, rename '/root/.ports.json.tmp' -> '/root/.ports.json'

16 | const service = appEnv.getService(name);
17 | return service && service.credentials;
> 18 | };
| ^
19 | 

at write_json (node_modules/ports/index.js:54:6)
at Object.getPort (node_modules/ports/index.js:40:3)
at getPort (node_modules/cfenv/lib/cfenv.js:224:32)
at new AppEnv (node_modules/cfenv/lib/cfenv.js:45:19)
at Object.<anonymous>.cfenv.getAppEnv (node_modules/cfenv/lib/cfenv.js:23:12)
at Object.<anonymous> (../common/env.ts:18:3)

It seems that one cfenv function call has deleted the temporary json file while another tries to rename the same temporary json file.

Please consider to fix this race condition.

@pmuellr
Copy link
Member

pmuellr commented Mar 25, 2019

I believe this is an issue with the ports package - hoodiehq-archive/node-ports#4

As a work-around, I think we can just try/catch around the getPort() call, in a loop (but not infinite) that calls it until it doesn't error. Per the note the PR, I think there may still be a race condition that multiple calls with different "names" could return the same port number, but shouldn't. This would be a problem if the ports were actually be used in real http servers. Is that the case for you? Do you use cfenv to get the port number, and then use that port to start an http server?

One immediate work-around for you would be to set the port number in one of the following env vars when running your app.

portString = process.env.PORT ||
process.env.CF_INSTANCE_PORT ||
process.env.VCAP_APP_PORT ||
appEnv?.app?.port

pmuellr pushed a commit that referenced this issue Mar 26, 2019
fixes #40

This is a very simple to fix to the problem with ports where it
will occaisonally throw an error:

- hoodiehq-archive/node-ports#4

The fix is to catch the error and use port 3000.  Not great, but should
handle some cases where this was a problem.

An alternate fix to try would be to try fetching the port again, probably
in a loop (with some limit), in hopes of getting a value from ports.
@pmuellr
Copy link
Member

pmuellr commented Mar 26, 2019

Thanks for the bug report! I've added a simple fix to prevent the error from ports from propagating up, hopefully this is good enough for your environment. If not, open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants