tcp-sprint/README.md

31 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2026-03-15 23:22:20 -07:00
# [Will It Blend? - TCP Streams](https://youtu.be/l69Vi5IDc0g)
2026-03-13 22:04:26 +00:00
This script demonstrates the risk of interleaving writes when cloning
`TcpStream` handles across threads, re:
[this discussion in the Servo Zulip](https://servo.zulipchat.com/#narrow/channel/263398-general/topic/TcpStream.3A.3Atry_clone.28.29.20thread.20safety.20in.20devtools/with/578172763).
2026-03-15 23:22:20 -07:00
It spawns 3 threads:
- 2 TCP clients, each of which sends predictable chunks of bytes over a shared
connection with `TcpStream::write_all()`
- 1 TCP listener, which tests whether messages arrive contiguously or scrambled
together
2026-03-13 22:04:26 +00:00
Messages consist of the range `0x00..=0xff`, repeated to fill the desired
2026-03-15 23:22:20 -07:00
message size. The TCP listener prints a warning if it detects any byte received
out of order.
2026-03-13 22:04:26 +00:00
## Usage
```sh
cargo run --release <message size in bytes>
2026-03-13 22:04:26 +00:00
```
## Results
A message size of 1024 bytes can be sufficient to trigger fragmentation when
running an optimized build. Results from debug builds are hit-or-miss depending
on the host system: they may have no issues with fragmentation regardless of
message size, or they may encounter issues at closer to 4096 bytes.
![Terminal screenshot showing results of running script with 512 and 1024 byte messages. The first run has no errors, while the second one displays two "out of order" warnings.](./screenshot.png)