question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Parent catching forked tasks errors?

See original GitHub issue

I’d like to do something like this:

function* saga() {
  const forkedTasks = [...];

  try {
    // Wait for all forked tasks (is there a more idiomatic way? maybe `join` ?)
    yield forkedTasks.map(task => task.done);
  }
  catch (e) {
    yield put(displayError());
    throw e;
  }
}

But it does not work. The parent seems to be cancelled and never get the opportunity to catch the error.

Errors from child tasks automatically bubble up to their parents. If any forked task raises an uncaught error, then the parent task will abort with the child Error, and the whole Parent’s execution tree (i.e. forked tasks + the main task represented by the parent’s body if it’s still running) will be cancelled.

So, when using forked tasks, is there a way for the parent to catch a child error? And eventually decide weither or not to kill the whole tasks tree?

The following seems to work, but it feels a bit unnatural and error prone to me. It’s quite easy when using spawn to get tricked and let tasks alive while they should dye.

function* saga() {
  const spawnedTasks = [...];

  try {
    yield spawnedTasks.map(task => task.done);
  }
  catch (e) {
    yield put(displayError());
    throw e;
  }
  finally {
    yield spawnedTasks.map(task => cancel(task));
  }
}

Any idea on how this “error interception” pattern (I often want to do that) can be more idiomatically implemented?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
yelouaficommented, Sep 15, 2016

You can wrap your forks with the desired Error handler

// Wrap forks with an Error handler
function* wrap(fn, args) {
  try {
    yield call(fn, ...args)
  }
  catch (e) {
    yield put(displayError());
    throw e;
  }
}

// a custom Effect creator for it
const forkWithErrHandler = (fn, ...args) => fork(wrap, fn, args)

function* saga() {
  //const forkedTasks = [...];

  // To fork a new task instead of `yield fork(fn, ...args)` use the wrapper
  yield forkWithErrHandler(fn, ...args)

  // yield forkedTasks.map(join);
  // You don't have to wait for forked tasks, saga will terminate when all forked tasks terminate

}
0reactions
yelouaficommented, Nov 30, 2016

closing this. I think more finer-grained error control is needed. will use #570 for tracking this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Handling | Redux-Saga
Errors in forked tasks bubble up to their parents until it is caught or reaches the root saga. If an error propagates to...
Read more >
Python Multiprocessing: Handling Child Errors in Parent
The problem I currently have is that, if a child process runs into an unhandled Exception, this is not recognized by the parent...
Read more >
Using fork() to produce 1 parent and its 3 child processes
Program to create four processes (1 parent and 3 children) where they terminates in a sequence as follows : (a) Parent process terminates...
Read more >
PARENT-CHILD TASK RELATIONSHIPS IN THE .NET ... - Microsoft
This document provides an in-depth explanation on parent-child task ... Exception Handling. ... but their use can also lead to subtle coding errors....
Read more >
Labs - Operating Systems - Nipun Batra
Question 2. Q: Write a program that opens a file (with the open() system call) and then calls fork() to create a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found