fix --mcp hang on a frame whose newline arrives as a separate write
sx-connector-mcp proc_send() writes the JSON body and the newline in two write() calls. read_request_line() treated the lone terminator as a blank header line and dropped it, so the frame never completed and every router call timed out ("initialize failed for filesystem"). The bare newline is now a frame terminator. Also switched stdin from fread() to read(): fread blocks until 64 KiB or EOF on a pipe, same class of stall. Adds a regression test that writes body and terminator apart (301 checks). Version 1.0.1.
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-13 23:50:13
Commitf6e6e788
Parents4f8f06f8
Changed files
release-dist.sh+2−2■■■■
src/plugin_filesystem.c+25−5■■■■■
tests/run.sh+22−3■■■■■
3 files+49−10
Diff
release-dist.sh+2−2■■■■
index c31d1d1..f23adb1 100755
--- a/release-dist.sh
+++ b/release-dist.sh
@@ -7,7 +7,7 @@
77 # every other oss plugin does, and so a stale release fails loudly instead of
88 # shipping a 3-target manifest that still says "release complete".
99 #
10-# ./release-dist.sh # build + publish version 1.0.0
10+# ./release-dist.sh # build + publish the default version
1111 # VERSION=1.0.1 ./release-dist.sh # explicit version
1212 #
1313 # Run it on the build host that has musl-gcc, aarch64-linux-gnu-gcc,
@@ -16,7 +16,7 @@
1616 set -eu
1717
1818 SLUG=filesystem
19-VERSION="${VERSION:-1.0.0}"
19+VERSION="${VERSION:-1.0.1}"
2020 PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"
2121
2222 ROOT=$(cd "$(dirname "$0")" && pwd)
src/plugin_filesystem.c+25−5■■■■■
index e003492..57e3465 100644
--- a/src/plugin_filesystem.c
+++ b/src/plugin_filesystem.c
@@ -87,7 +87,7 @@
8787
8888 #define PLUGIN_SLUG "filesystem"
8989 #define PLUGIN_ID "plugin_filesystem"
90-#define PLUGIN_VER "1.0.0"
90+#define PLUGIN_VER "1.0.1"
9191 #define PLUGIN_NAME "filesystem"
9292 #define PLUGIN_DESC "Allowlist FS"
9393 #define MCP_PROTO "2024-11-05"
@@ -2935,12 +2935,17 @@ static int read_request_line(buf_t *line)
29352935 want = strtoll(tmp + 15, NULL, 10);
29362936 continue;
29372937 }
2938- if (want < 0 && take == 0) continue; /* header blank */
2938+ if (want < 0 && take == 0 && !nl) continue; /* header blank */
29392939 buf_add(line, tmp, take);
29402940 if (want < 0) {
29412941 /* Newline-delimited: a chunk without '\n' is not a line,
29422942 * it is the front half of one. Only return when the
2943- * terminator was actually seen (or at EOF below). */
2943+ * terminator was actually seen (or at EOF below).
2944+ * take==0 && nl is the terminator that completes a frame
2945+ * written as two separate writes - the router's
2946+ * proc_send() writes the body and the '\n' apart, and an
2947+ * earlier cut of this function dropped that blank line
2948+ * into the header path, hanging every router call. */
29442949 if (nl) return 1;
29452950 continue;
29462951 }
@@ -2955,8 +2960,23 @@ static int read_request_line(buf_t *line)
29552960 }
29562961 }
29572962 if (rp >= rn) { rn = 0; rp = 0; }
2958- rn = fread(rb, 1, sizeof rb, stdin);
2959- if (rn == 0) return line->n ? 1 : 0;
2963+ /* NOT fread: on a pipe fread blocks until the whole 64 KiB buffer
2964+ * fills or EOF, so a client that sends "frame" and flushes, then
2965+ * sends "\n" later (the router's proc_send writes body and newline
2966+ * as separate writes), would hang. read() returns what is there. */
2967+ for (;;) {
2968+#if defined(_WIN32)
2969+ long got = (long)_read(_fileno(stdin), rb, (unsigned)sizeof rb);
2970+#else
2971+ long got = (long)read(fileno(stdin), rb, sizeof rb);
2972+#endif
2973+ if (got > 0) { rn = (size_t)got; break; }
2974+ if (got == 0) return line->n ? 1 : 0; /* EOF */
2975+#if !defined(_WIN32)
2976+ if (errno == EINTR) continue; /* signal, retry */
2977+#endif
2978+ return line->n ? 1 : 0; /* error = close */
2979+ }
29602980 }
29612981 }
29622982
tests/run.sh+22−3■■■■■
index 18b23fb..640109d 100755
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -189,7 +189,7 @@ check "--meta declares mcp true" '"mcp":true' "$META"
189189 check "--meta declares daemon false" '"daemon":false' "$META"
190190 check "--meta declares menus false (a connector must not flood the menus)" '"menus":false' "$META"
191191 check "--meta verified is live" '"verified":"live"' "$META"
192-check "--meta carries the version" '"version":"1.0.0"' "$META"
192+check "--meta carries the version" '"version":"1.0.1"' "$META"
193193 check_not "--meta name is not files (no collision with the read-only browser)" '"name":"files"' "$META"
194194 META2=$(SX_PLUGIN_META=1 ./plugin_filesystem)
195195 [ "$META" = "$META2" ] && pass "SX_PLUGIN_META=1 matches --meta" || fail "SX_PLUGIN_META=1 differs from --meta"
@@ -198,7 +198,7 @@ printf '%s' "$META" | grep -Eq '"name":"[^" ]+"' \
198198 DESC=$(printf '%s' "$META" | sed -n 's/.*"desc":"\([^"]*\)".*/\1/p')
199199 [ ${#DESC} -le 15 ] && pass "--meta desc is <=15 chars ($DESC)" \
200200 || fail "--meta desc is ${#DESC} chars (max 15)"
201-check "--version prints id + version" "plugin_filesystem 1.0.0" "$(./plugin_filesystem --version)"
201+check "--version prints id + version" "plugin_filesystem 1.0.1" "$(./plugin_filesystem --version)"
202202 check "--help lists the modes" "serve MCP JSON-RPC 2.0" "$(./plugin_filesystem --help)"
203203 check "--help names every tool" "list_allowed_directories" "$(./plugin_filesystem --help)"
204204
@@ -210,7 +210,7 @@ check "initialize echoes the requested protocol" '"protocolVersion":"2024-11-05"
210210 check "initialize names the server plugin_filesystem" '"name":"plugin_filesystem"' "$HDR"
211211 check "initialize advertises the tools capability" '"capabilities":{"tools"' "$HDR"
212212 check "initialize carries instructions" '"instructions"' "$HDR"
213-check "initialize reports the version" '"version":"1.0.0"' "$HDR"
213+check "initialize reports the version" '"version":"1.0.1"' "$HDR"
214214 HS=$(printf '%s\n' \
215215 '{"jsonrpc":"2.0","id":2,"method":"ping"}' \
216216 '{"jsonrpc":"2.0","id":3,"method":"resources/list"}' \
@@ -653,6 +653,25 @@ CL=$(printf 'Content-Length: %s\r\n\r\n%s' 116 \
653653 check "Content-Length framing is accepted" '"tools":[' "$CL"
654654 N=$(printf '{"jsonrpc":"2.0","method":"notifications/initialized"}\n' | ./plugin_filesystem --mcp)
655655 [ -z "$N" ] && pass "a notification gets no reply" || fail "a notification got a reply: $N"
656+# The router writes a frame and its terminator as two separate write()s. If
657+# the reader treats the bare "\n" that completes a frame as a skippable blank
658+# line, every call through sx-connector-mcp hangs until timeout. This check
659+# drives exactly that shape (the shell cannot - it cannot write half a line);
660+# python writes the body, waits, then writes only the newline.
661+SPLIT=$(python3 - ./plugin_filesystem <<'PYEOF' 2>&1
662+import json, os, select, subprocess, sys, time
663+frame = json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/list"}).encode()
664+p = subprocess.Popen([sys.argv[1], "--mcp"], stdin=subprocess.PIPE,
665+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
666+os.write(p.stdin.fileno(), frame) # body, no terminator
667+time.sleep(0.35)
668+os.write(p.stdin.fileno(), b"\n") # terminator as its own write
669+r, _, _ = select.select([p.stdout], [], [], 5)
670+sys.stdout.write(p.stdout.readline().decode() if r else "<TIMEOUT>")
671+p.kill()
672+PYEOF
673+)
674+check "a frame and its newline arriving as separate writes still answers" '"tools":[' "$SPLIT"
656675 PL=$(printf '{"jsonrpc":"2.0","id":1,"method":"tools/list"}\n' | ./plugin_filesystem --mcp)
657676 check "tools/list without initialize still answers" '"tools":[' "$PL"
658677 python3 -c 'import json,sys; sys.stdout.write(json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_file","arguments":{"path":sys.argv[1],"content":"A"*(11*1024*1024)}}})+"\n")' "$ROOT_A/big.txt" > "$WORK/big.json"
659678
release-dist.sh+2−2■■■■
index c31d1d1..f23adb1 100755index c31d1d1..f23adb1 100755
--- a/release-dist.sh--- a/release-dist.sh
+++ b/release-dist.sh+++ b/release-dist.sh
@@ -7,7 +7,7 @@@@ -7,7 +7,7 @@
7 # every other oss plugin does, and so a stale release fails loudly instead of7 # every other oss plugin does, and so a stale release fails loudly instead of
8 # shipping a 3-target manifest that still says "release complete".8 # shipping a 3-target manifest that still says "release complete".
9 #9 #
10-# ./release-dist.sh # build + publish version 1.0.010+# ./release-dist.sh # build + publish the default version
11 # VERSION=1.0.1 ./release-dist.sh # explicit version11 # VERSION=1.0.1 ./release-dist.sh # explicit version
12 #12 #
13 # Run it on the build host that has musl-gcc, aarch64-linux-gnu-gcc,13 # Run it on the build host that has musl-gcc, aarch64-linux-gnu-gcc,
@@ -16,7 +16,7 @@@@ -16,7 +16,7 @@
16 set -eu16 set -eu
17 17
18 SLUG=filesystem18 SLUG=filesystem
19-VERSION="${VERSION:-1.0.0}"19+VERSION="${VERSION:-1.0.1}"
20 PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"20 PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"
21 21
22 ROOT=$(cd "$(dirname "$0")" && pwd)22 ROOT=$(cd "$(dirname "$0")" && pwd)
src/plugin_filesystem.c+25−5■■■■■
index e003492..57e3465 100644index e003492..57e3465 100644
--- a/src/plugin_filesystem.c--- a/src/plugin_filesystem.c
+++ b/src/plugin_filesystem.c+++ b/src/plugin_filesystem.c
@@ -87,7 +87,7 @@@@ -87,7 +87,7 @@
87 87
88 #define PLUGIN_SLUG "filesystem"88 #define PLUGIN_SLUG "filesystem"
89 #define PLUGIN_ID "plugin_filesystem"89 #define PLUGIN_ID "plugin_filesystem"
90-#define PLUGIN_VER "1.0.0"90+#define PLUGIN_VER "1.0.1"
91 #define PLUGIN_NAME "filesystem"91 #define PLUGIN_NAME "filesystem"
92 #define PLUGIN_DESC "Allowlist FS"92 #define PLUGIN_DESC "Allowlist FS"
93 #define MCP_PROTO "2024-11-05"93 #define MCP_PROTO "2024-11-05"
@@ -2935,12 +2935,17 @@ static int read_request_line(buf_t *line)@@ -2935,12 +2935,17 @@ static int read_request_line(buf_t *line)
2935 want = strtoll(tmp + 15, NULL, 10);2935 want = strtoll(tmp + 15, NULL, 10);
2936 continue;2936 continue;
2937 }2937 }
2938- if (want < 0 && take == 0) continue; /* header blank */2938+ if (want < 0 && take == 0 && !nl) continue; /* header blank */
2939 buf_add(line, tmp, take);2939 buf_add(line, tmp, take);
2940 if (want < 0) {2940 if (want < 0) {
2941 /* Newline-delimited: a chunk without '\n' is not a line,2941 /* Newline-delimited: a chunk without '\n' is not a line,
2942 * it is the front half of one. Only return when the2942 * it is the front half of one. Only return when the
2943- * terminator was actually seen (or at EOF below). */2943+ * terminator was actually seen (or at EOF below).
2944+ * take==0 && nl is the terminator that completes a frame
2945+ * written as two separate writes - the router's
2946+ * proc_send() writes the body and the '\n' apart, and an
2947+ * earlier cut of this function dropped that blank line
2948+ * into the header path, hanging every router call. */
2944 if (nl) return 1;2949 if (nl) return 1;
2945 continue;2950 continue;
2946 }2951 }
@@ -2955,8 +2960,23 @@ static int read_request_line(buf_t *line)@@ -2955,8 +2960,23 @@ static int read_request_line(buf_t *line)
2955 }2960 }
2956 }2961 }
2957 if (rp >= rn) { rn = 0; rp = 0; }2962 if (rp >= rn) { rn = 0; rp = 0; }
2958- rn = fread(rb, 1, sizeof rb, stdin);2963+ /* NOT fread: on a pipe fread blocks until the whole 64 KiB buffer
2959- if (rn == 0) return line->n ? 1 : 0;2964+ * fills or EOF, so a client that sends "frame" and flushes, then
2965+ * sends "\n" later (the router's proc_send writes body and newline
2966+ * as separate writes), would hang. read() returns what is there. */
2967+ for (;;) {
2968+#if defined(_WIN32)
2969+ long got = (long)_read(_fileno(stdin), rb, (unsigned)sizeof rb);
2970+#else
2971+ long got = (long)read(fileno(stdin), rb, sizeof rb);
2972+#endif
2973+ if (got > 0) { rn = (size_t)got; break; }
2974+ if (got == 0) return line->n ? 1 : 0; /* EOF */
2975+#if !defined(_WIN32)
2976+ if (errno == EINTR) continue; /* signal, retry */
2977+#endif
2978+ return line->n ? 1 : 0; /* error = close */
2979+ }
2960 }2980 }
2961 }2981 }
2962 2982
tests/run.sh+22−3■■■■■
index 18b23fb..640109d 100755index 18b23fb..640109d 100755
--- a/tests/run.sh--- a/tests/run.sh
+++ b/tests/run.sh+++ b/tests/run.sh
@@ -189,7 +189,7 @@ check "--meta declares mcp true" '"mcp":true' "$META"@@ -189,7 +189,7 @@ check "--meta declares mcp true" '"mcp":true' "$META"
189 check "--meta declares daemon false" '"daemon":false' "$META"189 check "--meta declares daemon false" '"daemon":false' "$META"
190 check "--meta declares menus false (a connector must not flood the menus)" '"menus":false' "$META"190 check "--meta declares menus false (a connector must not flood the menus)" '"menus":false' "$META"
191 check "--meta verified is live" '"verified":"live"' "$META"191 check "--meta verified is live" '"verified":"live"' "$META"
192-check "--meta carries the version" '"version":"1.0.0"' "$META"192+check "--meta carries the version" '"version":"1.0.1"' "$META"
193 check_not "--meta name is not files (no collision with the read-only browser)" '"name":"files"' "$META"193 check_not "--meta name is not files (no collision with the read-only browser)" '"name":"files"' "$META"
194 META2=$(SX_PLUGIN_META=1 ./plugin_filesystem)194 META2=$(SX_PLUGIN_META=1 ./plugin_filesystem)
195 [ "$META" = "$META2" ] && pass "SX_PLUGIN_META=1 matches --meta" || fail "SX_PLUGIN_META=1 differs from --meta"195 [ "$META" = "$META2" ] && pass "SX_PLUGIN_META=1 matches --meta" || fail "SX_PLUGIN_META=1 differs from --meta"
@@ -198,7 +198,7 @@ printf '%s' "$META" | grep -Eq '"name":"[^" ]+"' \@@ -198,7 +198,7 @@ printf '%s' "$META" | grep -Eq '"name":"[^" ]+"' \
198 DESC=$(printf '%s' "$META" | sed -n 's/.*"desc":"\([^"]*\)".*/\1/p')198 DESC=$(printf '%s' "$META" | sed -n 's/.*"desc":"\([^"]*\)".*/\1/p')
199 [ ${#DESC} -le 15 ] && pass "--meta desc is <=15 chars ($DESC)" \199 [ ${#DESC} -le 15 ] && pass "--meta desc is <=15 chars ($DESC)" \
200 || fail "--meta desc is ${#DESC} chars (max 15)"200 || fail "--meta desc is ${#DESC} chars (max 15)"
201-check "--version prints id + version" "plugin_filesystem 1.0.0" "$(./plugin_filesystem --version)"201+check "--version prints id + version" "plugin_filesystem 1.0.1" "$(./plugin_filesystem --version)"
202 check "--help lists the modes" "serve MCP JSON-RPC 2.0" "$(./plugin_filesystem --help)"202 check "--help lists the modes" "serve MCP JSON-RPC 2.0" "$(./plugin_filesystem --help)"
203 check "--help names every tool" "list_allowed_directories" "$(./plugin_filesystem --help)"203 check "--help names every tool" "list_allowed_directories" "$(./plugin_filesystem --help)"
204 204
@@ -210,7 +210,7 @@ check "initialize echoes the requested protocol" '"protocolVersion":"2024-11-05"@@ -210,7 +210,7 @@ check "initialize echoes the requested protocol" '"protocolVersion":"2024-11-05"
210 check "initialize names the server plugin_filesystem" '"name":"plugin_filesystem"' "$HDR"210 check "initialize names the server plugin_filesystem" '"name":"plugin_filesystem"' "$HDR"
211 check "initialize advertises the tools capability" '"capabilities":{"tools"' "$HDR"211 check "initialize advertises the tools capability" '"capabilities":{"tools"' "$HDR"
212 check "initialize carries instructions" '"instructions"' "$HDR"212 check "initialize carries instructions" '"instructions"' "$HDR"
213-check "initialize reports the version" '"version":"1.0.0"' "$HDR"213+check "initialize reports the version" '"version":"1.0.1"' "$HDR"
214 HS=$(printf '%s\n' \214 HS=$(printf '%s\n' \
215 '{"jsonrpc":"2.0","id":2,"method":"ping"}' \215 '{"jsonrpc":"2.0","id":2,"method":"ping"}' \
216 '{"jsonrpc":"2.0","id":3,"method":"resources/list"}' \216 '{"jsonrpc":"2.0","id":3,"method":"resources/list"}' \
@@ -653,6 +653,25 @@ CL=$(printf 'Content-Length: %s\r\n\r\n%s' 116 \@@ -653,6 +653,25 @@ CL=$(printf 'Content-Length: %s\r\n\r\n%s' 116 \
653 check "Content-Length framing is accepted" '"tools":[' "$CL"653 check "Content-Length framing is accepted" '"tools":[' "$CL"
654 N=$(printf '{"jsonrpc":"2.0","method":"notifications/initialized"}\n' | ./plugin_filesystem --mcp)654 N=$(printf '{"jsonrpc":"2.0","method":"notifications/initialized"}\n' | ./plugin_filesystem --mcp)
655 [ -z "$N" ] && pass "a notification gets no reply" || fail "a notification got a reply: $N"655 [ -z "$N" ] && pass "a notification gets no reply" || fail "a notification got a reply: $N"
656+# The router writes a frame and its terminator as two separate write()s. If
657+# the reader treats the bare "\n" that completes a frame as a skippable blank
658+# line, every call through sx-connector-mcp hangs until timeout. This check
659+# drives exactly that shape (the shell cannot - it cannot write half a line);
660+# python writes the body, waits, then writes only the newline.
661+SPLIT=$(python3 - ./plugin_filesystem <<'PYEOF' 2>&1
662+import json, os, select, subprocess, sys, time
663+frame = json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/list"}).encode()
664+p = subprocess.Popen([sys.argv[1], "--mcp"], stdin=subprocess.PIPE,
665+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
666+os.write(p.stdin.fileno(), frame) # body, no terminator
667+time.sleep(0.35)
668+os.write(p.stdin.fileno(), b"\n") # terminator as its own write
669+r, _, _ = select.select([p.stdout], [], [], 5)
670+sys.stdout.write(p.stdout.readline().decode() if r else "<TIMEOUT>")
671+p.kill()
672+PYEOF
673+)
674+check "a frame and its newline arriving as separate writes still answers" '"tools":[' "$SPLIT"
656 PL=$(printf '{"jsonrpc":"2.0","id":1,"method":"tools/list"}\n' | ./plugin_filesystem --mcp)675 PL=$(printf '{"jsonrpc":"2.0","id":1,"method":"tools/list"}\n' | ./plugin_filesystem --mcp)
657 check "tools/list without initialize still answers" '"tools":[' "$PL"676 check "tools/list without initialize still answers" '"tools":[' "$PL"
658 python3 -c 'import json,sys; sys.stdout.write(json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_file","arguments":{"path":sys.argv[1],"content":"A"*(11*1024*1024)}}})+"\n")' "$ROOT_A/big.txt" > "$WORK/big.json"677 python3 -c 'import json,sys; sys.stdout.write(json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_file","arguments":{"path":sys.argv[1],"content":"A"*(11*1024*1024)}}})+"\n")' "$ROOT_A/big.txt" > "$WORK/big.json"
659678