Some applications (in our case, Langflow) attempt to find an open host port to run on by probing random ports. However, this currently does not work, as our connection always succeeds even if a port is not open.
Below is a simplified C version of code that works on native, but not on WASIX.
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static int connect_ex(const char *host, int port) {
struct addrinfo hints;
struct addrinfo *result = NULL;
char port_str[16];
int fd = -1;
int status = 0;
snprintf(port_str, sizeof(port_str), "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, port_str, &hints, &result) != 0) {
status = errno;
printf("getaddrinfo failed (errno = %d)\n", status);
return status;
}
fd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (fd < 0) {
status = errno;
printf("socket failed (errno = %d)\n", status);
freeaddrinfo(result);
return status;
}
int connect_result = connect(fd, result->ai_addr, result->ai_addrlen);
if (connect_result != 0) {
status = errno;
}
printf("connect returned %d (errno = %d)\n", connect_result, status);
close(fd);
freeaddrinfo(result);
return status;
}
static bool is_port_in_use(int port, const char *host) {
return connect_ex(host, port) == 0;
}
static int get_free_port(int port, const char *host) {
while (is_port_in_use(port, host)) {
port += 1;
}
return port;
}
int main(int argc, char **argv) {
const char *host = "localhost";
int port = 8000;
char *end = NULL;
if (argc > 1) {
port = (int)strtol(argv[1], &end, 10);
if (end == argv[1] || *end != '\0') {
fprintf(stderr, "Invalid port: %s\n", argv[1]);
return 1;
}
}
if (argc > 2) {
host = argv[2];
}
printf("Checking port %d, %s\n", port, host);
if (is_port_in_use(port, host)) {
printf("Port %d, %s is already in use. Finding a free port...\n", port, host);
port = get_free_port(port, host);
}
printf("Using port %d, %s\n", port, host);
return 0;
}
Compile and run on native with
clang freeport.c -o freeport.native
./freeport.native
Checking port 8000, localhost
connect returned -1 (errno = 111)
Using port 8000, localhost
Compile and run on WASIX
wasixcc -fwasm-exceptions freeport.c -o freeport
wasmer run --net ./freeport
Checking port 8000, localhost
connect returned 0 (errno = 0)
Port 8000, localhost is already in use. Finding a free port...
connect returned 0 (errno = 0)
connect returned 0 (errno = 0)
...
Some applications (in our case, Langflow) attempt to find an open host port to run on by probing random ports. However, this currently does not work, as our connection always succeeds even if a port is not open.
Below is a simplified C version of code that works on native, but not on WASIX.
Compile and run on native with
Compile and run on WASIX