Tedious: ConnectionError:接続が失われました-長い文字列を挿入するときにECONNRESETを書き込みます

作成日 2019年07月16日  ·  9コメント  ·  ソース: tediousjs/tedious

AzureSQLデータベースにテーブルがあります。

CREATE TABLE [dbo].[Owner](
    [OwnerId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [Name] [varchar](50) NOT NULL,
    [Signature] [varchar](max) NULL
)

署名列に非常に長い文字列を挿入しようとしました。 packetSizeが16384以上に設定されていない場合、次の例外が発生します。

ConnectionError: Connection lost - write ECONNRESET
    at ConnectionError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:26)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    at Socket.emit (events.js:205:15)
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at onwriteError (_stream_writable.js:438:5)
    at onwrite (_stream_writable.js:459:5)
    at internal/streams/destroy.js:49:7
    at Socket._destroy (net.js:593:3)
    at Socket.destroy (internal/streams/destroy.js:37:8)
Emitted 'error' event at:
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:12)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    [... lines matching original stack trace ...]
    at Socket.destroy (internal/streams/destroy.js:37:8)
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:84:12) {
  message: 'Connection lost - write ECONNRESET',
  code: 'ESOCKET'
}

以下は私の簡単なテストプログラムです。 私は最新の退屈なものを使用します。 また、パケットサイズに関係なく、長い文字列に対して読み取り操作は正常に機能するようです。 私が何を間違えたのか分かりますか?

var Connection = require('tedious').Connection;

var config = {
  server: "myserver.database.windows.net",
  options: {
    encrypt: true,
    database: "<mydb>",
    packetSize: 4096,
  },
  authentication: {
    type: "default",
    options: {  
      userName: "<myuser>",
      password: "<mypwd>",
    }
  }
};

var connection = new Connection(config);

connection.on('connect', function(err) {
    // executeStatement();
    executeInsert();
  }
);


var Request = require('tedious').Request;

function executeStatement() {
  /* Read a long string, work fine */
  request = new Request("select OwnerId, Signature From dbo.Owner Where OwnerId = 36", function(err, rowCount) {
    if (err) {
      console.log(err);
    } else {
      console.log(rowCount + ' rows');
    }
  });

  request.on('row', function(columns) {
    columns.forEach(function(column) {
      console.log(column.value);
    });
  });

  connection.execSql(request);
}


function executeInsert() {
  /************************************************
   * Insert a long string, not working when
   *     packetSize = 4096
   * If
   *     packetSize = 16384
   * or higher, insertion works fine.
   *****************************************************/
  let s = '0123456789'.repeat(100000);
  request = new Request("Insert into dbo.Owner VALUES ('Rick', '" + s + "')", function(err, rowCount) {
    if (err) {
      console.log(err);
    } else {
      console.log(rowCount + ' rows');
    }
  });

  connection.execSql(request);
}
released

最も参考になるコメント

長いクエリを実行するとConnection lost - read ECONNRESETを取得しました。 encrypt: truepacketSize: 32768を使用すると、すべてが正常に機能します。

ノードバージョン: v12.6.0

全てのコメント9件

同様の問題が発生します。 長い文字列を挿入しようとしたとき。 少しテストした結果、ドッキングされた場合に使用されたnodejsバージョンに関連していることがわかりました。
node:10-slim->正常に動作します
node:12.3.1->正常に動作します
node:12.4を開始すると、ソケットのハングアップまたはソケットのクローズエラーが発生します。

こんにちは@ricklang 、私はこれを探していました。 「暗号化:真」はあなたの側で必須の設定ですか? そうでない場合は、falseに設定するか、デフォルト値-falseを使用してこれらの問題を解決できるオプションに設定しないでください。 なぜこれが起こっているのかを解明しようとしていましたが、その根本的な原因はわかりませんでした。 暗号化接続と非暗号化接続の両方のパケットを確認しました。パケットはまったく同じですが、1つを暗号化する場合、EOMを含む最後のパケットを受信すると、接続喪失エラーが受信されます。 もう少し調査して、何か掘り下げることができるかどうかを確認します。
一方、これについて@arthurschreiberに質問して、彼に洞察があるかどうかを確認することもできます。

encrypt: false,

エラーメッセージは次のとおりです(パケットサイズに関係なく):

RequestError: Requests can only be made in the LoggedIn state, not the SentPrelogin state
    at RequestError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\errors.js:32:12)
    at Connection.makeRequest (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1680:24)
    at Connection.execSql (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1459:10)
    at executeInsert (C:\work\GitRepos\tedious-test\index.js:67:14)
    at Connection.<anonymous> (C:\work\GitRepos\tedious-test\index.js:24:5)
    at Connection.emit (events.js:200:13)
    at Connection.message (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1871:18)
    at Connection.dispatchEvent (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1172:36)
    at MessageIO.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1045:14)
    at MessageIO.emit (events.js:200:13) {
  message: 'Requests can only be made in the ' +
    'LoggedIn state, not the SentPrelogin ' +
    'state',
  code: 'EINVALIDSTATE'

暗号化設定を削除しても、パケットサイズを4096のままにすると、エラーメッセージは次のようになります。

ConnectionError: Connection lost - write ECONNRESET
    at ConnectionError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:26)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    at Socket.emit (events.js:205:15)
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at onwriteError (_stream_writable.js:438:5)
    at onwrite (_stream_writable.js:459:5)
    at internal/streams/destroy.js:49:7
    at Socket._destroy (net.js:593:3)
    at Socket.destroy (internal/streams/destroy.js:37:8)
Emitted 'error' event at:
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:12)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    [... lines matching original stack trace ...]
    at Socket.destroy (internal/streams/destroy.js:37:8)
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:84:12) {
  message: 'Connection lost - write ECONNRESET',
  code: 'ESOCKET'

ただし、暗号化を設定せずにパケットサイズを16384に増やすと、すべて正常に機能します。

ちなみに、ノードのバージョンは12.4.0、12.6.0でテストしました。 同じ結果。

長いクエリを実行するとConnection lost - read ECONNRESETを取得しました。 encrypt: truepacketSize: 32768を使用すると、すべてが正常に機能します。

ノードバージョン: v12.6.0

@ wy193777追加のオプションがなく、ノードバージョンを最大12.3.1で使用して、すべてが機能するかどうかを確認できますか?

@susaresノードv12.3.1はpacketSizeオプションなしで動作します。

ノードバージョン:v12.2.0はpacketSizeオプションなしで動作します
ノードバージョン:v.12.6.0では、 Connection lost - read ECONNRESETエラーを回避するためにpacketSize: 8192を使用する必要がありました

これはhttps://github.com/nodejs/node/pull/27861が原因のようです。 どういうわけか、大きなTLSセグメントが書き込まれると、SQLServerは接続を終了します。 私には修正があり、まもなくPRを開きます。 👍

待つことができない人のために、これを修正するdiffは次のとおりです。

diff --git a/src/message-io.js b/src/message-io.js
index 90875f8..79f5da8 100644
--- a/src/message-io.js
+++ b/src/message-io.js
@@ -72,6 +72,8 @@ module.exports = class MessageIO extends EventEmitter {
       encrypted: duplexpair.socket2
     };

+    securePair.cleartext.setMaxSendFragment(this.outgoingMessageStream.packetSize);
+
     // If an error happens in the TLS layer, there is nothing we can do about it.
     // Forward the error to the socket so the connection gets properly cleaned up.
     securePair.cleartext.on('error', (err) => {

:tada:この問題はバージョン6.2.1で解決されました:tada:

このリリースは次の場所で入手できます。

セマンティックリリースボット:package :: rocket:

このページは役に立ちましたか?
0 / 5 - 0 評価