Skip to content

Commit

Permalink
Fix taoensso#329 spit-appender deadlock
Browse files Browse the repository at this point in the history
In spit-appender, force the output before acquiring a lock in
case the forced code tries to acquire the same spit-appender lock
from a different thread.
  • Loading branch information
ryfow committed Nov 25, 2020
1 parent 1a8044e commit 35c6a39
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/taoensso/timbre/appenders/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@
(let [lock (Object.)]
(fn self [data]
(let [{:keys [output_]} data]
(try
(when locking? (monitor-enter lock)) ; For thread safety, Ref. #251
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String (force 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 lock)))))))}))
(let [forced (force output_)]
(try
(when locking? (monitor-enter lock)) ; For thread safety, Ref. #251
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String forced)
(.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 lock))))))))}))

(comment
(spit-appender)
Expand Down

0 comments on commit 35c6a39

Please sign in to comment.