NetworkConnection - Send not throwing?

Hi,

I played around the last days with the new NetworkConnection API from Network framework that supports structured concurrency. I discovered a behavior, which is unexpected from my understanding.

Let's say you have a dead endpoint or something that does not exist. Something where you receive a noSuchRecord error. When I then try to send data, I would expect that the send function throws an error but this does not happen. The function now suspends indefinitely which is well not a great behavior.

Example simplified:

func send() async {
    let connection = NetworkConnection(to: .hostPort(host: "apple.co.com", port: 8080)) {
        TCP()
    }
    do {
        try await connection.send("Hello World!".raw)
    } catch {
        print(error)
    }
}

I'm not sure if this is the intended behavior or how this should be handled.

Thanks and best regards, Vinz

Answered by DTS Engineer in 867825022

The concept of a ‘dead connection’ is not well-defined in most networking protocols. For example, TCP will happily keep trying to send a segment of data until either the remote peer ACKs it or you tell it to stop.

This is both a blessing and a curse. On the one hand, you can accidentally trip over your Ethernet cable and then plug it back in without breaking your multi-hour upload. OTOH, when writing code you have to handle this reality.

Network framework tends to follow the model of the underlying protocol, and hence the behaviour you’re seeing.

How you handle this depends on the nature of your networking. It’s very common for folks to install a viability handler and have it close things down if the connection becomes unviable. I talk about that in Moving from Multipeer Connectivity to Network Framework.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The concept of a ‘dead connection’ is not well-defined in most networking protocols. For example, TCP will happily keep trying to send a segment of data until either the remote peer ACKs it or you tell it to stop.

This is both a blessing and a curse. On the one hand, you can accidentally trip over your Ethernet cable and then plug it back in without breaking your multi-hour upload. OTOH, when writing code you have to handle this reality.

Network framework tends to follow the model of the underlying protocol, and hence the behaviour you’re seeing.

How you handle this depends on the nature of your networking. It’s very common for folks to install a viability handler and have it close things down if the connection becomes unviable. I talk about that in Moving from Multipeer Connectivity to Network Framework.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NetworkConnection - Send not throwing?
 
 
Q