luci-app-nlbw: fix sporadic premature EOF when rendering JSON data
authorJo-Philipp Wich <jo@mein.io>
Wed, 17 Jan 2018 20:15:47 +0000 (21:15 +0100)
committerJo-Philipp Wich <jo@mein.io>
Wed, 17 Jan 2018 20:15:47 +0000 (21:15 +0100)
Specific timing patterns sometimes caused the LuCI controller to prematurely
stop reading data, resulting in truncated JSON output.

Turn the nonblocking waitpid() call into a blocking call after the IO read
loop to avoid this issue.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
applications/luci-app-nlbwmon/luasrc/controller/nlbw.lua

index bb56bc6..a8c5779 100644 (file)
@@ -28,16 +28,17 @@ local function exec(cmd, args, writer)
 
                while true do
                        local buffer = fdi:read(2048)
-                       local wpid, stat, code = nixio.waitpid(pid, "nohang")
 
-                       if writer and buffer and #buffer > 0 then
-                               writer(buffer)
+                       if not buffer or #buffer == 0 then
+                               break
                        end
 
-                       if wpid and stat == "exited" then
-                               break
+                       if writer then
+                               writer(buffer)
                        end
                end
+
+               nixio.waitpid(pid)
        elseif pid == 0 then
                nixio.dup(fdo, nixio.stdout)
                fdi:close()