google / google/gopacket

Call handle.Close method has been blocked

Open
#1,126 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.8k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I found a similar issue

[https://github.com/google/gopacket/issues/253](https://github.com/google/gopacket/issues/253)

I noticed that this problem has been fixed, but it seems to be reproduced in my code, and the version I am using is **v1.1.19**
My code is as follows
```
func Tcpdump(device string, filter string, filePath string, fileName string, cancelChan chan struct{}) (err error) {
var (
handle *pcap.Handle
file *os.File
)
handle, err = pcap.OpenLive(device, 65535, true, pcap.BlockForever)
if err != nil {
return
}
defer func() {
fmt.Println("begin close...")
handle.Close()
fmt.Println("closed...")
}()
if err = handle.SetBPFFilter(filter); err != nil {
return
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
if err = os.MkdirAll(filePath, 0750); err != nil {
return
}
fileFullPath := ""
if strings.HasSuffix(filePath, "/") {
fileFullPath = filePath + fileName
} else {
fileFullPath = filePath + "/" + fileName
}
file, err = os.Create(fileFullPath)
if err != nil {
return
}
defer func() {
_ = file.Close()
}()
packetWriter := pcapgo.NewWriter(file)
if err = packetWriter.WriteFileHeader(65535, handle.LinkType()); err != nil {
return
}
for {
select {
case packet := <-packetSource.Packets():
if err = packetWriter.WritePacket(packet.Metadata().CaptureInfo, packet.Data()); err != nil {
return
}
case <-cancelChan:
return
}
}
}
```
I found that this function cannot end normally, and "**begin close...**" can be output normally but "**closed...**" cannot be output

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.