Skip to content

internal undici:fetch throws error where node-fetch and curl succeed #45497

@mknj

Description

@mknj

Version

v18.12.1

Platform

Linux xxx 5.10.0-19-amd64 #1 SMP Debian 5.10.149-2 (2022-10-21) x86_64 GNU/Linux

Subsystem

node:internal/deps/undici/undici:11000:53

What steps will reproduce the bug?

I do have some devices with commercial embedded web servers. I am not able to query them using the new fetch API.

They work fine with curl, web browsers and the 'old' node-fetch package.

The following example is self contained and tries to simulate such an device and triggers the same internal error.

If you uncomment the first line the code uses the node-fetch implementation and works fine.

//import fetch from 'node-fetch' // "^3.3.0"
import net from 'net'

const server = net.createServer((socket) => {
  const msg = "HTTP/1.1 200 OK\r\ncontent-Type: application/json\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n[1,2,3]"
  socket.write(msg)
  setTimeout(() => socket.end(), 1000) // simulates embedded web server
})
server.listen(1234)

server.on("listening", doFetch)

async function doFetch() {
  const url = 'http://127.0.0.1:1234'
  const res = await fetch(url)
  try {
    const data = await res.text() // internal fetch breaks here
    console.log('DATA' + data)
  } catch (e) {
    console.log(e)
  }
  server.close() // to stop process
}

How often does it reproduce? Is there a required condition?

every time

What is the expected behavior?

DATA[1,2,3]

What do you see instead?

TypeError: terminated
    at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
    at Fetch.emit (node:events:513:28)
    at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
    at Object.onError (node:internal/deps/undici/undici:11095:36)
    at Request.onError (node:internal/deps/undici/undici:6477:31)
    at errorRequest (node:internal/deps/undici/undici:8440:17)
    at Socket.onSocketClose (node:internal/deps/undici/undici:7895:9)
    at Socket.emit (node:events:513:28)
    at TCP.<anonymous> (node:net:313:12) {
  [cause]: SocketError: closed
      at Socket.onSocketClose (node:internal/deps/undici/undici:7883:35)
      at Socket.emit (node:events:513:28)
      at TCP.<anonymous> (node:net:313:12) {
    code: 'UND_ERR_SOCKET',
    socket: {
      localAddress: undefined,
      localPort: undefined,
      remoteAddress: undefined,
      remotePort: undefined,
      remoteFamily: undefined,
      timeout: undefined,
      bytesWritten: 171,
      bytesRead: 102
    }
  }
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions