bun icon indicating copy to clipboard operation
bun copied to clipboard

Some events from the Net module are not being triggered as expected.

Open trylovetom opened this issue 2 years ago • 2 comments

What version of Bun is running?

1.0.7

What platform is your computer?

Darwin 22.1.0 x86_64 i386

What steps can reproduce the bug?

Below are the execution results for following of code, one using Node.js (v18.18.2) and the other using Bun (v1.0.7).

1. Using the IORedis library

test-ioredis.js

const connection = new IORedis();

connection.on("ready", () => {
  console.log("connection ready");
});

connection.on("error", (error) => {
  console.error(error);
});

connection.on("end", () => {
  console.log("connection ended");
});

setTimeout(() => {
  console.log("connection disconnecting");
  connection.disconnect();
  console.log("connection disconnected");
}, 1000);

Node.js (v18.18.2)

% node test-ioredis.js
connection ready
connection disconnecting
connection disconnected
connection ended

Bun (v1.0.7)

% bun test-ioredis.js
connection ready
connection disconnecting
connection disconnected

2. Using the Node.js net module

test-net.js

const net = require('node:net');

const client = net.connect({ host: 'www.google.com', port: '80' });

client.on('connect', () => console.log('connect'));
client.on('ready', () => console.log('ready'));
client.on('end', () => console.log('end'));
client.on('close', () => console.log('close'));

setTimeout(() => client.end(), 1000);

Node.js (v18.18.2)

% node test-net.js
connect
ready
end
close

Bun (v1.0.7)

% bun test-net.js
close
close

What is the expected behavior?

% bun test-ioredis.js
connection ready
connection disconnecting
connection disconnected
connection ended

% bun test-net.js
connect
ready
end
close

What do you see instead?

% bun test-ioredis.js
connection ready
connection disconnecting
connection disconnected

% bun test-net.js 
close
close

Additional information

I was wondering, it seems that ioredis is implemented using the node:net module underneath. I suspect that the event emiter of Bun environment might be missing something event or it's logic differs.

In other words, the "connect", "ready" and "end" event is not emitted.

Why is this important

There are circumstances when you need to know that a connection has indeed been disconnected. For example, say you have a blocking command that is running and you need to close without waiting for said command, then you can issue a call to disconnect, and we would like to know has been disconnected. Furthermore, according to the documentation it should work, and also it works in the first case (because by the time we try to disconnect the connection is not ready yet).

trylovetom avatar Oct 24 '23 10:10 trylovetom

related to: https://github.com/redis/ioredis/issues/1830, https://github.com/taskforcesh/bullmq/issues/2237, https://github.com/oven-sh/bun/pull/6487

trylovetom avatar Oct 24 '23 10:10 trylovetom

in present versions of bun this is now far closer to accurate (included tracking of all events)

❯ node index.js 
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'lookup' ]
[ 'client', 'connectionAttempt' ]
[ 'client', 'connect' ]
[ 'client', 'ready' ]
[ 'client', 'prefinish' ]
[ 'client', 'finish' ]
[ 'client', 'readable' ]
[ 'client', 'end' ]
[ 'client', 'close' ]
❯ bun-debug index.js 
[ "client", "connect" ]
[ "client", "ready" ]
[ "client", "end" ]
[ "client", "prefinish" ]
[ "client", "finish" ]
[ "client", "close" ]

nektro avatar May 09 '24 23:05 nektro