szn

Bug 557 — Config and option parsing/scoping bugs

557. cfg.parseValue/options.coerceValue type mismatch, quoting defects, missing -u/setw -g propagation, and synchronous if-shell deadlock

Severity: HIGH
Status: ✅ RESOLVED
Files: src/cfg.zig:162-197, src/cfg.zig:244-275, src/cfg.zig:412-424, src/options.zig:152-195, src/cmd/cmd.zig:959-1089, src/server/server.zig:3647-3665

Problem

  1. Bidirectional cfg.parseValue + options.coerceValue type mismatch (src/cfg.zig:244-275, src/options.zig:152-195, src/cmd/cmd.zig:999):
    • In :set / CLI (setOptionInternal), cmd.parse has already stripped outer quotes from val_str. cfg.parseValue(val_str) then classifies any 1-character string (" ", "|", "a") or key name ("F1") as .key, any integer ("123") as .number, "on"/"off" as .flag, and "red" as .colour. When options.coerceValue runs for a .string option (like status-left, default-shell, word-separators), it rejects .key, .number, .flag, and .colour with error.TypeMismatch!
    • Conversely, in szn.conf, if a user quotes a non-string option (e.g. set -g prefix "C-a", set -g mouse "on", set -g history-limit "5000"), cfg.parseValue returns .string, which options.coerceValue rejects for .key, .flag, and .number options with error.TypeMismatch.
  2. Quoting and escaping defects in src/cfg.zig:
    • findUnescapedQuote (src/cfg.zig:412-424) checks if (s[i] == q and (i == 0 or s[i - 1] != '\\')), which fails when an even number of backslashes precedes the closing quote ("foo\\").
    • cfg.parseValue (src/cfg.zig:250-253) returns raw[1 .. raw.len - 1] for double-quoted strings without calling unescapeQuoted, leaving literal \" and \\ in option values.
    • stripInlineComment (src/cfg.zig:172) checks if (ch == '\\' and i + 1 < line.len) unconditionally even when in_single ('...') is true, so 'foo\' treats the closing ' as escaped.
  3. Incomplete flag support and setw -g propagation in setOptionInternal (src/cmd/cmd.zig:959-1089):
    • setOptionInternal does not support -u (unset) or combined flags (-gu, -gw, -q), even though cfg.zig supports set -u / set -gu.
    • set-window-option -g (setw -g) updates server.global_window_options and the single currently active window (session.active_window), but does not update session.window_options or any other open windows across sessions.
  4. Synchronous c.system() in if-shell can deadlock the server (src/server/server.zig:3647-3665): Server.applyDirectives executes if-shell synchronously via const ret = c.system(cmd_z.ptr);. Because the server is single-threaded, any if-shell command that runs an szn client command against the same server deadlocks permanently (or hangs the server if the shell command blocks).

Proposed Fix

  1. Parse option values directly against the target option’s OptionType from options.findOptionDef(opt_name) (or allow options.coerceValue to coerce between .string and .key/.flag/.number/.colour using the raw token text).
  2. Fix findUnescapedQuote and stripInlineComment to track backslash parity only outside single quotes, and unescape double-quoted strings.
  3. Support -u and combined flags (-gu, -gw, -q) in setOptionInternal, and propagate setw -g across all sessions/windows (or through Options.parent inheritance).
  4. Run if-shell asynchronously via Loop / SIGCHLD or set an environment/timeout guard so it cannot deadlock the server event loop.

Resolution

  1. Direct .string option preservation & bidirectional coercion (src/options.zig, src/cmd/cmd.zig, src/server/server.zig):
    • Added options.findOptionDef(name) and updated setOptionInternal to construct OptionValue{ .string = ... } directly when the target option’s declared type is .string, preventing cfg.parseValue from misclassifying unquoted CLI/prompt values like " ", "true", "on", "123", "F1", or "colour1" into non-string variants.
    • Updated options.coerceValue to coerce between .string and .key/.flag/.number/.colour/.choice (including preserving " " when coercing a space key to .string), and freed any temporary intermediate .string allocated by coerceValue inside Options.set.
    • Updated both setOptionInternal (applyOptionSideEffects) and Server.applyDirectives to read the coerced option value back from Options when applying side effects (prefix, codepoint-widths, variation-selector-always-wide, history-limit, pane-border-format, status_dirty), so quoted non-string values in szn.conf (e.g. set -g prefix "C-a", set -g history-limit "5000") and -u / -gu unsets trigger their runtime side effects.
  2. Quoting and escaping (src/cfg.zig):
    • Updated findUnescapedQuote and stripInlineComment to track backslash parity outside single quotes, and updated cfg.parseValue to verify that a double-quoted token’s closing quote is unescaped (findUnescapedQuote(s, 1) == s.len - 1) before unescaping via unescapeQuoted.
  3. Complete flag support (-u, -q, combined flags) and global session/window propagation (src/cmd/cmd.zig, src/cfg.zig, src/server/server.zig):
    • Added support for -u, -q, and combined flags (-gu, -gw, -gq, -gqu) in both setOptionInternal and cfg.parseSet.
    • Propagated set -g, setw -g, set -gu, and setw -gu across all open sessions (session.options, session.window_options) and windows (window.options) in both setOptionInternal and Server.applyDirectives.
  4. Non-blocking if-shell execution (src/server/server.zig):
    • Replaced blocking c.system() in Server.applyDirectives with spawnIfShellChild (fork + execvp("/bin/sh", ...) with /dev/null stdio and inherited fds $\ge 3$ closed).
    • Fast conditions that finish within the brief non-blocking WNOHANG check run immediately; slower or blocking conditions are queued into Server.pending_if_shells and reaped asynchronously via reapZombies / tickIfShells in Server.run (with a 5,000 ms timeout guard and teardown cleanup in Server.deinit), preventing single-threaded server deadlocks.