Back
+20 XP
Concept

Backpressure & Socket Write Buffer Draining

When writing faster than a peer can read, the kernel socket send buffer saturates. Non-blocking `sock.send()` performs partial writes or raises `BlockingIOError`. Proper backpressure tracks unsent bytes until the selector signals `EVENT_WRITE` 🌊.

python
import socket

s1, s2 = socket.socketpair()
s1.setblocking(False)

def drain_send(sock, data):
    while data:
        try:
            sent = sock.send(data)
            data = data[sent:]  # Slice off transmitted prefix
        except BlockingIOError:
            break  # Kernel buffer full: pause sending!
    return data

leftover = drain_send(s1, b"STREAM_DATA_CHUNK")
print(f"Unsent buffered bytes: {len(leftover)} 🌊")
s1.close()
s2.close()

# 6. Backpressure & Socket Write Buffer Draining When writing faster than a peer can read, the kernel socket send buffer saturates. Non-blocking `sock.send()` performs partial writes or raises `BlockingIOError`. Proper backpressure tracks unsent bytes until the selector signals `EVENT_WRITE` 🌊. ```python import socket s1, s2 = socket.socketpair() s1.setblocking(False) def drain_send(sock, data): while data: try: sent = sock.send(data) data = data[sent:] # Slice off transmitted prefix except BlockingIOError: break # Kernel buffer full: pause sending! return data leftover = drain_send(s1, b"STREAM_DATA_CHUNK") print(f"Unsent buffered bytes: {len(leftover)} 🌊") s1.close() s2.close() ``` Ignoring partial writes is the #1 bug in naive socket code: calling `sock.send(huge_payload)` silently drops trailing bytes when buffers fill! Always check return lengths or use `sock.sendall()` in blocking contexts 🛡️! --- ### Question What does it signify when a non-blocking `sock.send(payload)` returns fewer bytes than `len(payload)`? 🧠 - [ ] a. The OS kernel send buffer was full; only a partial slice was accepted, requiring backpressure buffering 🌊 - [ ] b. The socket connection encountered a permanent parity error - [ ] c. Python garbage collected the tail of the byte sequence - [ ] d. The receiving node requested packet truncation --- ### Challenge Implement `drain_send(sock, data)` returning unsent bytes. Send `b'TEST_DATA'` over non-blocking `s1` from `socket.socketpair()`. Print `len(drain_send(s1, b'TEST_DATA'))`. 🎯 <details> <summary>Solution</summary> ```python import socket s1, s2 = socket.socketpair() s1.setblocking(False) def drain_send(sock, data): while data: try: n = sock.send(data) data = data[n:] except BlockingIOError: break return data rem = drain_send(s1, b'TEST_DATA') print(len(rem)) s1.close() s2.close() ``` </details>

Checkpoint

What does it signify when a non-blocking `sock.send(payload)` returns fewer bytes than `len(payload)`? 🧠

Practice

Implement `drain_send(sock, data)` returning unsent bytes. Send `b'TEST_DATA'` over non-blocking `s1` from `socket.socketpair()`. Print `len(drain_send(s1, b'TEST_DATA'))`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks