Skip to content

libobs: Keep the Wayland keymap if one won't load - #2976

Merged
zackees merged 2 commits into
masterfrom
fix/bug-a-keymap-that-xkbcommon-can-286
Oct 30, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/bug-a-keymap-that-xkbcommon-can-286

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2975

What and why

On Wayland, the hotkey platform's wl_keyboard.keymap handler (platform_keyboard_keymap() in libobs/obs-nix-wayland.c.h) dropped the old keymap and state before it looked at the new one. When the compositor sends a keymap libxkbcommon can't compile, at startup or on any layout change, xkb_keymap_new_from_string() returns NULL and xkb_state_new(NULL) crashes OBS. The handler also compiled the fd of a no_keymap event, and compiled against a NULL xkb context when libxkbcommon found no keyboard data (the situation of #2437).

The handler now ignores any format but WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 and does nothing without a context (closing the fd either way). When compilation fails it keeps the previous keymap and state and logs [wayland] could not compile the compositor's keymap, keeping the previous one, so hotkeys go on with the last layout that loaded, or stay without one if none ever loaded (as they already do before the first keymap arrives).

Found reading the code; no upstream issue. Upstream OBS has the same handler.

RED → GREEN

  • RED commit: 0c4b6f6 adds test_wayland_keymap, which compiles in the real Wayland hotkey platform and hands its keymap handler keymaps through a memfd, as the compositor does. On master 4 of its 5 cases crash: broken_keymap_keeps_previous, broken_first_keymap, no_keymap_format_is_ignored and no_xkb_context each fail with Test failed with exception: Segmentation fault(11); good_keymap_loads passes.
  • GREEN: 4748929. test_wayland_keymap passes 5/5 against the C build, hybrid libobs and libobs-c-reference, and under ASan/UBSan with no reports. The cmocka suite: C build 630 tests, hybrid 630 and C reference 630, the Wayland tests (test_wayland_keymap, test_wayland_multiseat, test_wayland_hotkeys, test_wayland_volume_keys) passing in each. The only failures under parallel load were the pulse, v4l2, X11 hotplug and core shutdown tests, and each passes alone (the two that need X under Xvfb). Skips: 8 on the C and hybrid runs, 9 on the C reference (the extra one, test_pulse_default_follow, skips alone in every mode on this host). Exported symbols are identical on the C build, hybrid and C reference (1767 each). Local gates: --test-diff origin/master, lint and cross pass. The full runs were on 0acb92bc3, the same change before a rebase onto the macOS libcurl commit (infra: record the measured macOS system libcurl in the Brotli carve-out (#2907) #2977); after it, on 4748929, test_wayland_keymap passes again on all three libraries, the export sets are still identical, and lint and the port_comparison bench test pass. The local gate stamp needs Docker for its Tier 2 lane, which this machine cannot use, so the head is unstamped and PR CI runs the full suite.
  • Real app: the C build of obs-studio-rust-gui --multi on a headless sway (its own HOME, no audio) starts on Wayland (Platform: Wayland), takes sway's keymap with no warning, runs for 20 s and shuts down cleanly (Freeing OBS context data). No compositor at hand sends a keymap that fails to compile, so that path, and its log line, is shown by the test:
    [ RUN      ] broken_keymap_keeps_previous
    xkbcommon: ERROR: [XKB-769] (input string):1:14: syntax error, unexpected identifier
    xkbcommon: ERROR: [XKB-822] Failed to parse input xkb string
    warning: [wayland] could not compile the compositor's keymap, keeping the previous one
    [       OK ] broken_keymap_keeps_previous
    

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: not a port)

@zackees

zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member

Coordinator tick: the Tier 2 Linux lanes are red in the new test itself, and the fix it expects is not in this PR yet.

Both lanes fail identically (Build 🧱 / Rust Tier 2 Linux 🐧 (tests ON) job 123854223779 and (tests OFF) job 123854223940, run 40990686177, head 2d372fdc):

610/616 Test #297: test_wayland_keymap ............***Failed
xkbcommon: ERROR: [XKB-822] Failed to parse input xkb string
[  ERROR   ] --- Test failed with exception: Segmentation fault(11)
[  FAILED  ] broken_keymap_keeps_previous
[  FAILED  ] broken_first_keymap
[  FAILED  ] no_keymap_format_is_ignored
[  FAILED  ] no_xkb_context

4 of the 5 cases crash; only the good-keymap case passes. That is the master behaviour your commit message describes (xkb_state_new(NULL) after the old keymap and state are dropped), so the RED half is doing its job — but git diff --stat origin/master...pr2976 shows this branch is test-only: test/cmocka/test_wayland_keymap.c and test/cmocka/tests.d/test_wayland_keymap.cmake, 198 insertions, and no change to obs-nix-wayland.c.h. The GREEN half — keeping the previous keymap/state when the new keymap won't compile, skipping the no_keymap fd, and not compiling without an xkb context — still needs to land here.

Also worth a look before you mark it ready: the no_xkb_context case needs a way to make xkb_context_new() fail that does not depend on the runner's keyboard data; the log shows all four crashes, so the assertion order in the failing cases is only reached after the segfault.

Leaving the PR in draft. Ping me when the handler fix is in and the lanes are green, or reply here if the master-side fix belongs in a separate PR ahead of this one — in that case split the test off onto the fix's branch so the merge queue does not see a permanently red Tier 2.

A new cmocka test, test_wayland_keymap, compiles in the real Wayland
hotkey platform and hands its wl_keyboard.keymap handler keymaps
through a memfd, as the compositor does. It expects a keymap that
doesn't compile, a no_keymap event and a missing xkb context to leave
the last good keymap in place, or none when there was none.

On master the handler drops the old keymap and state first and then
dereferences the NULL keymap (xkb_state_new), so four of the five
cases crash.

Refs #2975.
platform_keyboard_keymap() dropped the old keymap and state before it
looked at the new keymap, so a keymap libxkbcommon couldn't compile
crashed OBS in xkb_state_new(NULL), at startup or on a layout change.
It also compiled the fd of a no_keymap event, and compiled with no xkb
context when libxkbcommon found no keyboard data.

Ignore any format but XKB_V1, and do nothing without a context. When
compilation fails, keep the previous keymap and state and log a
warning, so hotkeys go on with the last layout that loaded.

Fixes #2975.
@rdoupe
rdoupe force-pushed the fix/bug-a-keymap-that-xkbcommon-can-286 branch from 2d372fd to 4748929 Compare October 30, 2026 03:21
@rdoupe
rdoupe marked this pull request as ready for review October 30, 2026 04:14
@rdoupe
rdoupe requested a review from zackees as a code owner October 30, 2026 04:14

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed. The handler fix matches what the RED test needs, and PR CI is green (40/40 checks).

Correctness:

  • format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 || !plat->xkb_context returns early and closes the fd, so the no_keymap case never compiles a keymap and the no-context case (the #2437 situation) no longer reaches xkb_keymap_new_from_string(NULL, ...).
  • !xkb_keymap now returns before dropping plat->xkb_keymap / plat->xkb_state, which is the crash: xkb_state_new(NULL) on the old pointers. Keeping the previous keymap and state is the right behaviour, and the warning names the path.
  • rebuild_keymap_data() is only reached with a compiled keymap, so key_to_sym / obs_to_key are not cleared for a rejected layout.
  • Test covers both orderings (broken after good, broken first then good) and asserts pointer identity, not just non-NULL, so it would catch a re-compile of the old text.

Nits, non-blocking:

  • no_xkb_context manufactures the missing context by unref'ing it, which is fine here, but it does not exercise xkb_context_new() actually failing; the comment says so and the case still pins the handler's guard, so I am not asking for a change.
  • The features-ledger line is accurate and dated.

Queueing it now.

@zackees
zackees added this pull request to the merge queue Oct 30, 2026
Merged via the queue into master with commit 055f9fe Oct 30, 2026
40 checks passed
@zackees
zackees deleted the fix/bug-a-keymap-that-xkbcommon-can-286 branch October 30, 2026 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs: a keymap xkbcommon can't compile crashes OBS on Wayland

2 participants