uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.76k stars 563 forks source link

Error while terminating worker because it is not possible to close connections after end response #925

Closed uasan closed 1 year ago

uasan commented 1 year ago

Before closing the worker, we call

us_listen_socket_close(token);

But this is not enough, you also need to close all open connections, when you close the connection in which the completed response was, there will be this error:

Error: uWS.HttpResponse must not be accessed after uWS.HttpResponse.onAborted callback, or after a successful response.

When the worker terminates, the exit event handlers are not called, so your code that should clear all open resources is not called, as a result we get a fatal error from the libuv

uv loop at [0x17286aac8] has 2 open handles in total
/usr/local/bin/node[75654]: ../src/debug_utils.cc:324:void node::CheckedUvLoopClose(uv_loop_t *): Assertion `"Unreachable code reached" __VA_OPT__(": ") "uv_loop_close() while having open handles"' failed.

Two fix options:

  1. Allow call close after end response.
  2. Provide new method in uws.js app instance, something like this App.prototype.closeAllConnections()
e3dio commented 1 year ago

Provide new method in uws.js app instance, something like this App.prototype.closeAllConnections()

Are you aware there is app.close() method since like 6 months ago

https://github.com/uNetworking/uWebSockets/releases/tag/v20.34.0

Easy shutdown with App.close()

Calling this new function will:

  • Close and invalidate all open listen sockets.
  • Force close all open connections (both HTTP and WebSockets), calling their respective callbacks as needed (.onAborted, .close, .subscription).

This call can be used to gracefully close and release an App that needs maintenance.

uasan commented 1 year ago

Are you aware there is app.close() method since like 6 months ago

Did not know, all this time they were using an older version. Thank you very much, this completely solves my problem! I close the ticket