Skip to content

Q: upmap - need to wait until all the tasks are done when an exception happens in one of the computations #72

@jumarko

Description

@jumarko

I'm using the upmap function like this

(vec (cp/upmap 5 clone-git-repo-fn repos))

vec is used explicitly to make sure everything is cloned before proceeding with other logic.

But sometimes this fails with an exception thrown by clone-git-repo-fn for one of the repos (e.g. because of incorrect repository information/access).

In that case, I do want to abort the process but also delete all the other repos that may have been cloned on the disk.
The trouble is that cloning of some of the other repositories may still be in progress - there's no guarantee that all the "git clone" processes have finished when the exception is thrown.

Therefore, I would need to wait until all the processes/threads started by upmap finished before proceeding with the cleanup.
I'm not sure how to do that.

Consider this contrived example:

 ;; 1. fire the tasks
  (def results
    (cp/upmap 5
              (fn [s] (Thread/sleep s)
                (when (or (= s 3000)
                          (= s 6000)) (throw (ex-info "error" {})))
                (println s) s)
              (shuffle [1000 2000 3000 4000 5000 6000 5000 4000 5000 7000])))

;; 2. evaluate this immediately after the expression above
  (try
    (vec results)
    (catch Exception e
      (println "Got an exception" e)
      (println "Waiting till all of the results are finished")
      (vec results)
      (println "FINISHED ?!?")
      ))

It prints

1000
Got an exception #error {
 :cause error
 :data {}
 :via
 [{:type clojure.lang.ExceptionInfo
...
Waiting till all of the results are finished
FINISHED ?!?
4000
5000
2000
7000
5000
5000
4000

Funny enough, if I try to evaluate the second expression again, I get only this without any exception:

[1000]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions