SupervisorJob

Kotlin SupervisorJob – Complete Beginner Guide
When working with Kotlin Coroutines, managing failures correctly is extremely important. In complex applications — especially Android apps — you may launch multiple coroutines that run in parallel.
But what happens if one coroutine fails?
By default, a failure in one child coroutine cancels its siblings. This is where SupervisorJob becomes powerful.
In this beginner-friendly guide, you will learn:
What SupervisorJob is
Why it is needed
Difference between Job and SupervisorJob
How coroutine cancellation works
Real-world Android examples
Common mistakes
Best practices
Let’s dive in
What Is a Job in Kotlin Coroutines?
Before understanding SupervisorJob, you need to understand Job.
A Job represents a coroutine’s lifecycle. It:
Tracks coroutine state
Handles cancellation
Manages parent-child relationships
Example:
Jobs form a hierarchy in structured concurrency.
How Coroutine Hierarchy Works
In structured concurrency:
Parent coroutine controls children
If parent is cancelled → children are cancelled
If child fails → parent is cancelled
This default behavior ensures safety.
But sometimes, this behavior is too strict.
The Problem with Default Job
Consider this:
What happens?
First coroutine fails
Parent Job is cancelled
Second coroutine also gets cancelled
This may not be desirable.
What Is SupervisorJob?
SupervisorJob is a special type of Job where:
Failure of one child does NOT cancel other children.
It isolates failures between sibling coroutines.
Basic Example of SupervisorJob
Result:
First coroutine fails
Second coroutine continues
This is the key benefit.
Job vs SupervisorJob Comparison
| Feature | Job | SupervisorJob |
|---|---|---|
| Child failure cancels siblings | Yes | No |
| Parent cancellation cancels children | Yes | Yes |
| Suitable for independent tasks | No | Yes |
| Suitable for strict error control | Yes | Sometimes |
SupervisorJob provides flexibility.
When Should You Use SupervisorJob?
Use SupervisorJob when:
Running independent tasks
Fetching multiple APIs in parallel
Handling UI events separately
Preventing single failure from crashing entire scope
Avoid it when:
Strict failure propagation is required
Real-World Android Example – Multiple API Calls
Suppose ViewModel loads:
User profile
Notifications
Messages
You don’t want one API failure to cancel others.
ViewModel with SupervisorJob
Each coroutine runs independently.
SupervisorScope vs SupervisorJob
Kotlin also provides supervisorScope.
supervisorScope Example
Difference:
SupervisorJob → scope-level configuration
supervisorScope → local block-level control
Use supervisorScope for smaller blocks.
How Cancellation Works in SupervisorJob
Important rules:
- Parent cancellation cancels children
- Child failure does NOT cancel siblings
- Child failure does NOT cancel parent automatically
SupervisorJob changes downward cancellation behavior only.
Handling Errors with SupervisorJob
SupervisorJob does not automatically handle errors.
You must catch exceptions.
Example:
Without try-catch, unhandled exception still crashes coroutine.
Common Beginner Mistakes
Thinking SupervisorJob Handles Errors Automatically
It only isolates failure. You still need error handling.
Replacing All Jobs with SupervisorJob
Not always correct. Sometimes strict cancellation is better.
Forgetting Structured Concurrency
Do not create unmanaged coroutine scopes.
Ignoring CoroutineExceptionHandler
Use it for global error handling.
Using CoroutineExceptionHandler with SupervisorJob
Provides centralized error logging.
Advanced Example – Parallel Data Fetch
Failures do not cancel other async blocks.
Performance Considerations
SupervisorJob:
Adds minimal overhead
Improves reliability
Prevents unnecessary cancellations
Keeps UI responsive
Use wisely in parallel tasks.
When NOT to Use SupervisorJob
Avoid when:
All tasks depend on each other
Failure in one must stop all
Strict transactional operations
Example: Payment processing.
Frequently Asked Questions (FAQs)
1. What is SupervisorJob in Kotlin?
It is a special Job that prevents child coroutine failures from cancelling sibling coroutines.
2. What is the difference between Job and SupervisorJob?
With Job, child failure cancels siblings. With SupervisorJob, it does not.
3. Does SupervisorJob cancel children when parent is cancelled?
Yes, parent cancellation still cancels children.
4. Is SupervisorJob used in Android?
Yes, it is commonly used in ViewModel for independent tasks.
5. Does SupervisorJob handle exceptions automatically?
No, you must still handle exceptions using try-catch or CoroutineExceptionHandler.
Conclusion
It is a powerful tool for managing coroutine failure isolation.
You learned:
How Job works
Why SupervisorJob is needed
Difference between Job and SupervisorJob
Android examples
Best practices
Common mistakes
Mastering SupervisorJob will help you build stable, resilient, and production-ready Kotlin applications.
