MeWrite Docs

Socket: ECONNRESET - Connection reset by peer

TCP接続が相手側からリセットされた場合のエラー原因と解決策

概要

TCP接続が相手側から強制的にリセット(RST)された場合のエラーです。

エラーメッセージ

``` Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:205:27) ```

原因

  1. サーバーの再起動: 接続中にサーバーが再起動
  2. タイムアウト: サーバー側のアイドルタイムアウト
  3. ファイアウォール: 長時間アイドル接続の切断
  4. アプリケーションクラッシュ: サーバー側プロセスの異常終了

解決策

1. Keep-Aliveを設定

```javascript // Node.js HTTP Agent const http = require(‘http’);

const agent = new http.Agent({ keepAlive: true, keepAliveMsecs: 30000, });

// axios での設定 const axios = require(‘axios’); axios.defaults.httpAgent = agent; axios.defaults.httpsAgent = new https.Agent({ keepAlive: true }); ```

2. リトライロジックを実装

```javascript async function fetchWithRetry(url, options = {}, retries = 3) { for (let i = 0; i < retries; i++) { try { return await fetch(url, options); } catch (err) { if (err.code === ‘ECONNRESET’ && i < retries - 1) { await new Promise(r => setTimeout(r, 1000 * Math.pow(2, i))); continue; } throw err; } } } ```

3. 接続プールの設定

```javascript // データベース接続プール const pool = mysql.createPool({ connectionLimit: 10, acquireTimeout: 10000, waitForConnections: true, idleTimeout: 60000, });

// 接続エラーハンドリング pool.on(’error’, (err) => { if (err.code === ‘ECONNRESET’) { console.log(‘Connection reset, will reconnect’); } }); ```

4. ロードバランサーの設定確認

```nginx

nginx

upstream backend { server 127.0.0.1:3000; keepalive 32; }

server { location /api { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection “”; } } ```

5. TCPキープアライブ(システムレベル)

```bash

Linux

echo 60 > /proc/sys/net/ipv4/tcp_keepalive_time echo 10 > /proc/sys/net/ipv4/tcp_keepalive_intvl echo 6 > /proc/sys/net/ipv4/tcp_keepalive_probes ```

よくある間違い

  • エラーを無視してリトライしない
  • Keep-Aliveなしで長時間接続
  • サーバー側のタイムアウト設定を考慮しない

関連エラー

関連エラー

Network の他のエラー

最終更新: 2025-12-10