Skip to content

ASan: SEGV in X11 hotkey thread (keycode_pressed) on shutdown — NULL xcb_query_keymap reply #126

Closed

Description

@zackees

Found by the ASan launch pass (meta issue linked as parent). Upstream OBS code, unchanged in this fork.

Crash

==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000000e (T6, hotkey thread)
    #0 keycode_pressed            libobs/obs-nix-x11.c:1048
    #1 key_pressed                libobs/obs-nix-x11.c:1068
    #2 obs_nix_x11_hotkeys_platform_is_pressed libobs/obs-nix-x11.c:1087
    #3 is_pressed                 libobs/obs-hotkey.c:1008
    #4 query_hotkeys              libobs/obs-hotkey.c:1142
    #5 obs_hotkey_thread          libobs/obs-hotkey.c:1176

Cause

key_pressed only checks error after xcb_query_keymap_reply(). When the X connection is closed or broken, xcb returns a NULL reply with no error, and keycode_pressed() reads reply->keys[...] from NULL. The fault address 0xe is offsetof(keys) + code/8.

Repro

Built with -fsanitize=address,undefined, run under Xvfb, send SIGINT after ~45 s (normal shutdown). The crash hit after ==== Shutting down ====: the hotkey thread was still polling while the X connection was being torn down. Disconnecting the X server while OBS runs triggers it too.

Harness: nix-shell rust/tools/sanitize/shell.nix --run 'rust/tools/sanitize/sanitize.sh asan' (in the sanitize branch; not yet merged).

Fix

Treat a NULL reply as "not pressed" (if (!reply) { free(error); return false; }), and stop the hotkey thread before the frontend's X display goes away. RED test first per testing-policy.md.

Activity

  1. added
    bug
    on Oct 10, 2026
  2. zackees commented on Oct 10, 2026

    @zackees
    MemberAuthor

    Valgrind reproduces this independently, this time during startup rather than shutdown. The hotkey thread crashed while the main thread was still in Initializing OpenGL..., so it isn't only a teardown-ordering problem: any time the X connection returns a NULL keymap reply, the thread dies.

    Process terminating with default action of signal 11 (SIGSEGV)
     Access not within mapped region at address 0xC
       at keycode_pressed (obs-nix-x11.c:1048)
       by key_pressed (obs-nix-x11.c:1068)
       by obs_nix_x11_hotkeys_platform_is_pressed (obs-nix-x11.c:1087)
       by obs_hotkeys_platform_is_pressed (obs-nix.c:407)
       by is_pressed (obs-hotkey.c:1008)
       by query_hotkeys (obs-hotkey.c:1144)
       by obs_hotkey_thread (obs-hotkey.c:1176)
    

    Debug build, valgrind --leak-check=full --track-origins=yes, Xvfb, QT_ENABLE_REGEXP_JIT=0. Apart from this, valgrind found no invalid reads, invalid writes or uninitialized-value errors in OBS code during the launch, and all its unit tests were clean.

  3. zackees commented on Oct 11, 2026

    @zackees
    MemberAuthor

    The CI run of the sanitizer workflow (https://github.com/obs-rust/obs-studio/actions/runs/38097738155, ubuntu-26.04) hit this in all three passes:

    • ASan+UBSan: libobs/obs-nix-x11.c:1048:21: runtime error: member access within null pointer of type 'struct xcb_query_keymap_reply_t', after ==== Shutting down ====.
    • TSan: ERROR: ThreadSanitizer: SEGV on unknown address 0x00000000000e at keycode_pressed obs-nix-x11.c:1048, after shutdown began.
    • valgrind: SIGSEGV at the same frame.

    Each time the process then hung and was SIGKILLed. Fixing this unblocks leak checking (LSan and valgrind's leak report) for the launch.

  4. zackees commented on Oct 11, 2026

    @zackees
    MemberAuthor

    Claiming. Plan: move the keymap bit test into a tiny xcb-only helper, RED cmocka test with a NULL reply, then GREEN treats NULL as not pressed.

  5. added
    claimed
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclaimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions