Skip to content

tls.connect from node:tls errors with ECONNRESET after sending data #27652

Closed
@magurotuna

Description

@magurotuna

Description

TLS connection established with tls.connect from node:tls errors out with ECONNRESET after sending data to the server.

Reproducible code snippet

import * as tls from 'node:tls';

function connectToExampleCom() {
  const { promise, resolve, reject } = Promise.withResolvers();

  const options = {
    host: 'example.com',
    port: 443,
  };

  const client = tls.connect(options, () => {
    console.log('Connected to server');

    // Send an HTTP GET request
    client.write('GET / HTTP/1.1\r\n');
    client.write('Host: example.com\r\n');
    client.write('Connection: close\r\n\r\n');
  });

  let resp = '';
  client.on('data', (data) => {
    resp += data.toString();
  });

  client.on('end', () => {
    console.log('Connection closed by server');
    resolve(resp);
  });

  client.on('error', (err) => {
    reject(err);
  });

  return promise;
}

const resp = await connectToExampleCom();
console.log(resp);

Output

$ deno run --allow-net index.mjs
Connected to server
error: Uncaught (in promise) Error: read ECONNRESET
    at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:93:9)
    at __node_internal_errnoException (ext:deno_node/internal/errors.ts:141:10)
    at TCP.onStreamRead (ext:deno_node/internal/stream_base_commons.ts:209:20)
    at TCP.#read (ext:deno_node/internal_binding/stream_wrap.ts:252:12)
    at eventLoopTick (ext:core/01_core.js:175:7)

Version

deno 2.1.5 (stable, release, aarch64-apple-darwin)
v8 13.0.245.12-rusty
typescript 5.6.2

Additonal info

I did git bisect and found out that this issue seems to have started at c3c2b37 (which is included in v2.1.0).

Both Node.js (v22.11.0) and Bun (v1.1.42) work fine. Here's the result of Node.js:

$ node index.mjs
Connected to server
Connection closed by server
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 499485
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 13 Jan 2025 14:33:12 GMT
Etag: "3147526947"
Expires: Mon, 20 Jan 2025 14:33:12 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (lac/55A7)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
Connection: close

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    ... omitted ...
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions