Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trylovetom opened this issue Oct 24, 2023 · 2 comments
Closed
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@trylovetom
Copy link

trylovetom commented Oct 24, 2023

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 trylovetom added the bug Something isn't working label Oct 24, 2023
@trylovetom trylovetom changed the title Net module is missing some Event Some events from the Net module are not being triggered as expected. Oct 24, 2023
@trylovetom
Copy link
Author

trylovetom commented Oct 24, 2023

related to: redis/ioredis#1830, taskforcesh/bullmq#2237, #6487

@nektro
Copy link
Contributor

nektro commented May 9, 2024

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" ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

3 participants