puniverse / pulsar

Fibers, Channels and Actors for Clojure
http://docs.paralleluniverse.co/pulsar/
Other
910 stars 53 forks source link

(spawn-fiber #(rcv channel)) failing in the REPL #61

Open andreasthoelke opened 8 years ago

andreasthoelke commented 8 years ago

Entering the following in the REPL should return "got: 123" immediately:

; Example 1:
; With Pulsar 0.7.5
(def ch1 (channel))

(def fi1 (->> (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch1)
              (spawn-fiber (fn two [v] (str "got: " @v)))))

(snd ch1 123)
@fi1
;;=> "got: "

But when using Pulsar 0.7.5 @fi1 blocks until the 'rcv' times out. Though (snd ch1 123) does accept the value immediately/does not block, the rcv ch seems to never receive the value.

When I macroexpand one level and run:

; Example 2:
(def ch1 (channel))

(def fi1 (spawn-fiber (fn two [v] (str "got: " @v)) 
                      (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch1)))

(snd ch1 123)
@fi1
;;=> "got: 123"

I get the expected outcome: "got: 123". However if I enter the same four expressions again @fi1 blocks and I get "got: ".

If I then rename the channel the process works again:

(def ch2 (channel))

(def fi1 (spawn-fiber (fn two [v] (str "got: " @v)) 
                      (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch2)))

(snd ch2 123)
@fi1
;;=> "got: 123"

But I can only run this once per channel name. (This seems related to #60)

Running the following code always succeeds (in Pulsar 0.7.5 and 0.7.6) - I can enter it repeatedly in the REPL and it shows the same expected outcome:

; With Pulsar 0.7.5 or 0.7.6
(def ch1 (channel))

(def fi3 (let [fi1 (spawn-fiber #(rcv % 10000 :ms) ch1)
               fi2 (spawn-fiber #(str "got: " @%) fi1)]
           fi2))

(snd ch1 123)
@fi3
;;=> "got: 123"

With Pulsar 0.7.6 this succeeds in the first run:

; Example 1 (first run):
; With Pulsar 0.7.6
(def ch1 (channel))

(def fi1 (->> (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch1)
              (spawn-fiber (fn two [v] (str "got: " @v)))))

(snd ch1 123)
@fi1
;;=> "got: 123"

Also the second example succeeds as it did with Pulsar 0.7.5:

; Example 2 (first run):
; With Pulsar 0.7.6
(def ch1 (channel))

(def fi1 (spawn-fiber (fn two [v] (str "got: " @v)) 
                      (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch1)))

(snd ch1 123)
@fi1
;;=> "got: 123"

But entering example 1 or example 2 a second time will fail as it did with 0.7.5. Curiously, renaming the channel does not help with 0.7.6 as it did with 0.7.5:

; Example 2 (nth run):
; With Pulsar 0.7.6
(def ch2 (channel))

(def fi1 (spawn-fiber (fn two [v] (str "got: " @v)) 
                      (spawn-fiber (fn one [ch] (rcv ch 10000 :ms)) ch2)))

(snd ch2 123)
@fi1
;;=> "got: "

(Note: I'm currently preferring 0.7.5 as 0.7.6 seems to increase REPL startup time significantly in mid-sized projects. I could document this in a separate issue if it helps)