Skip to content

Commit 3f050d4

Browse files
authored
Remove leftover blocking assertion in TCP (#785)
From review of #782
1 parent fef47a5 commit 3f050d4

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

libc-bottom-half/sources/tcp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ static int tcp_read_eof(void *data) {
169169
tcp_socket_state_connected_t *state = &tcp->state.connected;
170170

171171
if (state->receive_result) {
172-
assert(tcp->blocking);
173172
sockets_result_void_error_code_t result;
174173
__wasilibc_future_block_on(sockets_future_result_void_error_code_read(
175174
state->receive_result, &result),

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ if (NOT (WASI STREQUAL "p1"))
415415
add_wasilibc_test(getaddrinfo.c NETWORK)
416416
add_wasilibc_test(sockets-nonblocking.c NETWORK)
417417
add_wasilibc_test(sockets-nonblocking-udp-no-connection.c NETWORK)
418+
add_wasilibc_test(sockets-eof-delayed.c NETWORK)
418419

419420
# Define executables for server/client tests, and they're paired together in
420421
# various combinations below for various tests.

test/src/sockets-eof-delayed.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "test.h"
2+
#include <arpa/inet.h>
3+
#include <errno.h>
4+
#include <fcntl.h>
5+
#include <netdb.h>
6+
#include <netinet/in.h>
7+
#include <poll.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <sys/socket.h>
12+
#include <unistd.h>
13+
14+
#define TEST(c) \
15+
do { \
16+
errno = 0; \
17+
if (!(c)) \
18+
t_error("%s failed (errno = %d)\n", #c, errno); \
19+
} while (0)
20+
21+
int main() {
22+
char buf[10];
23+
int listener_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
24+
25+
// Setup a listener bound to port 0 to have the OS assign us one.
26+
struct sockaddr_in server_address;
27+
socklen_t server_address_len = sizeof(server_address);
28+
server_address.sin_family = AF_INET;
29+
server_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
30+
server_address.sin_port = 0;
31+
TEST(bind(listener_fd, (struct sockaddr *)&server_address,
32+
sizeof(server_address)) != -1);
33+
TEST(getsockname(listener_fd, (struct sockaddr *)&server_address,
34+
&server_address_len) != -1);
35+
TEST(listen(listener_fd, 1) != -1);
36+
37+
// Connect a client to our server
38+
int client_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
39+
TEST(client_fd != -1);
40+
TEST(connect(client_fd, (struct sockaddr *)&server_address,
41+
server_address_len) != -1 ||
42+
errno == EINPROGRESS);
43+
44+
// Wait for the client to be writable, signaling the connection is there.
45+
struct pollfd client, listener;
46+
client.fd = client_fd;
47+
client.events = POLLWRNORM;
48+
TEST(poll(&client, 1, -1) == 1);
49+
50+
// Various ways of seeing that this isn't at EOF yet.
51+
TEST(read(client_fd, buf, sizeof(buf)) == -1 && errno == EWOULDBLOCK);
52+
client.events = POLLRDNORM;
53+
TEST(poll(&client, 1, 0) == 0);
54+
TEST(read(client_fd, buf, sizeof(buf)) == -1 && errno == EWOULDBLOCK);
55+
56+
// Accept the socket on the listener, then immediately close it.
57+
int server_fd;
58+
listener.fd = listener_fd;
59+
listener.events = POLLRDNORM;
60+
TEST(poll(&listener, 1, -1) == 1);
61+
TEST((server_fd = accept(listener_fd, NULL, NULL)) != -1);
62+
TEST(close(server_fd) != -1);
63+
64+
// Test to make sure that the client is now at EOF.
65+
TEST(poll(&client, 1, -1) == 1);
66+
TEST(read(client_fd, buf, sizeof(buf)) == 0);
67+
TEST(read(client_fd, buf, sizeof(buf)) == 0);
68+
69+
return t_status;
70+
}

0 commit comments

Comments
 (0)