Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Coroutines: How to continue work even when children tasks fail or timeout

Wahib Ul Haq
January 28, 2019

Kotlin Coroutines: How to continue work even when children tasks fail or timeout

This talk was about sharing our learnings with the community while using Kotlin Coroutines for an interesting use case we had in our project. We are barely scratching the surface but I felt it might help others in making some better decisions with parent-child setup if they know the different uses of certain CoroutineScopes.

This talk was presented at Kotlin User Group Munich (KTUG) meetup hosted at KI labs.

Meetup link: https://www.meetup.com/Kotlin-User-Group-Munich/events/257927489/
Talk Recording: https://www.pscp.tv/w/1RDxlqmypQjKL

Wahib Ul Haq

January 28, 2019
Tweet

More Decks by Wahib Ul Haq

Other Decks in Technology

Transcript

  1. How to continue even if Children Timeouts or Fail Wahib

    Ul Haq
  2. None
  3. Coroutine are light-weight threads, with little overhead and easier to

    be controlled by the developers.
  4. None
  5. Interesting Use Case

  6. Event A Event B Event C Event D Computation Computation

    Computation Time Taken Certain Limit
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. What if there is an exception in one of the

    children’s tasks?
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. None
  25. The first exception wins

  26. None
  27. None
  28. None
  29. None
  30. This scope waits for the completion of all its children

    and in case of a crash the scope cancels all children.
  31. None
  32. Supervision Scope

  33. It propagates cancellation only downwards and cancels all children only

    if it has failed itself. Difference #1
  34. Child’s failure is not propagated to the parent. Hence, every

    child should handle its exception by itself. Difference #2
  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. Use Coroutine Scope if you want your parent job to

    fail if one of the children fails
  46. Use Coroutine Scope if you want your parent job to

    fail if one of the children fails Use Supervision Scope if you want your parent job to complete in case of failure of one or more of children jobs
  47. Use Coroutine Scope if you want your parent job to

    fail if one of the children fails Use Supervision Scope if you want your parent job to complete in case of failure of one or more of children jobs CoroutineExceptionHandler can be used to replace try /catch blocks and handle exceptions in a centralised and cleaner way
  48. Use Coroutine Scope if you want your parent job to

    fail if one of the children fails Use Supervision Scope if you want your parent job to complete in case of failure of one or more of children jobs CoroutineExceptionHandler can be used to replace try /catch blocks and handle exceptions in a centralised and cleaner way InvokeOnInvocation() can be used to react to changes like when parent job completes (gracefully or not) or Timeout Cancellation happens
  49. https://kotlinlang.org/docs/reference/coroutines/exception-handling.html Helpful Resource

  50. Thanks for listening! @wahibhaq