
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
- Passing a SupervisorJob instance to the coroutine
- 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
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
Popular Pragmatic Bookshelf topics









Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)

Other popular topics










Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /debian
- /nixos
- /lisp
- /sublime-text
- /textmate
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /django
- /manjaro
- /diversity
- /lua
- /nodejs
- /julia
- /slackware
- /c
- /markdown