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

ERR_STREAM_PREMATURE_CLOSE #1253

Closed
LebCit opened this issue Jul 21, 2023 · 13 comments
Closed

ERR_STREAM_PREMATURE_CLOSE #1253

LebCit opened this issue Jul 21, 2023 · 13 comments

Comments

@LebCit
Copy link

LebCit commented Jul 21, 2023

Hello,

I'm using Node.js Version 18.17.0
I've updated today :

  • @hono/node-server from 1.0.2 to 1.1.0
  • hono from 3.2.7 to 3.3.2
    When I start my app I'm getting the following error:
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

The app continues to work but once in a while this error pops up without stopping the app.

@yusukebe
Copy link
Member

Hi @LebCit , thanks for reporting.

I've noticed the error you mentioned, but I believe it's already been fixed. Does this error occur consistently? If possible, could you share the piece of code or a snippet that triggers this bug?

@LebCit
Copy link
Author

LebCit commented Jul 24, 2023

Hello @yusukebe

I've updated today Hono from 3.3.2 to 3.3.3 and the error still occurs.
I've finally managed to spot when it occurs.
I'm not using any kind of back-end hot reload, so if I stay a little bit (a minute or maybe a little bit longer) on the front-end on any page then visit another route the error pops up in the back-end's console.

Regarding the code, the project is in the following GitHub repository: https://github.com/LebCit/blog-doc

I've just seen that you've starred it, thank you very much for your support.

@yusukebe
Copy link
Member

Hi @LebCit

Thank you for sharing this. While this doesn't directly concern Hono core, it pertains to @hono/node-server. I have encountered this error a few times as well. I'll create an issue in the github.com/honojs/node-server repo and we will investigate and address it later.

@tangye1234
Copy link
Contributor

tangye1234 commented Jul 25, 2023

@LebCit Hi, I downloaded your codes, and use node 18.17.0 / hono 3.3.3 / node-server 1.1.0.

But I couldn't reproduce your issues.
The ERR_STREAM_PREMATURE_CLOSE error means that, there is a user abort event from the user side.

So could you provide us some more detailed reproduction steps?

@LebCit
Copy link
Author

LebCit commented Jul 25, 2023

Hello @yusukebe @tangye1234

Thanks again both for your time, efforts and starring Blog-Doc.
@tangye1234 If you have installed the app and run node index or npm run fire and tried the app, the error will pop up if you leave the app on a page like Documentation for a minute or maybe a little bit longer.
N.B: Today I've worked on the app and the error showed only one time in a couple of hours.
I'm really sorry to bother you both with this error that I can only spot with the described step.

Another weird behavior is that if you stay about a minute on a page like Documentation without touching the app, the console.log(c.req.param()) defined in index.js returns { folder: 'rss' }

I've just opened the app from 7pm52 til 8pm04 played around a little bit leaved the app and this is the console:

{} # This is the entry route
{ folder: 'pages', filename: 'documentation' }      
# The { folder: 'rss' } was here but it disappeared!
{ folder: 'admin' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }        
{ folder: 'posts' }
{ folder: 'tags' }
{ folder: 'tags', tag: 'Archive', filename: 'Archive' }
{ folder: 'search' }
{ folder: 'pages', filename: 'documentation' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }
{ folder: 'admin' }
{ folder: 'admin-config-site' }
{ folder: 'admin-config-menu' }
{} # This is the entry route
{ folder: 'pages', filename: 'documentation' }
{} # This is the entry route
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}
{ folder: 'posts', filename: 'the-new-blog-doc' }

While editing the last code and leaved the app on without touching it then came back at 8pm18 and clicked a few route this is the console:

{}
{ folder: 'pages', filename: 'documentation' }      
{ folder: 'admin' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }        
{ folder: 'posts' }
{ folder: 'tags' }
{ folder: 'tags', tag: 'Archive', filename: 'Archive' }
{ folder: 'search' }
{ folder: 'pages', filename: 'documentation' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }
{ folder: 'admin' }
{ folder: 'icons', filename: 'favicon.ico' }
{ folder: 'admin' }
{ folder: 'admin-config-site' }
{ folder: 'admin-config-menu' }
{}
{ folder: 'pages', filename: 'documentation' }
{}
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}
{ folder: 'posts', filename: 'the-new-blog-doc' }
{ folder: 'posts' }
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}
{ folder: 'posts', filename: 'the-new-blog-doc' }
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}
{ folder: 'admin' }
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:526:35)
    at emitCloseNT (node:_http_server:996:10)
    at Socket.onServerResponseClose (node:_http_server:278:5)
    at Socket.emit (node:events:526:35)
    at TCP.<anonymous> (node:net:323:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

@tangye1234
Copy link
Contributor

This error is actually a normal tcp termination error. Usually we don't need to pay attention to it. If it really bothers, I can filter this error in node server, or just ignore the error stack. The latter thing is exactly what nextjs or express dose.

@yusukebe
Copy link
Member

Hi @tangye1234

Thanks for investigating! Ignoring the error stack in the node server is good for me. Can you create the issue?

@yusukebe
Copy link
Member

Hey @tangye1234 !

I too am facing this problem now. I want to make it ignores the error, to do so, do I make it does nothing in the catch statement?

https://github.com/honojs/node-server/blob/7f5dd58e1bb32747263bb89798d97c82e390a227/src/listener.ts#L85-L91

@tangye1234
Copy link
Contributor

yes, just don’t print console error, and PR is welcome.

The error may be thrown either from down stream in nodejs server response or from upstream in hono stream. The premature error is thrown from nodejs down stream when user abort a http request. Most frameworks just print error message instead of error stack.

Nuxt / Next do not swallow this error, but only print message instead.

@yusukebe
Copy link
Member

@tangye1234

Thanks for the explanation, would be great if you could make PR!

@tangye1234
Copy link
Contributor

tangye1234 commented Sep 19, 2023

I think neither outgoing.end nor ignoring thedestroy would make any sense.

The error thrown from the pipeline for kinds of situation:

  1. User aborts the client request. In fact, somehow the socket is closed before response's end is called normally. Such that ERR_STREAM_PREMATURE_CLOSE error is thrown. In that, we just finish the response by destroying it, because this would effectively prevent any writing action on the outgoing.
  2. Upstream reading error while pipeline is processing, thus we can stop the response to avoid waiting on the client. If we use end instead of destroy, the client will assume that we got a correct response, but fact is the response is corrupted, contaminated, like the response length dose not meet the Content-Length header, so destroying will give a proper hint for the client. Or if we just ignore the post processing, then the response dose not know when to close, which may lead a long tcp hangout.

The annoying stacktrace is carried by the console.log https://github.com/honojs/node-server/blob/7f5dd58e1bb32747263bb89798d97c82e390a227/src/listener.ts#L87C10-L87C10

We can just test the error info, if it is ERR_STREAM_PREMATURE_CLOSE, just console.info('use abort a request')

@yusukebe Hope you could do the test and PR. As for now, I am in a very hurry leading project and have barely time for verifying that. Thanks 👍

@yusukebe
Copy link
Member

@tangye1234

Thanks for the detailed explanation despite you are busy . I've corrected the PR honojs/node-server#83. Please take a look if you like.

@yusukebe
Copy link
Member

This was closed by honojs/node-server#83.

Thanks!

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

No branches or pull requests

3 participants