🐛 BUG: TCP Stream read() function gets stuck
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
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 andread()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 ?
@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.
Thank you for fixing my playground example @dom96, the read() behaviour makes more sense now :bow: