Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/library_sockfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ addToLibrary({
#endif

Module['websocket'].emit('open', sock.stream.fd);
sock.connecting = false;

try {
var queued = peer.msg_send_queue.shift();
Expand Down Expand Up @@ -402,7 +403,15 @@ addToLibrary({

if ((dest && dest.socket.readyState === dest.socket.CLOSING) ||
(dest && dest.socket.readyState === dest.socket.CLOSED)) {
mask |= {{{ cDefs.POLLHUP }}};
// When an non-blocking connect fails mark the socket as writable.
// Its up to the calling code to then use getsockopt with SO_ERROR to
// retrieve the error.
// See https://man7.org/linux/man-pages/man2/connect.2.html
if (sock.connecting) {
mask |= {{{ cDefs.POLLOUT }}};
} else {
mask |= {{{ cDefs.POLLHUP }}};
}
}

return mask;
Expand Down Expand Up @@ -496,6 +505,7 @@ addToLibrary({
// because we cannot synchronously block to wait for the WebSocket
// connection to complete, we return here pretending that the connection
// was a success.
sock.connecting = true;
},
listen(sock, backlog) {
if (!ENVIRONMENT_IS_NODE) {
Expand Down
20 changes: 18 additions & 2 deletions test/sockets/test_sockets_echo_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif

typedef enum {
MSG_CONNECT,
MSG_READ,
MSG_WRITE
} msg_state_t;
Expand Down Expand Up @@ -109,6 +110,21 @@ void main_loop() {
}
}

if (server.state == MSG_CONNECT) {
if (!FD_ISSET(server.fd, &fdw)) {
return;
}
int error = 0;
socklen_t len = sizeof(error);
int ret = getsockopt(server.fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (error != 0) {
printf("connect failed: %s\n", strerror(error));
finish(EXIT_FAILURE);
}
printf("connected\n");
server.state = MSG_WRITE;
}

if (server.state == MSG_WRITE) {
if (!FD_ISSET(server.fd, &fdw)) {
return;
Expand Down Expand Up @@ -160,7 +176,7 @@ int main() {
int res;

memset(&server, 0, sizeof(server_t));
server.state = MSG_WRITE;
server.state = MSG_CONNECT;

// setup the message we're going to echo
memset(&echo_msg, 0, sizeof(msg_t));
Expand Down Expand Up @@ -191,7 +207,7 @@ int main() {
perror("inet_pton failed");
finish(EXIT_FAILURE);
}

res = connect(server.fd, (struct sockaddr *)&addr, sizeof(addr));
if (res == -1 && errno != EINPROGRESS) {
perror("connect failed");
Expand Down
5 changes: 4 additions & 1 deletion test/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import clang_native
import common
from common import BrowserCore, no_windows, create_file, test_file, read_file
from common import parameterized, requires_native_clang, crossplatform, PYTHON
from common import parameterized, requires_native_clang, crossplatform, PYTHON, NON_ZERO
from tools import config, utils
from tools.shared import EMCC, path_from_root, run_process, CLANG_CC

Expand Down Expand Up @@ -292,6 +292,9 @@ def test_nodejs_sockets_echo(self, harness_class, port, args):
expected = 'do_msg_read: read 14 bytes'
self.do_runf('sockets/test_sockets_echo_client.c', expected, emcc_args=['-DSOCKK=%d' % harness.listen_port] + args)

def test_nodejs_sockets_connect_failure(self):
self.do_runf('sockets/test_sockets_echo_client.c', 'connect failed: Connection refused', emcc_args=['-DSOCKK=666'], assert_returncode=NON_ZERO)

@requires_native_clang
def test_nodejs_sockets_echo_subprotocol(self):
# Test against a Websockified server with compile time configured WebSocket subprotocol. We use a Websockified
Expand Down
Loading