This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-20
Channels
- # announcements (16)
- # aws (1)
- # babashka (24)
- # beginners (54)
- # calva (25)
- # cider (4)
- # cljs-dev (2)
- # clojure (16)
- # clojure-dev (39)
- # clojure-europe (18)
- # clojure-nl (1)
- # clojure-uk (4)
- # clojurescript (66)
- # core-async (16)
- # cursive (13)
- # datomic (15)
- # deps-new (1)
- # duct (22)
- # fulcro (20)
- # jobs-discuss (1)
- # kaocha (3)
- # lsp (11)
- # malli (8)
- # off-topic (38)
- # other-languages (18)
- # polylith (18)
- # portal (15)
- # react (2)
- # reagent (9)
- # reitit (7)
- # releases (10)
- # reveal (1)
- # sci (13)
- # shadow-cljs (3)
- # specter (4)
- # sql (4)
- # xtdb (16)
Question for folks - I’m noticing some behavior w/ core.async that I’m a bit surprised by. In Java, when an exception occurs in some thread that isn’t handled explicitly in that thread’s Thread#run
method, the default thread exception handler is called on the same thread and is waited upon until the thread itself joins. However, with a core.async thread, while the Java default thread exception handler is called, it appears to be called separately from the thread that threw the exception, which means that in the event of an unhandled exception the channel returned by clojure.core.async/thread
closes before the exception handling is complete. Examples:
Preparation (registering default thread exception handler):
(Thread/setDefaultUncaughtExceptionHandler
(reify Thread$UncaughtExceptionHandler
(uncaughtException [_ thread ex] (Thread/sleep 1000) (println "CAUGHT EXCEPTION"))))
Java thread:
(do
(let [thread (Thread. (reify Runnable (run [this] (throw (Exception. "Test Exception")))))]
(.start thread)
(.join thread))
(println "THREAD COMPLETE" {}))
;; wait 1 second, then
> CAUGHT EXCEPTION
> THREAD COMPLETE {}
core.async thread:
(do
(a/<!! (a/thread (throw (Exception. "Test Exception"))))
(println "THREAD COMPLETE" {}))
> THREAD COMPLETE {}
;; wait 1 second, then
> CAUGHT EXCEPTION
This was surprising behavior to me, but now I wonder if there’s any method I can use to explicitly wait on the exception handler to complete before proceeding in execution?yeah - is there a means I can use to halt execution until the default exception handler completes?
you could call the default exception handler yourself, wrap everything in a try catch, get the default exception handler and invoke it
Yeah, there are workarounds I’m exploring now, just didn’t know if there was some established means of handling the issue that I wasn’t aware of.
it is kind of an interesting case, should core.async being doing something like that before the finally that closes the channel, dunno, you might throw something on http://ask.clojure.org
Bit of a beginner question. Are Atoms threadlocal? I have a bug where I edit an atom in a async context, but when I read it back I only find the value stored there after the first swap!
. However my unit tests find no such issue, but those work in a synchronous context.
firing off another thread to mutate an atom, then reading the atom, without making sure the task in the other thread has completed first
I'm reading them after getting a notification from the thread which edits them that it has been altered.