Severity: LOW (code quality)
Status: ✅ FIXED
Files: src/server/dispatch.zig:140-181, src/server/server.zig:2714-2753, src/layout.zig:295-303, src/mode_copy.zig:769-842, src/cmd/cmd.zig
| # | Duplication | Locations |
|---|---|---|
| 1 | Non-blocking write loop: write → retry INTR → on EAGAIN stash the remainder and arm POLLOUT → advance offset |
dispatch.zig:140-181 (sendResponse), server.zig:2714-2753 (sendRequestCellSize), server.zig:2884-2943 (writeFrameToClient), server.zig:2949-2992 (flushDisplayClient), pty.zig:291-346 (writeInput/flushInput) |
| 2 | Byte-identical function bodies | layout.zig:295-303: removePane and extractPane are the same three lines under two names |
| 3 | Mirrored keymaps: arrows, page up/down, home/end | mode_copy.zig:769-816 (handleViKey) vs :822-923 (handleEmacsKey) |
| 4 | Three-line active-target prologue | ~30 sites in cmd.zig, e.g. cmdSendKeys:150-152, cmdSplitWindow:161-163, cmdSelectPane:303-304, cmdKillPane:312-314, cmdSwapPane:321-323, cmdPasteBuffer:511-513 |
The five write loops are not merely redundant — one has already drifted.
sendRequestCellSize writes only a 5-byte header inline, does not break after
the first EAGAIN when out_buf was already non-empty, and silently ignores
non-EAGAIN write errors. Bugs #262 (“protocol message corruption on partial
write in sendRequestCellSize”), #403 (“busy-spins forever on EAGAIN”) and
#472 (“dispatch write loops slice with @intCast(n) without clamping”) are all
defects that existed because the loop was copy-pasted rather than shared.
Site 2 invites the two call sites to drift, as Window.removePane /
Window.extractPane already have (they differ only in the empty-list branch and
whether resize runs).
writeOrQueue(fd, data, *out_buf, *events) helper (plus one
MAX_OUT_BUF cap helper) and route all five sites through it.Layout.extractPane; alias it to removePane.applyMovement(k, grid) bool that
both copy-mode keymaps call.activeTarget(server) ?Target helper returning
{ session, window, pane }.Bug #477 was “duplication and dead-code cluster 3” and covered triplicated
split math (splitSizes call sites) and mirrored resize handlers. Site 1’s
write loops, site 2 and site 4 are not covered there and should be reconciled
against it when #477’s fix is re-read.
Deliberate deviation from the Proposed Fix: site 1 was not collapsed into a
single writeOrQueue(fd, data, *out_buf, *events) helper. The five callers have
genuinely different buffering policies that are load-bearing:
dispatch.sendResponse is synchronous (it usleep-retries inline and
never stashes) — folding it into a stash-and-arm-POLLOUT helper would change
its timing and risk regressing #262/#403/#472, the exact bugs site 1 exists to
protect.server.sendRequestCellSize / writeFrameToClient / flushDisplayClient and
pty.writeInput / flushInput are non-blocking stash-and-arm loops with
distinct partial-write recovery and event-mask bookkeeping.So the fix centralizes only the truly shared fragment — the raw
write(2) + errno + EINTR/EAGAIN dispatch — into a new src/io.zig:
pub const WriteOutcome = union(enum) { wrote: usize, would_block, closed, failed };
pub fn writeOnce(fd: i32, buf: []const u8) WriteOutcome { ... }
Each caller switches on WriteOutcome, preserving its own buffering policy:
| Site | File | Policy preserved |
|---|---|---|
sendResponse (header+body) |
src/server/dispatch.zig |
inline usleep retry on .would_block; .closed→ConnectionClosed, .failed→WriteFailed |
sendRequestCellSize |
src/server/server.zig |
non-blocking stash-on-partial / EAGAIN, but .closed falls through to the original queue-whole-slice path |
writeFrameToClient |
src/server/server.zig |
.would_block/.closed→break, .failed→clear+removeFdEvents+return true |
flushDisplayClient |
src/server/server.zig |
same mapping as writeFrameToClient |
writeInput / flushInput |
src/server/pty.zig |
.would_block/.closed→break, .failed→WriteFailed/clear+return true |
Layout.extractPane): collapsed to byte-identical delegation onto
removePane (src/layout.zig).mode_copy.zig): extracted applyMovement(k, grid) bool shared by
handleViKey and handleEmacsKey. Also fixed a latent bug: handleEmacsKey
called moveToLineEnd() with no argument, but moveToLineEnd(self, grid) takes
the grid — corrected to self.moveToLineEnd(grid). Both keymaps now also honor
escape → .exit_mode.cmd.zig): added activeTarget(server) ?ActiveTarget returning
{ session, window, pane } and replaced the ~11 uniform three-line prologues
with it. Each handler binds only the fields it actually uses (Zig hard-errors
on unused locals), so over-declaration was trimmed per function.No new test cases were added for #520 itself — the cluster is pure refactor
(behaviour-preserving), and the full suite (1058 tests) passes on both Zig
0.16.0 and 0.17.0 after the change, which is the regression gate. The
moveToLineEnd arity fix is exercised by the existing copy-mode movement tests.
bash /tmp/bt.sh → 0.16.0 rc=0 All 1058 tests passed. /
0.17.0 rc=0 All 1058 tests passed. / BOTH TOOLCHAINS OK.io.zig, dispatch.zig, server.zig, pty.zig, layout.zig,
mode_copy.zig, cmd.zig all compile and link under both toolchains.