Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spit-appender lock for graalvm #334

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/taoensso/timbre/appenders/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@

;;;; Spit appender (clj only)

#?(:clj
(defn- write-to-file [{:keys [output_] :as data} fname append? self]
(let [output (force output_)]
(try
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String output)
(.newLine w))

(catch java.io.IOException e
(if (:spit-appender/retry? data)
(throw e) ; Unexpected error
(do
(jio/make-parents fname)
(self (assoc data :spit-appender/retry? true)))))))))

#?(:clj
(defn spit-appender
"Returns a simple `spit` file appender for Clojure."
Expand All @@ -82,29 +97,14 @@

(have? enc/nblank-str? fname)

{:enabled? true
:fn
(let [lock (Object.)]
(let [lock (Object.)]
{:enabled? true
:fn
(fn self [data]
(let [{:keys [output_]} data
output (force output_)
locklocal lock ; Ensure obj on stack for verifiers, Ref. CLJ-1472
]
(try
(when locking? (monitor-enter locklocal)) ; For thread safety, Ref. #251
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String output)
(.newLine w))

(catch java.io.IOException e
(if (:spit-appender/retry? data)
(throw e) ; Unexpected error
(do
(jio/make-parents fname)
(self (assoc data :spit-appender/retry? true)))))
(finally
(when locking?
(monitor-exit locklocal)))))))}))
(if locking?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericdallo If I understand @ptaoussanis correctly, the (force output) should happen outside of the lock, probably as the first step in the function body, outside the if. Correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(locking lock
(write-to-file data fname append? self))
(write-to-file data fname append? self)))})))

(comment
(spit-appender)
Expand Down