mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
363 stars 93 forks source link

Getting "Error: socket has unexpectedly been closed" while exporting data #255

Closed kartikgupta2607 closed 9 months ago

kartikgupta2607 commented 9 months ago

I am using mariadb version - 3.2.0 promise based approach to create an export adaptor for mariadb server using queryStream on simple connection (not using pooled option). But after exporting few records (50k out of 1M records), the execution suddenly terminates with Error: socket has unexpectedly been closed error with following details [{"code":"ER_SOCKET_UNEXPECTED_CLOSE","message":"(conn=706534, no: 45009, SQLState: 08S01) socket has unexpectedly been closed"}].

On debugging, it seems that in the __streamreadable.js Readable.prototype.read function it is somehow going into the following block

if (state.length === 0) {
    // If we have nothing in the buffer, then we want to know
    // as soon as we *do* get something into the buffer.
    if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick.
    if (nOrig !== n && state.ended) endReadable(this);
  }

and calling the endReadable function which further calls the endReadableNT function which emits end event and the end event handler has no error passed to it, so it throws the above error. I tried to modify the server variable timeouts (net_read_timeout, net_write_timeout and max_allowed_packet) and connection level are mainly kept as default (tried with connectTimeout option). Can someone help fix the issue?

Here is a reference of the script which replicates the behaviour on debugging Mariadb Export Sample.txt

rusher commented 9 months ago

That's a guess, but i expect to be the solution: The server usually expects clients to read off the result set relatively quickly. The net_write_timeout server variable controls this behavior (defaults to 60s). When reaching that timeout, server will close socket. If you don't expect results to be handled in this amount of time there is a different possibility:

kartikgupta2607 commented 9 months ago

I will certainly give that a try and provide you with an update. Thank you for your prompt assistance.

kartikgupta2607 commented 9 months ago

Hello @rusher , thank you for the suggestion, I tried setting the net_write_timeout variable using connection options and its working as expected. However, I have a question: Are there any scenarios where the server might wait for this duration, even in error cases?

rusher commented 9 months ago

Server bever wait, it either send results or error. The only time server wait is because of locks.

roy-mdr commented 9 months ago

I'm also having the same error "Error: socket has unexpectedly been closed" but every day at 6:00am (I guess 12:00 UTC because my server timezone is UTC-06:00). Since this is the only time I'm getting the error is hard to debug with just the custom logs after the app crashes every 24hrs. I have another app running MariaDB connector 2.5.4 without issues, so my temporal solution was to downgrade to 2.5.4. Working fine in that version.

kartikgupta2607 commented 9 months ago

@roy-mdr Maybe check for any scheduled restarts or connection clean up tasks? For me the issue was resolved by increasing net_write_timeout variable as I had to export large amount of data