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

Netlify dev server hangs if function returns 404 #695

Closed
miohtama opened this issue Feb 3, 2020 · 15 comments · Fixed by #965
Closed

Netlify dev server hangs if function returns 404 #695

miohtama opened this issue Feb 3, 2020 · 15 comments · Fixed by #965
Assignees
Labels
type: bug code to address defects in shipped code

Comments

@miohtama
Copy link

miohtama commented Feb 3, 2020

This is a bug report.

Netlify dev server hangs when returning status code 404. This occurs when used with Eleventy dev server, though I am not sure if this is related.

I suspect this is somehow related to proxy behaviour - maybe there is some kind of conflict between servers handling 404.

  1. Set up Elventy + Netlify dev as instructed here https://github.com/philhawksworth/eleventyone

  2. Create a following function bug404.js

exports.handler = async (event, context) => {

    return {
        statusCode: 404,
        body: "this hangs"
    }
};
  1. Start server
npx netlify dev

(starts eleventy serve on the background)

  1. Call function using curl
curl --request POST --data "xxx" "http://localhost:8888/api/bug404"

What is the expected behaviour?

The dev server returns the response immediately.

What is the actual behaviour?

The server never finishes processing the response and the curl waits out for it forever. You need to abort with CTRL+C.

The behaviour is the same under actual browsers. AJAX POST, e.g. initiated by jQuery, never returns when status code is set to 404.

However, if you change the status code e.g. to 403, or 200, it works:

exports.handler = async (event, context) => {
    
    return {
        statusCode: 403,
        body: "this does not hang"
    }
};

curl call returns immediately.

Local Environment Information

  System:
    OS: macOS Mojave 10.14
    CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
  Binaries:
    Node: 11.0.0 - ~/.nvm/versions/node/v11.0.0/bin/node
    Yarn: 1.21.1 - ~/code/capitalgram/node_modules/.bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v11.0.0/bin/npm
  Browsers:
    Chrome: 79.0.3945.130
    Firefox: 72.0.2
    Safari: 12.0
  npmGlobalPackages:
    netlify: 3.0.0
@nonissue
Copy link

I'm having the same problem. I have a function that I expect to return a 404 in certain cases, and if I send a request with postman, it hangs, and no response is returned. Any other statusCode resolves successfully without modifying any of the other code.

If I then cancel the request in Postman, netlify dev hits an exception and stops (Socket hang up or something similar).

Looks like it's easily reproducible, but if more info is needed I can provide some, or try and set up a codesandbox to demo the issue. I hope this gets worked on!

@nonissue
Copy link

Actually, to provide an update, this only seems to be happen with nested functions (ie. functions inside subfolders within my functions directory). I copied the pattern implemented in the netlify workshop REST API example (with the API methods being called from a subfolder using a switch statement in a file in the root of the functions directory). When I return a 404 from the API method in the subfolder, the 404 seems to be returned, but then netlify starts looking for html files of the same name. Maybe this is a redirects issue?

Here's a sample of a the logs from a request that returns a 404:

Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Setting Mongo URI to dev DB
  Error occurred running read: Could not find matching song
Response with status 404 in 1960 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss.html
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Using cached database instance
  Error occurred running read: Could not find matching song
Response with status 404 in 77 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss.htm
api/read
  api/read called
  db-connect
    Starting attempt to connect to db.
    Using cached database instance
  Error occurred running read: Could not find matching song
Response with status 404 in 77 ms.
Request from ::1: GET /.netlify/functions/songs/kanye-west-jesus-walkss/index.html
segments length: 2
Response with status 500 in 1 ms.

I did create a test function called 404test (file: 404test.js) in the root of my functions dir, and it does successfully return 404 to postman when called. Super strange.

@RaeesBhatti
Copy link
Contributor

I haven't been able to reproduce this while trying in both root functions directory and a subdirectory function. Could you please share a minimal reproducible example!

@RaeesBhatti
Copy link
Contributor

RaeesBhatti commented Mar 16, 2020

@miohtama @nonissue:

I haven't been able to reproduce this while trying in both root functions directory and a subdirectory function. Could you please share a minimal reproducible example!

@nonissue
Copy link

@miohtama @nonissue:

I haven't been able to reproduce this while trying in both root functions directory and a subdirectory function. Could you please share a minimal reproducible example!

Oops, missed your response. Will try and put one together tomorrow!

@richard-flosi
Copy link

richard-flosi commented Mar 20, 2020

Running into the same issue.
The issue doesn't exist for requests without data, but it does exist for requests with data.

Here is a minimal project demo with example curl commands and output:
https://github.com/richard-flosi/netlify-dev-404

e.g.
curl --request GET "http://localhost:8888/.netlify/functions/notfoundbug" is OK
curl --request GET --data "{}" "http://localhost:8888/.netlify/functions/notfoundbug" Hangs

@RaeesBhatti RaeesBhatti self-assigned this May 9, 2020
@RaeesBhatti RaeesBhatti added the type: bug code to address defects in shipped code label May 9, 2020
@fpagnoux
Copy link

fpagnoux commented Jun 4, 2020

Same issue here.

@cjinghong
Copy link

I'm having the same exact issue.

@donavon
Copy link

donavon commented Jun 22, 2020

The same thing is happening to me. I'm POSTing data to a lambda and when it returns a 404, it hangs. I'm using Postman to hit the API. I'm not sure if it hands in the browser or not (I haven't gotten that far yet).

@donavon
Copy link

donavon commented Jun 30, 2020

aeesBhatti closed this in #965 23 hours ago

This is awesome news. When will this fix hit npm?

@RaeesBhatti
Copy link
Contributor

@donavon netilfy-cli@2.55.0 has just been published that includes a fix for this bug. Please try it out and let us know!

@terazus
Copy link

terazus commented Nov 11, 2020

Hello,
I'm using cli version 2.68.4 and i'm still getting the issue.
The server doesn't hang but test extra strings (html, htm, index.html) on top of the given input. If I manually remove these strings with a regex it ends up with a BadRequest: invalid expression error (400):
image

EDIT:
I can also reproduce the bug with 403's.

@erezrokah
Copy link
Contributor

Hello,
I'm using cli version 2.68.4 and i'm still getting the issue.
The server doesn't hang but test extra strings (html, htm, index.html) on top of the given input. If I manually remove these strings with a regex it ends up with a BadRequest: invalid expression error (400):

Thanks @terazus for reporting. This seems like a different issue from the one reported. Can you open a new one for it?

@terazus
Copy link

terazus commented Nov 12, 2020

Hi @erezrokah
I don't think i need to open a new issue, there's already #1442 which seeems to be related to my issue (im also using netlify dev)

Let me kow if i need to open a new issue anyway :)

@erezrokah
Copy link
Contributor

Thanks for finding that! This looks like what you're experiencing. Going to cross link it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants