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

createConnection(sockPath).on('error' from 'net' errors are not reported correctly #16481

Open
benorgera opened this issue Jan 17, 2025 · 0 comments
Labels
bug Something isn't working node:net

Comments

@benorgera
Copy link

benorgera commented Jan 17, 2025

What version of Bun is running?

1.1.44+ec11ea4e7

What platform is your computer?

Linux 5.15.0-130-generic x86_64 x86_64

What steps can reproduce the bug?

In publisher.ts

import { createServer, Socket } from 'net';

const FEED_SOCKETS_BASE_PATH = '/tmp/';

const unlinkFeeds: Array<string> = [];
const createFeedPublisher = async (feedName: string) => {
    const sockName = FEED_SOCKETS_BASE_PATH + feedName;
    unlinkFeeds.push(sockName);

    const listeners: { [key: number]: Socket } = [];
    const server = createServer((socket) => {
        const uid = Object.keys(listeners).length;
        listeners[uid] = socket;

        console.log(`A listener ${uid} connected to feed ${feedName}. Num listeners ${Object.keys(listeners).length}`);

        socket.on('end', () => {
            delete listeners[uid];
            console.log(`Listener ${uid} disconnected from feed ${feedName}. Num listeners ${Object.keys(listeners).length}`);
        });
    
        socket.on('error', (err) => {
            delete listeners[uid];
            console.error(`Listener ${uid} error on feed ${feedName}, deleted listener. Num listeners ${Object.keys(listeners).length}`, err);
        });
    });

    server.listen(sockName, () => {
        console.log(`Feed ${feedName} server running on ${sockName}`);
    });

    return (data: object) => {
        const str = JSON.stringify(data);

        for (const sock of Object.values(listeners)) {
            sock.write(str + '\n');
        }
    };
};


const publishFeed = await createFeedPublisher('testFeed');

setInterval(() => {
    publishFeed(undefined as any)
}, 1000);

In listener.ts

const FEED_SOCKETS_BASE_PATH = '/tmp/';

import { createConnection } from 'net';

const subscribeFeed = async (feedName: string, dataCallback: (json: object) => void) => {
    const sockPath = FEED_SOCKETS_BASE_PATH + feedName;

    const connect = () => {
        const reconnect = () => {
            setTimeout(() => {
                console.log(`Reconnecting to feed ${feedName}`);
                connect();
            }, 1000);
        };

        let client = createConnection(sockPath);

        client.on('connect', () => {
            console.log(`Connected to feed ${feedName} socket`);
        });
      
        client.on('data', (dataStr: string) => dataCallback(JSON.parse(dataStr)));
      
        client.on('end', () => {
            console.error(`Connection to feed ${feedName} closed, attempting reconnection`);
            reconnect();
        });
      
        client.on('error', (err) => {
            console.error(err)
            client.destroy()
            reconnect()
        });
    };

    connect();
};

subscribeFeed('testFeed', data => {
    console.log(data)
});

Then bun publisher.ts in one terminal followed by bun listener.ts in another terminal

What is the expected behavior?

The second terminal repeatedly outputs a javascript JSON parse error

What do you see instead?

The second terminal repeatedly outputs a [native code: Exception] error

Additional information

No response

@benorgera benorgera added bug Something isn't working needs triage labels Jan 17, 2025
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:net
Projects
None yet
Development

No branches or pull requests

2 participants