aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2026-08-29 19:59:33 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2026-08-30 08:46:47 +0200
commite21720da69f2a505bcd1405cc7e8b52d5649df55 (patch)
treeb37bd8a78ba484dd9987b306a50d391315f639ab
parent827ae20771d34912a8f79b50dc68e6430945b9eb (diff)
downloadluasocket-e21720da69f2a505bcd1405cc7e8b52d5649df55.tar.gz
luasocket-e21720da69f2a505bcd1405cc7e8b52d5649df55.tar.bz2
luasocket-e21720da69f2a505bcd1405cc7e8b52d5649df55.zip
fix(receive): a receive 0 should immediately return, not block
The fix is slightly more complex because reading from a closed socket should still return a closed-error. fixes: #427 fixes: https://github.com/ledgetech/lua-resty-http/pull/313
-rw-r--r--.github/workflows/build.yml1
-rw-r--r--src/buffer.c23
-rw-r--r--src/usocket.c2
-rw-r--r--src/wsocket.c1
-rw-r--r--test/test_receive_zero.lua36
5 files changed, 53 insertions, 10 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 229fcbf..d516568 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -49,4 +49,5 @@ jobs:
49 lua mimetest.lua 49 lua mimetest.lua
50 lua urltest.lua 50 lua urltest.lua
51 lua test_socket_error.lua 51 lua test_socket_error.lua
52 lua test_receive_zero.lua
52 kill %1 53 kill %1
diff --git a/src/buffer.c b/src/buffer.c
index 3d48a09..b7e97f7 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -11,7 +11,7 @@
11static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b); 11static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b);
12static int recvline(p_buffer buf, luaL_Buffer *b, size_t budget); 12static int recvline(p_buffer buf, luaL_Buffer *b, size_t budget);
13static int recvall(p_buffer buf, luaL_Buffer *b, size_t budget); 13static int recvall(p_buffer buf, luaL_Buffer *b, size_t budget);
14static int buffer_get(p_buffer buf, const char **data, size_t *count); 14static int buffer_get(p_buffer buf, const char **data, size_t *count, size_t wanted);
15static void buffer_skip(p_buffer buf, size_t count); 15static void buffer_skip(p_buffer buf, size_t count);
16static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent); 16static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
17 17
@@ -230,15 +230,14 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
230static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { 230static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) {
231 int err = IO_DONE; 231 int err = IO_DONE;
232 size_t total = 0; 232 size_t total = 0;
233 while (err == IO_DONE) { 233 do {
234 size_t count; const char *data; 234 size_t count; const char *data;
235 err = buffer_get(buf, &data, &count); 235 err = buffer_get(buf, &data, &count, wanted - total);
236 count = MIN(count, wanted - total); 236 count = MIN(count, wanted - total);
237 luaL_addlstring(b, data, count); 237 luaL_addlstring(b, data, count);
238 buffer_skip(buf, count); 238 buffer_skip(buf, count);
239 total += count; 239 total += count;
240 if (total >= wanted) break; 240 } while (total < wanted && err == IO_DONE);
241 }
242 return err; 241 return err;
243} 242}
244 243
@@ -253,7 +252,7 @@ static int recvall(p_buffer buf, luaL_Buffer *b, size_t budget) {
253 size_t total = 0; 252 size_t total = 0;
254 while (err == IO_DONE) { 253 while (err == IO_DONE) {
255 const char *data; size_t count; 254 const char *data; size_t count;
256 err = buffer_get(buf, &data, &count); 255 err = buffer_get(buf, &data, &count, BUF_SIZE);
257 if (budget && count > budget - total) { /* strictly more than fits */ 256 if (budget && count > budget - total) { /* strictly more than fits */
258 count = budget - total; 257 count = budget - total;
259 luaL_addlstring(b, data, count); 258 luaL_addlstring(b, data, count);
@@ -286,7 +285,7 @@ static int recvline(p_buffer buf, luaL_Buffer *b, size_t budget) {
286 size_t total = 0; 285 size_t total = 0;
287 while (err == IO_DONE) { 286 while (err == IO_DONE) {
288 size_t count, pos; const char *data; 287 size_t count, pos; const char *data;
289 err = buffer_get(buf, &data, &count); 288 err = buffer_get(buf, &data, &count, BUF_SIZE);
290 pos = 0; 289 pos = 0;
291 while (pos < count && data[pos] != '\n') { 290 while (pos < count && data[pos] != '\n') {
292 /* we ignore all \r's -- they are consumed but never counted */ 291 /* we ignore all \r's -- they are consumed but never counted */
@@ -325,15 +324,19 @@ static void buffer_skip(p_buffer buf, size_t count) {
325 324
326/*-------------------------------------------------------------------------*\ 325/*-------------------------------------------------------------------------*\
327* Return any data available in buffer, or get more data from transport layer 326* Return any data available in buffer, or get more data from transport layer
328* if buffer is empty 327* if buffer is empty. 'wanted' is how many more bytes the caller is still
328* after; when it is zero, the transport layer is still consulted (so an
329* already-closed connection is still reported), but no more than zero bytes
330* are requested from it, so a healthy connection with no data pending can
331* never block.
329\*-------------------------------------------------------------------------*/ 332\*-------------------------------------------------------------------------*/
330static int buffer_get(p_buffer buf, const char **data, size_t *count) { 333static int buffer_get(p_buffer buf, const char **data, size_t *count, size_t wanted) {
331 int err = IO_DONE; 334 int err = IO_DONE;
332 p_io io = buf->io; 335 p_io io = buf->io;
333 p_timeout tm = buf->tm; 336 p_timeout tm = buf->tm;
334 if (buffer_isempty(buf)) { 337 if (buffer_isempty(buf)) {
335 size_t got; 338 size_t got;
336 err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); 339 err = io->recv(io->ctx, buf->data, wanted == 0 ? 0 : BUF_SIZE, &got, tm);
337 buf->first = 0; 340 buf->first = 0;
338 buf->last = got; 341 buf->last = got;
339 } 342 }
diff --git a/src/usocket.c b/src/usocket.c
index 7965db6..e00c43d 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -258,6 +258,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
258 int err; 258 int err;
259 *got = 0; 259 *got = 0;
260 if (*ps == SOCKET_INVALID) return IO_CLOSED; 260 if (*ps == SOCKET_INVALID) return IO_CLOSED;
261 if (count == 0) return IO_DONE;
261 for ( ;; ) { 262 for ( ;; ) {
262 long taken = (long) recv(*ps, data, count, 0); 263 long taken = (long) recv(*ps, data, count, 0);
263 if (taken > 0) { 264 if (taken > 0) {
@@ -343,6 +344,7 @@ int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
343 int err; 344 int err;
344 *got = 0; 345 *got = 0;
345 if (*ps == SOCKET_INVALID) return IO_CLOSED; 346 if (*ps == SOCKET_INVALID) return IO_CLOSED;
347 if (count == 0) return IO_DONE;
346 for ( ;; ) { 348 for ( ;; ) {
347 long taken = (long) read(*ps, data, count); 349 long taken = (long) read(*ps, data, count);
348 if (taken > 0) { 350 if (taken > 0) {
diff --git a/src/wsocket.c b/src/wsocket.c
index d3af9d4..86c6994 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -247,6 +247,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
247 int err, prev = IO_DONE; 247 int err, prev = IO_DONE;
248 *got = 0; 248 *got = 0;
249 if (*ps == SOCKET_INVALID) return IO_CLOSED; 249 if (*ps == SOCKET_INVALID) return IO_CLOSED;
250 if (count == 0) return IO_DONE;
250 for ( ;; ) { 251 for ( ;; ) {
251 int taken = recv(*ps, data, (int) count, 0); 252 int taken = recv(*ps, data, (int) count, 0);
252 if (taken > 0) { 253 if (taken > 0) {
diff --git a/test/test_receive_zero.lua b/test/test_receive_zero.lua
new file mode 100644
index 0000000..2259910
--- /dev/null
+++ b/test/test_receive_zero.lua
@@ -0,0 +1,36 @@
1-- a TCP receive(0) must never block: requesting zero bytes is trivially
2-- satisfied without touching the transport layer, POSIX recv(fd, buf, 0, 0)
3-- returns immediately regardless of whether data is available.
4local socket = require "socket"
5
6local host, port = "127.0.0.1", "5464"
7
8local server = assert(socket.bind(host, port))
9local client = assert(socket.connect(host, port))
10local peer = assert(server:accept())
11
12client:settimeout(2)
13
14-- no data has been sent by the peer: the read buffer is empty, so if
15-- receive(0) touches the network it will block until the timeout expires
16local t0 = socket.gettime()
17local data, err = client:receive(0)
18local elapsed = socket.gettime() - t0
19
20assert(data == "", "receive(0) on empty buffer returned " .. tostring(data))
21assert(err == nil, "receive(0) on empty buffer returned error " .. tostring(err))
22assert(elapsed < 1, "receive(0) on empty buffer blocked for " .. elapsed .. "s")
23
24-- receive(0) must also not consume any bytes when data *is* available
25assert(peer:send("hello"))
26data, err = client:receive(0)
27assert(data == "", "receive(0) with data pending returned " .. tostring(data))
28assert(err == nil, "receive(0) with data pending returned error " .. tostring(err))
29data = assert(client:receive(5))
30assert(data == "hello", "receive(0) consumed bytes meant for receive(5)")
31
32client:close()
33peer:close()
34server:close()
35
36print("done!")