socketry / async

An awesome asynchronous event-driven reactor for Ruby.
MIT License
2.09k stars 86 forks source link

Exception propagation in async tasks #185

Closed yard closed 1 year ago

yard commented 1 year ago

Hey guys,

Found a slightly unsettling piece of code within the library located at https://github.com/socketry/async/blob/main/lib/async/task.rb#L248

Could you please elaborate on the reason why StandardErrors are not propagated up to the parent, whereas Exceptions do?

While seemingly insignificant at first glance, it makes certain things less convenient. Consider the following code:

Async do
  Async { sleep 1 }
  Async { raise StandardError }
end

Once this is written like that, there is no way to understand that an inner task failed without explicitly tracking its status (e.g. storing it into a variable and querying it later), which appears to be a contradiction with the comment of fail! function suggesting "If the user writes code which raises an exception, that exception should always be visible".

Many thanks in advance!

ioquatix commented 1 year ago

Firstly, can you run the code you proposed and add the output to this issue.

ioquatix commented 1 year ago

I was hoping you'd come back, but in any case, if you ran the code, you'd see it would log the errors.

Regarding your specific question:

Could you please elaborate on the reason why StandardErrors are not propagated up to the parent, whereas Exceptions do?

Because StandardErrors are errors you can do something about and are propagated out of the task (promise) when calling wait. But Exceptions (like memory allocation failure) is generally not something you can do anything about, and thus it will kill the reactor.