Interplay of Net Package Import and Deadlock Detection
In a Go program, if a channel operation blocks while the program is running, the program will eventually receive a "deadlock" error. However, the behavior changes when the net package is imported.
The code snippet in question:
package main
import (
"fmt"
"net/http"
)
func main() {
var ch = make(chan int)
ch If the net/http package is not imported, the program exits with a "deadlock" error. This is because the channel operation (sending to an unbuffered channel) blocks forever, and no other goroutine is running to perform asynchronous operations that would allow the deadlock detector to identify the issue.
However, when the net/http package is imported, the program does not deadlock. This is because importing the net package starts background polling Goroutines that effectively disable the deadlock detector.
The net package includes functionality for managing network connections, and it uses Goroutines to handle connections asynchronously. These background polling Goroutines keep running even if no HTTP connection is currently being established or processed, which makes the program appear non-blocking to the deadlock detector. As a result, the program does not exit with a "deadlock" error, despite the blocked channel operation.
This behavior has been discussed in the GitHub issue https://github.com/golang/go/issues/12734.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3