Deadlock in Channel in Golang

Learn via video courses
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more