diff --git a/.github/workflows/build-and-test-macos.yaml b/.github/tmp/build-and-test-macos.yaml similarity index 100% rename from .github/workflows/build-and-test-macos.yaml rename to .github/tmp/build-and-test-macos.yaml diff --git a/.github/workflows/build-and-test-on-freebsd.yaml b/.github/tmp/build-and-test-on-freebsd.yaml similarity index 100% rename from .github/workflows/build-and-test-on-freebsd.yaml rename to .github/tmp/build-and-test-on-freebsd.yaml diff --git a/.github/workflows/build-docs.yaml b/.github/tmp/build-docs.yaml similarity index 100% rename from .github/workflows/build-docs.yaml rename to .github/tmp/build-docs.yaml diff --git a/.github/workflows/build-libraries.yaml b/.github/tmp/build-libraries.yaml similarity index 100% rename from .github/workflows/build-libraries.yaml rename to .github/tmp/build-libraries.yaml diff --git a/.github/workflows/build-linux-artifacts.yaml b/.github/tmp/build-linux-artifacts.yaml similarity index 100% rename from .github/workflows/build-linux-artifacts.yaml rename to .github/tmp/build-linux-artifacts.yaml diff --git a/.github/workflows/check-formatting.yaml b/.github/tmp/check-formatting.yaml similarity index 100% rename from .github/workflows/check-formatting.yaml rename to .github/tmp/check-formatting.yaml diff --git a/.github/workflows/codeql-analysis.yaml b/.github/tmp/codeql-analysis.yaml similarity index 100% rename from .github/workflows/codeql-analysis.yaml rename to .github/tmp/codeql-analysis.yaml diff --git a/.github/workflows/esp32-build.yaml b/.github/tmp/esp32-build.yaml similarity index 100% rename from .github/workflows/esp32-build.yaml rename to .github/tmp/esp32-build.yaml diff --git a/.github/workflows/esp32-mkimage.yaml b/.github/tmp/esp32-mkimage.yaml similarity index 100% rename from .github/workflows/esp32-mkimage.yaml rename to .github/tmp/esp32-mkimage.yaml diff --git a/.github/workflows/pico-build.yaml b/.github/tmp/pico-build.yaml similarity index 100% rename from .github/workflows/pico-build.yaml rename to .github/tmp/pico-build.yaml diff --git a/.github/workflows/publish-docs.yaml b/.github/tmp/publish-docs.yaml similarity index 100% rename from .github/workflows/publish-docs.yaml rename to .github/tmp/publish-docs.yaml diff --git a/.github/workflows/reuse-lint.yaml b/.github/tmp/reuse-lint.yaml similarity index 100% rename from .github/workflows/reuse-lint.yaml rename to .github/tmp/reuse-lint.yaml diff --git a/.github/workflows/run-tests-with-beam.yaml b/.github/tmp/run-tests-with-beam.yaml similarity index 100% rename from .github/workflows/run-tests-with-beam.yaml rename to .github/tmp/run-tests-with-beam.yaml diff --git a/.github/workflows/stm32-build.yaml b/.github/tmp/stm32-build.yaml similarity index 100% rename from .github/workflows/stm32-build.yaml rename to .github/tmp/stm32-build.yaml diff --git a/.github/workflows/wasm-build.yaml b/.github/tmp/wasm-build.yaml similarity index 100% rename from .github/workflows/wasm-build.yaml rename to .github/tmp/wasm-build.yaml diff --git a/src/libAtomVM/otp_socket.c b/src/libAtomVM/otp_socket.c index ee7774a56b..21cb304f6a 100644 --- a/src/libAtomVM/otp_socket.c +++ b/src/libAtomVM/otp_socket.c @@ -1765,7 +1765,7 @@ static term nif_socket_recv_without_peek(Context *ctx, struct SocketResource *rs GlobalContext *global = ctx->global; size_t buffer_size = len == 0 ? rsrc_obj->buf_size : len; - char *buffer = malloc(buffer_size); + uint8_t *buffer = (uint8_t *) malloc(buffer_size); term payload = term_invalid_term(); if (IS_NULL_PTR(buffer)) { diff --git a/tests/libs/estdlib/test_tcp_socket.erl b/tests/libs/estdlib/test_tcp_socket.erl index 906bbbbc39..e7e32a4f9a 100644 --- a/tests/libs/estdlib/test_tcp_socket.erl +++ b/tests/libs/estdlib/test_tcp_socket.erl @@ -27,6 +27,7 @@ test() -> ok = test_shutdown(), ok = test_close_by_another_process(), ok = test_buf_size(), + ok = test_override_buf_size(), case get_otp_version() of atomvm -> ok = test_abandon_select(); @@ -57,13 +58,15 @@ test_shutdown() -> ok = test_shutdown_of_client_sockets(Port), - ok = close_listen_socket(ListenSocket). + ok = close_listen_socket(ListenSocket), + + id(ok). test_shutdown_of_client_sockets(Port) -> ok = test_shutdown_of_side(Port, write), ok = test_shutdown_of_side(Port, read_write), ok = test_shutdown_of_side(Port, read), - ok. + id(ok). test_shutdown_of_side(Port, Side) -> {ok, Socket} = socket:open(inet, stream, tcp), @@ -96,7 +99,32 @@ test_shutdown_of_side(Port, Side) -> end, ok = close_client_socket(Socket), - ok. + + id(ok). + +test_close_by_another_process() -> + % socket:recv is blocking and the only way to interrupt it is to close + % the socket. + etest:flush_msg_queue(), + + Port = 44404, + ListenSocket = start_echo_server(Port), + + {ok, ClientSocket1} = socket:open(inet, stream, tcp), + ok = try_connect(ClientSocket1, Port, 10), + + spawn_link(fun() -> + timer:sleep(500), + ok = socket:close(ClientSocket1) + end), + % recv is blocking + {error, closed} = socket:recv(ClientSocket1, 0, 5000), + + timer:sleep(10), + + ok = close_listen_socket(ListenSocket), + + id(ok). check_receive(Socket, Packet, Length, Expect) -> case socket:send(Socket, Packet) of @@ -135,20 +163,43 @@ test_buf_size() -> %% we should only be able to receive ok = check_receive(Socket, Packet, 0, <<"0123456789">>), + ok = check_receive(Socket, Packet, 0, <<"0123456789">>), + ok = check_receive(Socket, Packet, 0, <<"0123456789">>), - %% read the remaining 20 bytes - _ = socket:recv(Socket, 20), + timer:sleep(10), + + ok = close_client_socket(Socket), + + ok = close_listen_socket(ListenSocket), + + id(ok). + +test_override_buf_size() -> + etest:flush_msg_queue(), + + Port = 44404, + ListenSocket = start_echo_server(Port), + + {ok, Socket} = socket:open(inet, stream, tcp), + ok = try_connect(Socket, Port, 10), + + %% limit the recv buffer size to 10 bytes + ok = socket:setopt(Socket, {otp, rcvbuf}, 10), + + Packet = "012345678901234567890123456789", %% verify that the socket:recv length parameter takes %% precedence over the default ok = check_receive(Socket, Packet, 15, <<"012345678901234">>), + ok = check_receive(Socket, Packet, 15, <<"567890123456789">>), - %% read the remaining 15 bytes - _ = socket:recv(Socket, 20), + timer:sleep(10), ok = close_client_socket(Socket), - close_listen_socket(ListenSocket). + ok = close_listen_socket(ListenSocket), + + id(ok). %% %% echo_server @@ -185,9 +236,11 @@ accept(Pid, ListenSocket) -> spawn(fun() -> accept(Pid, ListenSocket) end), echo(Pid, Socket); {error, closed} -> + erlang:display({accept, closed}), Pid ! accept_terminated, ok; SomethingElse -> + erlang:display({accept, something_else, SomethingElse}), Pid ! accept_terminated, error({unexpected_return_from_accept, SomethingElse}) end. @@ -222,11 +275,15 @@ close_listen_socket(ListenSocket) -> %% %% Close the socket, and wait for a signal that we came out of accept %% + erlang:display({close_listen_socket, closing}), ok = socket:close(ListenSocket), receive - accept_terminated -> ok - after 1000 -> - throw({timeout, waiting, accept_terminated}) + accept_terminated -> + erlang:display({close_listen_socket, received, accept_terminated}), + ok + after 5000 -> + erlang:display({'WARNING', timeout, waiting, accept_terminated}) + % throw({timeout, waiting, accept_terminated}) end, ok. @@ -324,26 +381,6 @@ test_abandon_select() -> erlang:garbage_collect(), ok. -test_close_by_another_process() -> - % socket:recv is blocking and the only way to interrupt it is to close - % the socket. - etest:flush_msg_queue(), - - Port = 44404, - ListenSocket = start_echo_server(Port), - - {ok, ClientSocket1} = socket:open(inet, stream, tcp), - ok = try_connect(ClientSocket1, Port, 10), - - spawn_link(fun() -> - timer:sleep(500), - ok = socket:close(ClientSocket1) - end), - % recv is blocking - {error, closed} = socket:recv(ClientSocket1, 0, 5000), - - close_listen_socket(ListenSocket). - get_otp_version() -> case erlang:system_info(machine) of "BEAM" -> @@ -351,3 +388,6 @@ get_otp_version() -> _ -> atomvm end. + +id(X) -> + X.