这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@partic2
Copy link
Contributor

@partic2 partic2 commented May 20, 2025

No description provided.

@saghul
Copy link
Owner

saghul commented May 20, 2025

What are you fixing here? Can you create a test case?

@partic2
Copy link
Contributor Author

partic2 commented May 21, 2025

I'm trying to implementing a http server on txiki.js.
When I use Firefox(on Windows) to test the server, Some connections worked fine, Some connections didn't response any data(also didn't refused).
After checking the code, I guess the cause is that tjs doesn't accept socket if there is NO pending accept call from JS side.

txiki.js/src/mod_streams.c

Lines 341 to 344 in 6e01bf4

if (!TJS_IsPromisePending(s->ctx, &s->accept.result)) {
// TODO - handle this.
return;
}

And github copilot tell me

"Do NOT call uv_accept outside the connection callback (uv_connection_cb)."

So this patch queue socket to avoid socket lost.

@saghul
Copy link
Owner

saghul commented May 21, 2025

It must be something else, that advice is wrong.

See: https://github.com/libuv/libuv/blob/71ec5c0fcdd867b64c46ade1e0a6b59101281a4a/src/unix/stream.c#L528

@saghul
Copy link
Owner

saghul commented May 21, 2025

That said, I do have plans to change the socket API to align it with Chrome's direct sockets API and WinterTC's one, so I'll take a look then.

@partic2
Copy link
Contributor Author

partic2 commented May 21, 2025

I'll show the difference.

Below code is a simple echo server, expected to print 'hello'
But it will print nothing on current version txiki.js,
And it will print "hello" on this patch.

async function test() {
    let serv = await tjs.listen('tcp', '127.0.0.1', 1180);
    (async () => {
        const conn = await tjs.connect('tcp', '127.0.0.1', 1180);
        await conn.write(new TextEncoder().encode('hello'));
        let msg = new Uint8Array(10);
        msg = msg.subarray(0, (await conn.read(msg)));
        console.info(new TextDecoder().decode(msg));
    })();
    await new Promise((resolve)=>setTimeout(resolve, 1000));
    while (true) {
        const conn = await serv.accept();
        let msg = new Uint8Array(10);
        msg = msg.subarray(0, (await conn.read(msg)));
        await conn.write(msg);
    }
}
test()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants