szn

Bug 506 — codepoint-widths is process-global but registered as a session option

506. Session-scoped option mutates a process-global width table

Severity: HIGH
Status: ✅ FIXED
Files: src/options.zig:259, src/server/server.zig:3547, src/char_width.zig:30-31

Problem

codepoint-widths is registered in the session option table:

// src/options.zig:259
.{ .name = "codepoint-widths", .type = .string, .default = OptionValue{ .string = "" } },

Registering it in SESSION_OPTIONS means it is inherited and settable per session, so set -t <session> codepoint-widths "U+2705=1" is accepted and stored in that session’s option table. The handler that reacts to the change then discards the session identity entirely and writes a module-global table:

// src/server/server.zig:3547
if (std.mem.eql(u8, s.option, "codepoint-widths") and s.value == .string) {
    char_width.applyCodepointWidths(self.allocator, s.value.string) catch |err| {
        std.log.warn("codepoint-widths: {s}", .{@errorName(err)});
    };
}
// src/char_width.zig:30-31
var override_count: usize = 0;
var overrides: [MAX_OVERRIDES]Override = undefined;

Two consequences follow:

  1. Cross-session bleed. Every session’s charWidth call reads the same overrides array. Setting the option on one session changes rendering in all others. Because these are ambiguous-width and emoji-presentation codepoints (bug #206), the visible effect is cursor drift and leftover characters in sessions that never asked for the override.
  2. Destructive interaction between sessions. applyCodepointWidths calls clearOverrides() and rebuilds from scratch (by design, so a full assignment replaces the table). A second session assigning the default empty string therefore erases the first session’s overrides rather than merely adding to them.

variation-selector-always-wide has the identical shape via setVariationSelectorAlwaysWide writing the module-global variation_selector_always_wide (src/char_width.zig:47).

Divergence from the reference implementation

tmux scopes this option at the server level, not the session level:

/* tmux/options-table.c:323 */
{ .name = "codepoint-widths",
  .type = OPTIONS_TABLE_STRING,
  .scope = OPTIONS_TABLE_SERVER,

So the correct end state is one of:

The current middle ground — advertised as session-scoped, implemented as process-global — is wrong under either reading. The docstring at src/options.zig:258 compounds the confusion by documenting the form set -g codepoint-widths ... (a global store) on an option registered in the session table.

Proposed Fix

Decide the intended scope, then make the registration, the handler, and the docstring agree:

  1. Server-scoped (tmux parity, smaller change). Move codepoint-widths and variation-selector-always-wide out of SESSION_OPTIONS into a server option table, reject set -t for them, and keep the single global override table. Update the comment at src/options.zig:253-258 to state that the table is process-wide.
  2. Per-session (honours AGENTS.md principle 4, larger change). Move overrides / override_count / variation_selector_always_wide onto Session, change charWidth(cp) to charWidth(cp, widths), and update the ~30 call sites in screen.zig, render.zig, grid.zig, and tty/.

Either way, add a regression test asserting that a second session’s assignment does not alter the first session’s resolved widths.

Resolution

Fixed. codepoint-widths and variation-selector-always-wide are now declared server-scoped and behave that way.

Five regression tests: three in cmd.zig (bare set-option lands in the server store and the active session mirrors it; the server store is the single authority; variation-selector-always-wide is server-scoped) and two in options.zig (the predicate, including near-miss names, and that both names remain registered in SESSION_OPTIONS with the right type so the set -g write cannot fail with UnknownOption).

One finding from implementing this: the original report assumed sessions each kept a live view of the global store. They do not — Session.init clones it, and setOptionInternal deliberately mirrors a -g write onto the active session so live config still applies (bug #506’s “session clone” note). So a session that was never active keeps its creation-time snapshot. That is the general snapshot-inheritance model shared by every option and is unchanged here; it does not affect rendering, because the width table is process-wide and holds the single applied value. show-options on a non-active session will therefore still report the snapshot for these two options, which is a pre-existing property of the option model rather than a regression.