Fork me on GitHub
#missionary
<
2022-09-08
>
Laverne Schrock21:09:55

I've run into an odd issue that I can't explain:

(ns test-ns)
(require '[missionary.core :as m])

(defn h [x] (* x x))
(h 3) ;; 9 ;; obviously, a normal function

(defn g [x] (m/sp (h x)))
(g 3) ;; #function[clojure.core/partial/fn--5857]  ;; seems right, since we're returning a task

(defn f [x] (m/? (g x)))
(f 3) ;; 9 -- seems right, since we're fetching a task and waiting for its completion


(defn h' [x] (m/? (m/sp (* x x))))
(h' 3) ;; 9 -- good, appears to be a normal function

(defn g' [x] (m/sp (h' x)))
(g' 3) ;; #function[clojure.core/partial/fn--5857] ;; seems right, since that's what g returned

(defn f' [x] (m/? (g' x)))
(f' 3) ;; Throws
;;  Unhandled java.lang.NullPointerException
;;    Cannot invoke "clojure.lang.IFn.invoke(Object)" because the return
;;    value of "clojure.lang.RT.aget(Object[], int)" is null
;;
;;                  impl.cljc:   60  cloroutine.impl$coroutine$fn__59703/invoke
;;            Sequential.java:   50  missionary.impl.Sequential/step
;;            Sequential.java:   62  missionary.impl.Sequential/<init>
;;                   impl.clj:   31  missionary.impl/sp
;;                   impl.clj:   31  missionary.impl/sp
;;                  impl.cljc:   65  cloroutine.impl$coroutine$fn__59703/invoke
;;                   core.clj: 2629  clojure.core/partial/fn
;;                 Fiber.java:   24  missionary.impl.Fiber$1$1$1/<init>
;;                 Fiber.java:   20  missionary.impl.Fiber$1$1/task
;;                   impl.clj:   38  missionary.impl/fiber-task
;;                   impl.clj:   38  missionary.impl/fiber-task
;;                  core.cljc:  178  missionary.core$_QMARK_/invokeStatic
;;                  core.cljc:  170  missionary.core$_QMARK_/invoke
;;                       REPL:   65  test-ns/f'
;;                       REPL:   65  test-ns/f'
;;                       REPL:   78  test-ns/eval104116
;;                       REPL:   78  test-ns/eval104116
;;              Compiler.java: 7181  clojure.lang.Compiler/eval
;;              Compiler.java: 7136  clojure.lang.Compiler/eval
;;                   core.clj: 3202  clojure.core/eval
;;                   core.clj: 3198  clojure.core/eval
;;     interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
;;                   ... cider details from here on up ...
It's not clear to me how the task returned by g' now fails to complete, since like the task returned by g, it's just calling a function that returns a Long.

leonoel08:09:33

That looks like https://github.com/leonoel/missionary/issues/35 It is not supported and the error message must be improved.

Laverne Schrock12:09:40

Thanks, that does seem to be the same issue.

Laverne Schrock12:09:34

I am curious though, why you say that get-value is a blocking function

(defn get-value []
  (m/? (m/sp :internal-use-of-missionary)))
I would expect (m/? (m/sp :internal-use-of-missionary)) to be equivalent to :internal-use-of-missionary , at least on the JVM where m/? can execute the generated task on the given thread

leonoel16:09:08

m/? outside sp blocks the thread until the task terminates, however the task implementation is free to use any other thread to perform the job

Laverne Schrock16:09:37

ah, so the question of whether m/? is blocking depends on the task (not just the contents of the task)

leonoel16:09:48

no, it is always blocking unless the task is instantaneous

(do (println 1 (.getName (Thread/currentThread)))
    (m/? (m/sp
           (println 2 (.getName (Thread/currentThread)))
           (m/? (m/sleep 1))
           (println 3 (.getName (Thread/currentThread)))
           (m/? (m/via m/cpu))
           (println 4 (.getName (Thread/currentThread)))
           (m/? (m/via m/blk))
           (println 5 (.getName (Thread/currentThread)))))
    (println 6 (.getName (Thread/currentThread))))
1 nREPL-session-a7e6ab84-8e10-4452-a036-2990815a8a62 2 nREPL-session-a7e6ab84-8e10-4452-a036-2990815a8a62 3 missionary scheduler 4 missionary cpu-3 5 missionary blk-1 6 nREPL-session-a7e6ab84-8e10-4452-a036-2990815a8a62

1
Laverne Schrock16:09:43

wow, that's some interesting output. 1,2 and 6 make sense, and I guess 3 makes sense, but it's very surprising to see parts of the body being run on threads in the cpu and blk pools despite those parts of the body not being in a (m/via m/blk ...)