nREPL server never answers a request with non-ASCII text that arrives in more than one chunk

Open Beginner friendly
#443 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start in src/nbb/impl/nrepl_server.cljs at on-connect, where pending is joined with incoming TCP data, then read nbb.impl.bencode/decode to understand how request lengths are parsed. Keep the pending data byte-based across chunks as the issue describes. Confirm completion by sending the two-write reproduction with non-ASCII text and checking that it returns the expected value and the socket can handle later requests.

Written by the indexing model from the issue text.

Description

version

nbb 1.3.204. The same code is on main at v1.6.215.

platform

macOS 26.7.3, Node.js v26.14.0

problem

nbb's nREPL server never answers a request that holds a multi-byte character when the request arrives in two or more TCP chunks. After that, it answers nothing else on that socket either. A new connection works.

on-connect in src/nbb/impl/nrepl_server.cljs keeps the unparsed tail in pending and joins it with the next chunk via (str p data). That turns the bytes into a JS string. nbb.impl.bencode/decode then slices each netstring by UTF-16 units, while the length prefix counts bytes. The request never parses and stays in pending, and every later request on the socket queues behind it.

A client that writes bencode token by token hits this on every request with non-ASCII text. One example is bencode.core's write-bencode on a socket output stream. Requests over 64 KB arrive in several chunks whatever the client does.

repro

Start nbb nrepl-server :port 1667, then run this with bb:

(require '[bencode.core :as b])
(import '[java.net Socket] '[java.io ByteArrayOutputStream PushbackInputStream])

(defn eval-in-two-writes [code]
  (let [bo (ByteArrayOutputStream.)
        _ (b/write-bencode bo {"op" "eval" "code" code "id" "1"})
        bs (.toByteArray bo)]
    (with-open [s (doto (Socket. "localhost" 1667) (.setSoTimeout 3000))]
      (let [out (.getOutputStream s)]
        (.write out bs 0 1)
        (.flush out)
        (Thread/sleep 100)
        (.write out bs 1 (dec (alength bs)))
        (.flush out)
        (try (String. ^bytes (get (b/read-bencode (PushbackInputStream. (.getInputStream s))) "value") "UTF-8")
             (catch java.net.SocketTimeoutException _ "no answer in 3 s"))))))

(println "(str \"abc\") ->" (eval-in-two-writes "(str \"abc\")"))
(println "(str \"a·b\") ->" (eval-in-two-writes "(str \"a·b\")"))

Output:

(str "abc") -> "abc"
(str "a·b") -> no answer in 3 s

expected behavior

Both requests get their value: (str "a·b") -> "a·b".

Keeping pending as a Buffer and joining with (js/Buffer.concat #js [p data]) should fix it. decode already handles Buffers: the same request sent in one write gets its answer.

Dominant language
Clojure
Stars
972
Forks
56
Avg merge
1m
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from babashka/nbb

All issues in babashka/nbb

Similar issues

More Clojure issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.