vercel / vercel/next.js

Middleware and custom server randomly causes `uncaughtException` `error: aborted` when work with nginx using `error_page`

Open
#49,587 12 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug not stale Runtime
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101
    Binaries:
      Node: 16.13.1
      npm: 8.1.2
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.2-canary.4
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)

Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue

/

To Reproduce
  1. Set up nginx and run with config:
server {
    listen 80;
    server_name _;

    location / {
      proxy_pass http://host.lima.internal:3000;

    }

    location /not_found {
      return 404 "The requested 1 resource was not found";
    }

    proxy_intercept_errors on;

    error_page 404 /not_found;
  }
  1. Add middleware.js file inside /src
//middleware.js
import { NextResponse } from 'next/server';

export async function middleware(req, event) {
  console.log(`[*] Request new: ${req.url}`);
  return NextResponse.next();
}
  1. Add custom server inside /src:
//server.js
const express = require('express');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3000;


process.on('uncaughtException', (error) => {
  console.error('Uncaught Exception log :', error);
})

const app = next({
  dev,
  hostname,
  port
});
const handler = app.getRequestHandler();

app.prepare().then(() => {
    const server = express();
    server.all('*', (req, res) => {
      return handler(req, res);
    });

    server.listen(3000, (err) => {
      if (err) {
        console.log(err);
        process.exit(1);
      }

      console.log(`> Listening on http://:3000`);
    });
});
  1. Set up script run custom server:
"scripts": {
  "dev": "next dev",
  "start": "NODE_ENV=production node src/server.js",
  "start-next": "next start",
  "build": "next build",
  "lint": "next lint"
},
  1. run nginx proxy in local machine
  2. run npm run build
  3. run npm run start
  4. run fetch requests in console window:
for (let index = 0; index < 120; index++) {
    fetch(`http://localhost/hyusy/isisom${index}`, {
      "headers": {
        "accept": "application/json, text/plain, */*",
        "accept-language": "en-US,en;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/json",
        "sec-fetch-dest": "empty"
      },
      "referrerPolicy": "unsafe-url",
      "body": "{}",
      "method": "POST",
      "credentials": "include"
    });
}
  1. Randomly cause an error Error: aborted
Uncaught Exception log : Error: aborted
    at connResetException (node:internal/errors:691:14)
    at abortIncoming (node:_http_server:596:17)
    at socketOnClose (node:_http_server:590:3)
    at Socket.emit (node:events:402:35)
    at TCP.<anonymous> (node:net:687:12) {
  code: 'ECONNRESET'
}
Describe the Bug
  • Randomly cause an error Error: aborted
Uncaught Exception log : Error: aborted
    at connResetException (node:internal/errors:691:14)
    at abortIncoming (node:_http_server:596:17)
    at socketOnClose (node:_http_server:590:3)
    at Socket.emit (node:events:402:35)
    at TCP.<anonymous> (node:net:687:12) {
  code: 'ECONNRESET'
}
  • This error occurs when using both custom server and middleware and nginx proxy (with error_page 404 /not_found).
  • The error does not occur when we do not use the middleware, nginx, or a custom server.
  • The error does not occur when we use nginx proxy without error_page 404 /not_found.
  • The error does not occur when we do not use custom server, we start with next start.
Expected Behavior

Nextjs should not throw the uncaughtException

Which browser are you using? (if relevant)

Chrome 112.0.5615.137

How are you deploying your application? (if relevant)

using custom server and local

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the nginx error_page configuration, src/middleware.js, and src/server.js; start by comparing the custom Express server path with next start and with or without middleware and error_page. Trace the Node ECONNRESET/aborted stack under the reported request pattern. Done means the setup no longer produces an uncaughtException.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, nginx, node.js
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.