Review of PR #918: "perf(sfu): send media from all goroutines at once on the shared UDP socket"
From mnema-talk v3.3.13, rendered on this site.
Independent adversarial review of the first commit (a direct sendto(2) path for the SFU's mux sockets and the built-in TURN relay's socket, and counters for Pion's silent buffer drops). Verdict on that commit: MERGE AFTER FIXES. No correctness or security defect was found. The findings were gaps in tests and docs, and the second commit addresses them; see the resolution column.
Findings
| # | Severity | Finding | Resolution |
|---|---|---|---|
| F1 | Medium | The EAGAIN fallback (internal/fastudp/fastudp_linux.go) was untested. A mutation that returned the error instead of falling back passed every test. | sendto is a package variable that tests replace. TestFullSendBufferFallsBackToTheRuntime asserts that a would-block send still goes out through the runtime with the full length. TestDirectSendErrorsAreReturned and TestSendtoReportsWouldBlockAsNotDone cover the other outcomes. |
| F2 | Medium | The buffer counter test called Write, but srtp writes through WriteWithAttributes. Deleting the override would have silenced the counter without failing the test. | The test now writes through WriteWithAttributes, the method Pion calls, and fails if the buffer loses it. |
| F3 | Low | The mux counter depends on Pion's exact log text, and nothing checked it. | TestPionLogLinesStillMatch reads the Pion module source (go list -m) and asserts that both matched log lines still exist (the mux drop and the unhandled-RTCP line). It skips only when the source is unavailable. |
| F4 | Low | The mux socket test listened on udp4, while Pion listens on udp with an interface address. A wildcard bind would yield a dual-stack socket that is not wrapped. | The test now opens the socket exactly the way Pion's mux does. |
| F5 | Low | doc/benchmark.md had no entry for the result, and reason="mux" also counts RTCP and DTLS. | The new section "The mixed Talk holds: the socket's write lock" has the before and after runs. doc/operations.md states that mux also counts RTCP and DTLS packets of any member. |
| F6 | Info | Write deadlines are not applied on the direct path, a writer that falls back may wait behind direct writers, and direct-path errors carry no addresses. | Accepted. Pion's mux uses deadlines only to unblock writes stuck for 5 s, and a direct send never blocks. The fallback fairness note is in the package doc. No caller reads the error's address fields. |
What was verified
- Descriptor lifetime:
- Every direct send reads
closedunder a read lock, andClosesets it under the write lock before closing the socket. - Every close path of these sockets goes through
Conn.Close: Pion'sUDPMuxDefault.Closeand its constructor's error path,turn.Server.Close, and the relay's closers. - Nothing closes the embedded conn directly.
- A send therefore never reaches a released or reused descriptor number.
- Every direct send reads
- System call:
- EINTR is retried, and UDP never sends partially.
- A zero-length payload takes the runtime's path.
- The port is written in network byte order; a mutation swapping it fails two tests.
- IPv4-mapped destinations are unmapped. IPv6 and dual-stack sockets are not wrapped.
- The
unsafe.Pointerconversions sit in theSyscall6argument list. -gcflags=-mshows that neither the payload nor the address escapes: 0 allocations per send.
- Pion integration:
asAddrPortReaderWriterpicks the wrapper'sWriteToAddrPort, and buffer sizes still reach the socket.- No ECN reader is lost, because mux candidates never had one.
- pion/turn uses only
ReadFrom,WriteToandLocalAddrand asserts no concrete type. - The RTCP buffer is still a plain
attrBuffer, so the existing type assertion holds.
- Security:
- A remote peer cannot influence the descriptor.
- Destinations still come only from Pion's ICE and STUN addresses and from the TURN 5-tuple.
- The packet count per input is unchanged, so there is no amplification and no new panic path.
- Runs:
GOMAXPROCS=4 go test -race -count=3 ./internal/fastudp ./internal/turnrelayand the targeted./internal/sfutests pass on macOS.- The Linux test binaries (arm64) pass in an Alpine container.
go test -racepasses in a Linux Go container, exercising the direct path.BenchmarkParallelSendon Linux arm64 with 4 goroutines: 724 ns/op through the runtime's lock against 314 ns/op direct.
- Repo limits:
- Files:
transport.go377 lines,ingress.go346,fastudp.go117. - Test files, functions and cognitive complexity are within limits.
- Files: