Skip to content

Commit e775db0

Browse files
committed
Close channels on IOException reading header
1 parent 6c50655 commit e775db0

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/lsp4clj/io_chan.clj

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
[clojure.java.io :as io]
88
[clojure.string :as string])
99
(:import
10-
(java.io EOFException InputStream OutputStream)))
10+
(java.io
11+
EOFException
12+
IOException
13+
InputStream
14+
OutputStream)))
1115

1216
(set! *warn-on-reflection* true)
1317

@@ -72,15 +76,18 @@
7276
(defn ^:private read-header-line
7377
"Reads a line of input. Blocks if there are no messages on the input."
7478
[^InputStream input]
75-
(let [s (java.lang.StringBuilder.)]
76-
(loop []
77-
(let [b (.read input)] ;; blocks, presumably waiting for next message
78-
(case b
79-
-1 ::eof ;; end of stream
80-
#_lf 10 (str s) ;; finished reading line
81-
#_cr 13 (recur) ;; ignore carriage returns
82-
(do (.append s (char b)) ;; byte == char because header is in US-ASCII
83-
(recur)))))))
79+
(try
80+
(let [s (java.lang.StringBuilder.)]
81+
(loop []
82+
(let [b (.read input)] ;; blocks, presumably waiting for next message
83+
(case b
84+
-1 ::eof ;; end of stream
85+
#_lf 10 (str s) ;; finished reading line
86+
#_cr 13 (recur) ;; ignore carriage returns
87+
(do (.append s (char b)) ;; byte == char because header is in US-ASCII
88+
(recur))))))
89+
(catch IOException _e
90+
::eof)))
8491

8592
(defn input-stream->input-chan
8693
"Returns a channel which will yield parsed messages that have been read off

0 commit comments

Comments
 (0)