Deadlock in Channel in Golang
Overview
We can transmit values over typed channels by using the channel operator. As a result, a channel gives goroutines an alternative means of communication to locks. The channel is bidirectional, which means that data can be sent and received simultaneously.
What is Deadlock?
Deadlock in Channel in Golang is a condition that happens when a few goroutines are waiting for each other but no goroutine can proceed. The program gets blocked thus leading to a deadlock. Suppose we want to write or read from a channel but the channel does not contain any value therefore the execution of that goroutine will be blocked and the control will go to some other goroutine but if the other goroutines are also blocked due to the same reason or it might be that there are no other goroutines present. The system enters a deadlock.
Under What Situations Does a Deadlock Occur in the Golang Channel?
Several conditions may lead to a deadlock in channel in golang. Suppose a channel is called to read or write but the channel does not contain any value. The execution of that goroutine will be blocked and the control will go to some other goroutine. Therefore the situations that might lead to the deadlock include:
1. Read from an Empty Channel
If you try to read from an empty channel then it leads to a deadlock.
Example 1
The output of the code is
Both channels do not contain any value. The program enters a deadlock.
Example 2
The output of the above code is
2. Sending Data to a Full Channel
All the channels has a capacity to store the data. For an unbuffered channel it is 1 and for the buffered channel it is the initialize value. If the channel stores the data at its full capacity, then next send data call will results in deadlock.
The output of the above code is
Ways to Detect Deadlock in Golang Channel
The threads that the program creates serve as the foundation for the deadlock detector's investigation. If there are more threads generated and active than threads waiting for work, a deadlock has occurred. Four threads are created when a deadlock is created
- When a program starts then a thread is created
- A thread known as sysmon keeps track of the system.
- A thread that is created by the goroutines for the garbage collector.
- When the main goroutine is blocked during initialization, one thread is generated. Go must establish a new thread to offer the other goroutines running time because this one is locked to its thread.
The detector is informed each time a thread becomes idle. The debug displays an increasing number of idle threads on each line. When the number of idle threads is the same as the number of active threads minus the system thread, a deadlock occurs.
Solutions to Resolve Deadlock in Golang Channel
The switch statement in Golang provides us with a feature of default case to avoid the program from blocking and getting into a deadlock. Let us consider an example.
In the above code, both channels are empty. Therefore our program will lead to deadlock but since in the switch statement we are using a default case, therefore the program is prevented from getting into a deadlock. The output of the above code is
Conclusion
Let us summarize what we have learned so far
- A channel acts as a medium that enables the goroutine to communicate amongst each other and the communication is lock-free.
- Deadlock in Channel in Golang is a condition that happens when a few goroutines are waiting for each other but no goroutine can proceed. The program gets blocked thus leading to a deadlock.
- The threads that the program creates serve as the foundation for the deadlock detector's investigation. If there are more threads generated and active than threads waiting for work, a deadlock has occurred.
- The switch statement in Golang provides us with a feature of default case to avoid the program from blocking and getting into a deadlock.