Skip to content

Commit 36b2859

Browse files
authored
Merge pull request #592 from arnaudgeiser/542
client: ensure response in case of Exception
2 parents a0c6406 + 481966c commit 36b2859

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/aleph/http/client.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,16 @@
554554
;; might be different in case we use :multipart
555555
(reset! save-body body))
556556

557-
(netty/safe-execute ch
558-
(http/send-message ch true ssl? req' body))))
557+
(-> (netty/safe-execute ch
558+
(http/send-message ch true ssl? req' body))
559+
(d/catch' (fn [e]
560+
(s/put! responses (d/error-deferred e))
561+
(netty/close ch))))))
559562

560563
;; this will usually happen because of a malformed request
561564
(catch Throwable e
562-
(s/put! responses (d/error-deferred e)))))
565+
(s/put! responses (d/error-deferred e))
566+
(netty/close ch))))
563567
requests)
564568

565569
(s/on-closed responses

src/aleph/http/core.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@
509509
(d/catch' (fn [_]))))]
510510

511511
(defn send-message
512+
"Write an HttpMessage and body to a Netty channel.
513+
514+
Accepts Strings, ByteBuffers, ByteBufs, byte[], ChunkedInputs,
515+
Files, Paths, HttpFiles, seqs and streams for bodies.
516+
517+
Seqs and streams must be, or be coercible to, a stream of ByteBufs."
512518
[ch keep-alive? ssl? ^HttpMessage msg body]
513519

514520
(let [f (cond
@@ -539,7 +545,8 @@
539545
(try
540546
(send-streaming-body ch msg body)
541547
(catch Throwable e
542-
(log/error e "error sending body of type " class-name)))))]
548+
(log/error e "error sending body of type " class-name)
549+
(throw e)))))]
543550

544551
(when-not keep-alive?
545552
(handle-cleanup ch f))

src/aleph/netty.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@
272272
x
273273
(.channel ^ChannelHandlerContext x)))
274274

275-
(defmacro safe-execute [ch & body]
275+
(defmacro safe-execute
276+
"Executes the body on the event-loop (an executor service) associated with the Netty channel.
277+
278+
Executes immediately if current thread is in the event loop. Otherwise, returns a deferred
279+
that will hold the result once done."
280+
[ch & body]
276281
`(let [f# (fn [] ~@body)
277282
event-loop# (-> ~ch aleph.netty/channel .eventLoop)]
278283
(if (.inEventLoop event-loop#)

test/aleph/http_test.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@
273273
{:body (io/file "test/file.txt")
274274
:pool client-pool}))))))))
275275

276+
(deftest test-invalid-body
277+
(let [client-url (str "http://localhost:" port)]
278+
(with-handler identity
279+
(is (thrown? IllegalArgumentException
280+
@(http-put client-url
281+
{:body 123}))))))
282+
276283
(def characters
277284
(let [charset (conj (mapv char (range 32 127)) \newline)]
278285
(repeatedly #(rand-nth charset))))

0 commit comments

Comments
 (0)