You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a particular scenario where calling Client.end will result in a hanging promise when using Cloudflare's socket implementation and the socket does not successfully connect. I've found that this can be reproduced consistently by specifying an incorrect port in the connection string. This only occurs when using the Cloudflare runtime and CloudflareSocket.
// use a connection string with an unreachable port hereconstclient=newClient({ connectionString });try{awaitclient.connect();awaitclient.query(`SELECT 1`);}finally{// ⬇️ hangs hereawaitclient.end();}
I believe I've found the root causes to it. The Socket.closed promise on the Cloudflare socket as used here does not resolve if the connection never successfully opened to begin with. Connection expects the event here to bubble out the end event to Clienthere and resolve the promise or invoke the callback.
Additionally, the stream.end callback is also not invoked in Connection.end. That's from the CloudflareSocket.write function expecting the callback to always be in the third argument here, where Connection.end passes it as the second argument here.
I made the following naive patches that resolve the issue in my case, though I'm not sure it's the best resolution. I'm expecting that successfully connected sockets would emit the close event twice with this patch.
diff --git a/node_modules/pg-cloudflare/dist/index.js b/node_modules/pg-cloudflare/dist/index.js
index e2db57e..f94b723 100644
--- a/node_modules/pg-cloudflare/dist/index.js+++ b/node_modules/pg-cloudflare/dist/index.js@@ -89,7 +89,8 @@ export class CloudflareSocket extends EventEmitter {
end(data = Buffer.alloc(0), encoding = 'utf8', callback = () => { }) {
log('ending CF socket');
this.write(data, encoding, (err) => {
- this._cfSocket.close();+ // the closed Promise does not resolve if the connection was never open to begin with+ this._cfSocket.close().then(() => this.emit('close'));
if (callback)
callback(err);
});
There is a particular scenario where calling
Client.end
will result in a hanging promise when using Cloudflare's socket implementation and the socket does not successfully connect. I've found that this can be reproduced consistently by specifying an incorrect port in the connection string. This only occurs when using the Cloudflare runtime andCloudflareSocket
.I believe I've found the root causes to it. The
Socket.closed
promise on the Cloudflare socket as used here does not resolve if the connection never successfully opened to begin with.Connection
expects the event here to bubble out theend
event toClient
here and resolve the promise or invoke the callback.Additionally, the
stream.end
callback is also not invoked inConnection.end
. That's from theCloudflareSocket.write
function expecting the callback to always be in the third argument here, whereConnection.end
passes it as the second argument here.I made the following naive patches that resolve the issue in my case, though I'm not sure it's the best resolution. I'm expecting that successfully connected sockets would emit the
close
event twice with this patch.The text was updated successfully, but these errors were encountered: