rust icon indicating copy to clipboard operation
rust copied to clipboard

TcpListener::bind alwary success though specific port already occupied

Open swq123459 opened this issue 1 year ago • 1 comments

swq123459 avatar Aug 26 '24 08:08 swq123459

Please provide steps to reproduce or an overview over the bound sockets after this happens. There are enough complications about multiple network interfaces, IP addresses etc. that it's not obvious what could be the issue here.

the8472 avatar Aug 26 '24 12:08 the8472

The following example can reproduce this issue, this program can still running though i see this addr was already occupied image

swq123459 avatar Aug 27 '24 01:08 swq123459

When i impl this example by golang (or another programming language), it can not work in port-occupied situation

	listener, err := net.Listen("tcp", "0.0.0.0:35628")
	if err != nil {
		fmt.Println("Error creating listener:", err)
		os.Exit(1)
	}
	defer listener.Close()

swq123459 avatar Aug 27 '24 01:08 swq123459

For me, your example fails as expected with:

Error: Os { code: 98, kind: AddrInUse, message: "Address already in use" }

strace (or equivalent on whatever platform we're talking about) output would be informative.

ctz avatar Aug 27 '24 17:08 ctz

For me, your example fails as expected with:

Error: Os { code: 98, kind: AddrInUse, message: "Address already in use" }

strace (or equivalent on whatever platform we're talking about) output would be informative.

When i run this example to occupy this addr, another example can no use this address and raise the above error ( which is expected), but when this addr was occupied by another (no example code) process (like ssh 127.0.0.1:22 or even port 127.0.0.1:1 ), this addr can be use by this example (wire)

swq123459 avatar Aug 27 '24 18:08 swq123459

I think you're observing the effect that https://github.com/rust-lang/rust/blob/748c54848dc2964b7e133f945cabe5bc64079947/library/std/src/sys_common/net.rs#L403-L411 achieves.

But SO_REUSEADDR does not allow binding if there is an active listening socket elsewhere, so some of your description is still confusing.

ctz avatar Aug 28 '24 07:08 ctz