missionary

zy C 2024-04-28T16:55:02.110269Z

hi! I have a question about m/race: My understanding of the behavior of race is: the second task should execute successfully, returning 2, and the first sleep task should be cancelled, so the only printed content should be :s 2, and :clean should not appear. but it looks like the second task is cancelled by m/race

(def cancel2 ((m/race (m/sleep 1000 1)
                        (fn [s f]
                          (s 2)
                          #(prn :clean)))
                #(prn :s %) #(js/console.log :f %)))
  ;; print:
  ;; :s 2
  ;; :clean

Eric Dvorsak 2024-04-30T11:05:20.658149Z

@leonoel by writing tasks manually you mean writing tasks following the task spec without using missionary api (eg m/sp)?

leonoel 2024-04-30T11:21:11.143369Z

yes

leonoel 2024-04-28T17:03:23.448019Z

Observing :clean is an implementation detail. The cancelling function is supposed to become a no-op after completion, therefore m/race considers it safe to call it on the race winner.

zy C 2024-04-28T17:06:36.888079Z

so even if the second task wins in the race, its canceller will still be called? did i understand it correctly?

leonoel 2024-04-28T17:07:49.773219Z

yes, but it's accidental and you should not rely on that

zy C 2024-04-28T17:08:51.468969Z

According to the docstring “If any task succeeds, others are cancelled then race completes with this result.” I originally thought that the canceller of the winning task would definitely not be called.

leonoel 2024-04-28T17:11:58.301789Z

it may or may not be called, no guarantees

❤️ 1
zy C 2024-04-28T17:13:13.318709Z

okay, understand now. thank you for the quick reply!

leonoel 2024-04-28T17:23:20.691749Z

The reason is because cancellation and completion may be concurrent, when it happens the task process must be able to resolve the race condition and handle the "completion then cancellation" scenario. It is therefore always safe for the parent process to cancel after completion, so it's allowed to do that if it's beneficial for the implementation.

👍 2
❤️ 1
zy C 2024-04-28T17:35:56.585489Z

Thank you so much for the detailed explanation! It might be best to add this explanation to the docstring or somewhere else? Otherwise, beginners like me might misunderstand the meaning of the m/race docstring.

leonoel 2024-04-28T17:43:01.658089Z

This is expert-only material, as an application developer you're not supposed to write tasks manually

zy C 2024-04-28T17:45:30.540819Z

haha, I am now half an expert

zy C 2024-04-28T17:45:35.965649Z

thanks again