Cli: lambda listening on wrong port

Created on 19 Dec 2019  ·  10Comments  ·  Source: netlify/cli

- Do you want to request a _feature_ or report a _bug_?
report a bug

- What is the current behavior?
run netlify dev, notice the port it mentions is being listened to for lambda.
At the end it says "Waiting for localhost:34567" and hangs there.

- If the current behavior is a bug, please provide the steps to reproduce.
add to netlify.toml
[dev]
autoLaunch = false
functionsPort = 34567

  • run netlify dev, notice the port it mentions is being listened to for lambda.
  • I see something like: Lambda server is listening on 51976, but that port seems to change
  • let the dev server (quasar in my case) finish building
  • note the message: "Waiting for localhost:34567"

- What is the expected behavior?
lambda and the last localhost to use the same port and use the one specified for functionsPort if it is specified.

Couple notes:

  • this is on a clean install of OSX Catalina.
  • netlify-cli is installed with yarn global. yarn global list shows: "[email protected]"
  • node v12.14.0 installed via asdf-vm

I had another setup on High Sierra that did not have this issue. Unsure if its related, but that setup used npm -g for the install of netlify-cli.

Most helpful comment

It seems like getPort is used twice.
Before calling serveFunctions
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/commands/dev/index.js#L400
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/commands/dev/index.js#L403
and inside it
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/utils/serve-functions.js#L193

serveFunctions does the actual port allocation but du to the previous call it always will be locked it will default to "any available port".

All 10 comments

@smakinson Thanks for reporting this. Unfortunately I'm unable to reproduce this on my machine. Could you please share a project with minimal files to reproduce this issue as a public repo!

getPort seems like it always returns a random port and doesn't respect settings.port nor the defaultPort- https://github.com/netlify/cli/blob/52646b9f9e4b4c413a47b0ccea9a4913cf39a9f0/src/utils/serve-functions.js#L193

@RaeesBhatti @smakinson I was able to reproduce this with the following basic repo:
netlify.toml:

[dev]
    functions = "./functions"
    functionsPort = 23456
    targetPort = 8080
    port = 8080
    command = "python -m SimpleHTTPServer 8080"

index.html:

hello world!

functions/hello.js:

const headers = {
  'Access-Control-Allow-Origin': '*',
};

module.exports.handler = async (event) => {
    return new Promise((resolve, reject) => {
        resolve({
            statusCode: 200,
            headers,
            body: '{"hello": "world!"}',
        })

    })
}

macOS version: 10.12.6
node version: 12.14.0
netlify-cli version: 2.25.0
netlify dev output:

> $ netlify dev                             ⬡ 12.14.0 [±master ✓]
◈ Netlify Dev ◈
◈ Starting Netlify Dev with undefined
Waiting for localhost:8080.
◈ Lambda server is listening on 51249
Serving HTTP on 0.0.0.0 port 8080 ...

Connected!
Waiting for localhost:23456.......

httpie functions test:

> $ http GET localhost:23456/hello          ⬡ 12.14.0 [±master ✓]
http: error: ConnectionError: HTTPConnectionPool(host='localhost', port=23456): Max retries exceeded with url: /hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x101d63510>: Failed to establish a new connection: [Errno 61] Connection refused')) while doing GET request to URL: http://localhost:23456/hello
> $ http GET localhost:51249/hello          ⬡ 12.14.0 [±master ✓]
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Date: Sat, 21 Dec 2019 18:57:08 GMT
Transfer-Encoding: chunked
X-Powered-By: Express

{"hello": "world!"}

as you can see, the lambda is running on localhost:51249, not localhost:23456 (also tested with other port numbers).

@maxlever Thank you for doing that! I have not had a free moment to create the example.

I just uninstalled from global yarn, cloned the repo and yarn linked it and so far it appears to be working as expected from the repo. Is there any possibility that installing with yarn global differs from npm -g? That is one difference between my old set up and the new one.

I was having the same issue in my project, and couldn't reproduce it with @maxlever's repo, so I decided to remove netlify-cli from my devDependencies and that fixed it. 🤷‍♂️ I like to have it there since I use it in my npm scripts, is it not recommended?

Also of note, I installed [email protected] globally using npm i -g netlify-cli.

Okay, so I'm having the issue on another laptop both on my project and @maxlever's repo, and nor running netlify dev from global nor reinstalling it altogether seems to help.

$ netlify dev
◈ Netlify Dev ◈
◈ Starting Netlify Dev with undefined
Waiting for localhost:8080.
◈ Lambda server is listening on 50963
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
.
Connected!
Waiting for localhost:23456..............................................

The lambda server is starting on a random port instead of the one specified in netlify.toml (same thing happens with the default port).

netlify-cli --version output is exactly the same on both the working and non-working machines:

netlify-cli/2.25.0 darwin-x64 node-v10.18.0

The working machine is running macOS High Sierra and the non-working one Catalina, it seems like it may be related to that.

Running into the same issue on my Macbook, same project works fine on Ubuntu.

It seems like getPort is used twice.
Before calling serveFunctions
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/commands/dev/index.js#L400
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/commands/dev/index.js#L403
and inside it
https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/utils/serve-functions.js#L193

serveFunctions does the actual port allocation but du to the previous call it always will be locked it will default to "any available port".

Based on @130n's comment I changed

https://github.com/netlify/cli/blob/6fe0d2aa49197148804b5292e01fed09621c55f9/src/commands/dev/index.js#L400

to

        const functionsPort = settings.functionsPort

and the problem was fixed for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomPichaud picture TomPichaud  ·  3Comments

karl-cardenas-coding picture karl-cardenas-coding  ·  4Comments

jasikpark picture jasikpark  ·  5Comments

iamskok picture iamskok  ·  3Comments

bcomnes picture bcomnes  ·  3Comments