Skip to content

Instantly share code, notes, and snippets.

@favila
Created July 20, 2017 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favila/e897b8070979659844c754fa664d95ab to your computer and use it in GitHub Desktop.
Save favila/e897b8070979659844c754fa664d95ab to your computer and use it in GitHub Desktop.
Minimum reproducible case where immutant connections seem to hang when using async/as-channel responses. This problem appears in immutant 2.1.7. The cause is not closing the InputStream from the request.
;;; immutant.web >= 2.1.7 will block subsequent requests on the same connection
;;; when using an async/as-channel response if the request body InputStream is
;;; not closed
;;; The essential difference seems to be the undertow dependency: 1.3.x
;;; does not care if the request is closed, but 1.4.x seems to care.
;;; Immutant 2.1.6 used undertow 1.3.x, but 2.1.7 switched to 1.4.x
;; leinigen Dependency: [[org.immutant/web "2.1.9"]]
(require '[immutant.web :as web]
'[immutant.web.undertow :as undertow]
'[immutant.web.async :as async])
(import '(org.projectodd.wunderboss.web.async Channel)
'(org.projectodd.wunderboss.web Attachments)
'(java.io InputStream Closeable))
(defn ring-handler [request]
; (class (:body request)) ;=> io.undertow.io.UndertowInputStream
(async/as-channel request
{:on-open (fn [^Channel ch]
(async/send! ch (.getBytes "hi " "ASCII") {:close? true})
;; Uncomment the following form to fix the problem
#_(let [body (:body (.get ^Attachments ch :originating-request))]
(if (instance? Closeable body)
(.close ^Closeable body))))}))
(def server (web/run #'ring-handler))
;; In another process, make two requests on the same connection:
; curl -v --data-binary "something" http://localhost:8080/ http://localhost:8080/
;; The --data-binary is necessary to ensure that the request has a body
;; The repeated URL is necessary to ensure the same connection is reused
;; Only one "hi " will be printed until the body of the first request is closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment