HarryDeveloper

HarryDeveloper

Programming Kotlin: Difference between SupervisorJob() and supervisorScope (324, 325)

Hi @venkats,

It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same effect - preventing the parent from cancelling when an unhandled exception occurs in one of the children

  1. Passing a SupervisorJob instance to the coroutine
  2. Wrapping the children inside a supervisorScope,

However, the example provided in ‘async/cancellationbidirectional.kts’ does use ‘SupervisorJob()’ in the parent ‘launch’, and parent job fails as mentioned in the book. When I try it, the result is the same: parent job fails, there is no successful 200 response, CancellationException is caught.

Why is that SupervisorJob() instance passage to the coroutine does not result in the desired behaviour? Using supervisorScope() works as expected. Is there any difference in using SupervisorJob() and supervisorScope()?

Please advise.

Thanks,
Harry

First Post!

venkats

venkats

Author of Programming Kotlin, Rediscovering JavaScript (and 6 other titles)

Hi @HarryDeveloper,

Thank you for the query.

The supervisorScope is a syntax sugar or a convenience function for creating a SupervisorJob. If the child coroutines run in a different SupervisorJob than the parent, then the cancellation of a Chile does not propagate to the parent.

It is much better to use supervisorScope than to use SupervisorJob, but from the curiosity point of view, you may try to create a SupervisorJob, something like this:

val job = launch(Dispatchers.IO + SupervisorJob() + handler) {

val supervisor = SupervisorJob()

launch(coroutineContext + supervisor) { fetchResponse(200, 5000) }
launch(coroutineContext + supervisor) { fetchResponse(200, 1000) }
launch(coroutineContext + supervisor) { fetchResponse(200, 3000) }

supervisor.children.forEach { it. join() }

/*
supervisorScope {
  launch { fetchResponse(200, 5000) }
  launch { fetchResponse(200, 1000) }
  launch { fetchResponse(200, 3000) }
}
*/

}

job.join()

Hope that answers your question.

Regards,

Venkat

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New

Sub Categories: