Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PacketSource ReClose Error #1189

Open
tongchengbin opened this issue Sep 27, 2024 · 1 comment
Open

PacketSource ReClose Error #1189

tongchengbin opened this issue Sep 27, 2024 · 1 comment

Comments

@tongchengbin
Copy link

I want to create a temporary source and close it after use. The handle needs to be used for a longer period of time, but after I finally close the handle, it results in the source channel being closed multiple times.

func TestSourceClose(t *testing.T) {
	h, err := pcap.OpenLive("\\Device\\NPF_Loopback", 65536, false, time.Second*3)
	assert.Equal(t, nil, err)
	source := gopacket.NewPacketSource(h, nil)
	go func() {
		for _ = range source.Packets() {
			continue
		}
		println("done")

	}()
	close(source.Packets())
	source2 := gopacket.NewPacketSource(h, nil)
	go func() {
		for _ = range source2.Packets() {
			continue
		}
		println("done2")
	}()
	time.Sleep(1 * time.Second)
	h.Close()

}
panic: send on closed channel

goroutine 18 [running]:
github.com/google/gopacket.(*PacketSource).packetsToChannel(0xc0000215c0)
	D:/code/github.com/gopacket/packet.go:820 +0xb3
created by github.com/google/gopacket.(*PacketSource).Packets in goroutine 7
	D:/code/github.com/gopacket/packet.go:861 +0x98
panic: send on closed channel
	panic: close of closed channel

goroutine 8 [running]:
github.com/google/gopacket.(*PacketSource).packetsToChannel(0xc0000215c0)
	D:/code/github.com/gopacket/packet.go:820 +0xb3
created by github.com/google/gopacket.(*PacketSource).Packets in goroutine 6
	D:/code/github.com/gopacket/packet.go:861 +0x98
@Surya-7890
Copy link

I see that you are closing the channel here "close(source.Packets())", like this.

You are closing the channel right after the go routine. Try using defer in your code.

defer close(source.Packets()) 

This way the channel will only be closed, when the function completes its execution (function returns).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants