TCP Sockets read() function got stuck

Is there a way to handle the timeout when using TCP sockets? When this is called and the server has no response, the Cloudflare Worker get stuck at the read() function. Not confirmed in production, it might also get stuck there.
import { connect } from 'cloudflare:sockets'

connect(...).socket.readable.getReader().read()
import { connect } from 'cloudflare:sockets'

connect(...).socket.readable.getReader().read()
2 Replies
Kitodi
Kitodi5mo ago
Similar use case when I do write and expected to receive a response, you do the write() first then read() but when try to call again the read() it will get stuck.
import { connect } from 'cloudflare:sockets'

const socket = connect(...);

socket.writable.getWriter().write(...);
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
import { connect } from 'cloudflare:sockets'

const socket = connect(...);

socket.writable.getWriter().write(...);
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
Kitodi
Kitodi5mo ago
Created GitHub issue in case if this is a bug or it is intended and needed more action to prevent this happening. https://github.com/cloudflare/workers-sdk/issues/4882
GitHub
🐛 BUG: TCP Stream read() function gets stuck · Issue #4882 · cloudf...
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...