From 592ab236dc9d62868b0841f0813b988fcd43b7dc Mon Sep 17 00:00:00 2001 From: "Yuri S." Date: Wed, 18 Nov 2026 23:49:45 +0500 Subject: [PATCH 1/2] fix: deliver frames split across reads in WebSocket active once mode --- src/hackney_ws.erl | 5 +++++ test/hackney_ws_tests.erl | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/hackney_ws.erl b/src/hackney_ws.erl index 275ce2a5..808e0dba 100644 --- a/src/hackney_ws.erl +++ b/src/hackney_ws.erl @@ -930,6 +930,11 @@ handle_active_data(SocketData, #ws_data{buffer = Buffer, owner = Owner, active = end, Frames), %% Handle active mode case Active of + once when Frames =:= [] -> + %% Only part of a frame has arrived: keep reading until + %% a whole frame can be delivered. + _ = set_socket_active(Data2, once), + {keep_state, Data2}; once -> _ = set_socket_active(Data2, false), {keep_state, Data2#ws_data{active = false}}; diff --git a/test/hackney_ws_tests.erl b/test/hackney_ws_tests.erl index 83e5e954..d0de5a8f 100644 --- a/test/hackney_ws_tests.erl +++ b/test/hackney_ws_tests.erl @@ -114,6 +114,8 @@ ws_integration_test_() -> fun() -> test_ping_pong(WsUrl) end}, {"Active mode once", fun() -> test_active_once(WsUrl) end}, + {"Active mode once with a frame split across reads", + fun() -> test_active_once_split_frame(WsUrl) end}, {"Active mode true", fun() -> test_active_true(WsUrl) end} ] @@ -209,6 +211,16 @@ test_ping_pong(WsUrl) -> ?assertMatch({ok, pong}, Result), hackney:ws_close(Ws). +test_active_once_split_frame(WsUrl) -> + {ok, Ws} = hackney:ws_connect(WsUrl, [{active, once}]), + ok = hackney:ws_send(Ws, {text, <<"large:1048576">>}), + receive + {hackney_ws, Ws, {binary, Bin}} -> ?assertEqual(1048576, byte_size(Bin)) + after 3000 -> + ?assert(false) + end, + hackney:ws_close(Ws). + test_active_once(WsUrl) -> {ok, Ws} = hackney:ws_connect(WsUrl, [{active, once}]), From a997ab82f81c2bdee09c7b9887a211ba73754659 Mon Sep 17 00:00:00 2001 From: "Yuri S." Date: Sat, 21 Nov 2026 04:40:24 +0500 Subject: [PATCH 2/2] test: check active once delivers one large frame per setopts Two 1 MiB frames are sent; the second must arrive only after ws_setopts re-arms {active, once}. --- test/hackney_ws_tests.erl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/hackney_ws_tests.erl b/test/hackney_ws_tests.erl index d0de5a8f..bd1de67d 100644 --- a/test/hackney_ws_tests.erl +++ b/test/hackney_ws_tests.erl @@ -214,8 +214,20 @@ test_ping_pong(WsUrl) -> test_active_once_split_frame(WsUrl) -> {ok, Ws} = hackney:ws_connect(WsUrl, [{active, once}]), ok = hackney:ws_send(Ws, {text, <<"large:1048576">>}), + ok = hackney:ws_send(Ws, {text, <<"large:1048576">>}), + receive + {hackney_ws, Ws, {binary, First}} -> ?assertEqual(1048576, byte_size(First)) + after 3000 -> + ?assert(false) + end, + receive + {hackney_ws, Ws, Unexpected} -> ?assertEqual(no_message, Unexpected) + after 500 -> + ok + end, + ok = hackney:ws_setopts(Ws, [{active, once}]), receive - {hackney_ws, Ws, {binary, Bin}} -> ?assertEqual(1048576, byte_size(Bin)) + {hackney_ws, Ws, {binary, Second}} -> ?assertEqual(1048576, byte_size(Second)) after 3000 -> ?assert(false) end,