Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mknj opened this issue Nov 17, 2022 · 2 comments
Closed

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

mknj opened this issue Nov 17, 2022 · 2 comments

Comments

@mknj
Copy link

mknj commented Nov 17, 2022

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

@climba03003
Copy link
Contributor

climba03003 commented Nov 17, 2022

Should be duplicate of nodejs/undici#1414 nodejs/undici#1412 nodejs/undici#1490
And working PR nodejs/undici#1540

@mknj
Copy link
Author

mknj commented Nov 17, 2022

Thanks for the repo link.

I wrote a undici test. It fails with undici v5.11.0(as in node@18.12.1) and it works with undici v5.12.0 and above. So it will hopefully be fixed with the next nodejs release ;)

$ ./node_modules/.bin/tap test/single.js

test/single.js

const { fetch } = require('..')
const { test } = require('tap')
const net = require('net')

test('succeeds on early server socket close', (t) => {
    t.plan(1)
    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.end(msg)
    })
      
    t.teardown(server.close.bind(server))
  
    server.listen(0, async () => {
      const res = await fetch(`http://localhost:${server.address().port}`)
      try {
        const data = await res.text() // internal fetch breaks here
        t.equal( data.toString(),"[1,2,3]")
      } catch (err) {
        t.error(err)
      }
    })
  })

@mknj mknj closed this as completed Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants