Search code examples
clojure

No implementation of method: :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for class: clojure.core.async$chan


I'm learning clojure.async library.

Below is my code.

(ns channels.create
  (:require [clojure.core.async :refer [ chan >!! <!!]]))

(let [c chan]
  (future (>!! c 42))
  (future (println (<!! c))))

When I send the above namespace to repl I get below error -

#object[clojure.core$future_call$reify__8544 0x3cf627ec {:status :failed, :val #error{:cause "No implementation of method: :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for class: clojure.core.async$chan", :via [{:type java.util.concurrent.ExecutionException, :message "java.lang.IllegalArgumentException: No implementation of method: :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for class: clojure.core.async$chan", :at [java.util.concurrent.FutureTask report "FutureTask.java" 122]} {:type java.lang.IllegalArgumentException, :message "No implementation of method: :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for class: clojure.core.async$chan", :at [clojure.core$_cache_protocol_fn invokeStatic "core_deftype.clj" 584]}], :trace [[clojure.core$_cache_protocol_fn invokeStatic "core_deftype.clj" 584] [clojure.core$_cache_protocol_fn invoke "core_deftype.clj" 576] [clojure.core.async.impl.protocols$eval1957$fn__1958$G__1948__1965 invoke "protocols.clj" 15] [clojure.core.async$fn__2637 invokeStatic "async.clj" 135] [clojure.core.async$fn__2637 invoke "async.clj" 127] [channels.create$eval11241$fn__11244 invoke "create.clj" 3] [clojure.core$binding_conveyor_fn$fn__5823 invoke "core.clj" 2047] [clojure.lang.AFn call "AFn.java" 18] [java.util.concurrent.FutureTask run$$$capture "FutureTask.java" 264] [java.util.concurrent.FutureTask run "FutureTask.java" -1] [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1128] [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 628] [java.lang.Thread run "Thread.java" 829]]}}]

What is the error in my code? How can I fix this error?

Note: The error doesn't happens always but happens consitently 1 out of 4 times.Below is the screenshot.

channel put and take error


Solution

  • chan is a function. (chan) is a channel.

    Regarding your "inconsistency" edit: multithreaded code is racy. The REPL sometimes manages to look at the future before it's been evaluated enough to notice the error, and sometimes it doesn't.