Cli: Netlify dev server hangs if function returns 404

Created on 3 Feb 2020  路  15Comments  路  Source: netlify/cli

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
bug

Most helpful comment

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

All 15 comments

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!

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.

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!

@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!

@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!

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

Same issue here.

I'm having the same exact issue.

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).

aeesBhatti closed this in #965 23 hours ago

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

@donavon [email protected] has just been published that includes a fix for this bug. Please try it out and let us know!

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.

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?

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 :)

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

Was this page helpful?
0 / 5 - 0 ratings