workerd icon indicating copy to clipboard operation
workerd copied to clipboard

🐛 BUG: TCP Stream read() function gets stuck

Open JamesDelfini opened this issue 2 years ago • 3 comments

Which Cloudflare product(s) does this pertain to?

Workers for Platforms, Workers Runtime

What version(s) of the tool(s) are you using?

3.18.0 [Wrangler]

What version of Node are you using?

v20.11.0

What operating system and version are you using?

Windows 11

Describe the Bug

After performing a write operation and expecting a response, the issue arises when calling read() again. It gets stuck in this scenario.

import { connect } from 'cloudflare:sockets'

const socket = connect(...);

socket.writable.getWriter().write(...);
const { value: bytesRead } = socket.readable.getReader().read(); // Valid: Expected a response from the server
// Tries to read again to reproduce the behaviour
socket.readable.getReader().read(); // Invalid: The program got stuck here and there are no returns or error throwns

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

JamesDelfini avatar Jan 31 '24 12:01 JamesDelfini

Hello,

I experienced the same bug, the read method work as expected if a single read is enough to get the full socket response, but if the method is called more than once the script will get stuck.

  • The first read() return the full response => fine
  • The first read() return the full response and read() is called a second time => stuck
  • Multiple read() are needed to get the full response => stuck

I also noticed that the object returned by the read() method {done: false, value: 'something'} always return done: false which might be the source of the bug ?

Here is a playground script exposing the bug

nicolasbeauvais avatar Jun 23 '24 14:06 nicolasbeauvais

@nicolasbeauvais why do you expect it to return done: true? In your example the server keeps the connection open, so the socket isn't closed immediately after the data is sent. If you add a connection: close header then it doesn't get stuck anymore.

dom96 avatar Jul 08 '24 14:07 dom96

Thank you for fixing my playground example @dom96, the read() behaviour makes more sense now :bow:

nicolasbeauvais avatar Jul 11 '24 13:07 nicolasbeauvais