frogscoopersoftware
frogscooper / blog /

Porting cbtorrent's Zig dataplane to Windows

· cbtorrent, zig, windows

Porting cbtorrent's Zig dataplane to Windows

cbtorrent is made of two languages, Python and Zig. Right now Python calls the shots: it decides which piece to claim, from whom, and when to steal. FlowNet, the adaptive ML at the heart of cbtorrent, is also written in Python. A Zig data layer (the dataplane) handles the data processing: sockets, framing, block assembly, SHA-1, positional disk I/O, and so on. To the uninitiated that's nothing special. To the miserable lot among us who have had to port something to Windows, it's a pain all too familiar.

0.4.0 shipped the first Windows native build. 0.4.1 brought it to parity. Between those two releases I hit almost every cross-platform trap I know of.

a SOCKET is not a file descriptor

On POSIX, a socket is an int. On Windows, a socket is a SOCKET: a pointer-sized handle, 64 bits on modern Windows. My C ABI originally took cb_conn_new(fd: i32, ...), which silently truncates handles above 2^31. The fix is one line of Zig (fd: i64, then @ptrFromInt), but changing anything ABI-related generally means changing everything ABI-related: every caller, the Python ctypes signature, and the ABI version constant all have to move in concert. I ended up bumping the ABI twice, to 13.

the event loop is completely different

Windows' default asyncio loop is ProactorEventLoop (IOCP), and it has no add_reader/add_writer. Everything in native.py that registered a raw descriptor with the loop was written for POSIX. I had to add _SocketPoller: one background thread runs select() over the registered handles and schedules callbacks back onto the loop with call_soon_threadsafe. Each ready handle has to be disarmed while its callback is pending, otherwise select() chews up 100% of a CPU core.

Storage safety rules are one of the things you don't want to weaken when porting to a new platform. Nothing gets written before its SHA-1 matches, every path component is opened without following links, and writable files must be regular files with a single hard link. On POSIX that's openat with O_NOFOLLOW plus a fstat.

Windows does have the tools for this, thankfully: CreateFileW with FILE_FLAG_OPEN_REPARSE_POINT, a component-by-component walk using directory handles, and GetFileInformationByHandle to check reparse points, file type, link count and size. Positional I/O is OVERLAPPED + ReadFile/WriteFile + GetOverlappedResult instead of pread/pwrite. The API surface is completely dissimilar, and it made the past couple of days very bleak. I'd be doomed without Windows CI.

self-pipes

Two native services wake Python through a pipe: the reactor thread (one byte when a connection is ready) and the async write pool (one byte per completion batch). Windows has no portable pipe() that's plug-and-play for my needs, and the Proactor loop can't watch a pipe fd anyway.

I ended up introducing a NotifyFd abstraction in the Zig core: a pipe on POSIX, and a non-blocking loopback TCP socket pair on Windows. The reactor thread gained a WSAPoll implementation, same structure as the poll path but over SOCKETs, with no fd_set size limit to work around. Hashing was solved with the Win32 path walk and overlapped reads, so the Python hasher doesn't have to run on Windows.

where that leaves us

0.4.1 has the Windows core running the reactor thread, native create hashing, and the async write pool, with both x64 and arm64 wheels (a wheel is just a release target for a certain platform). The Windows CI job runs the native test suite, so I don't skip over reactor tests.

If you're interested in cbtorrent and where it's headed, it's public at frogscooper/cbtorrent. The native core is in native/src/, and docs/PR_NOTES.md has short AI-produced notes on each of the changes.

← all posts