Repository navigation
Description
Found while fixing #2713 (WebSocketServer::Stop's unguarded session wait). Standalone defect, same file, not folded into that PR.
The defect
plugins/obs-websocket/src/websocketserver/WebSocketServer.cpp:346-355 (onClose):
std::unique_lock<std::mutex> lock(_sessionMutex);
SessionPtr session = _sessions[hdl]; // :347 — operator[] INSERTS a default SessionPtr
uint64_t eventSubscriptions = session->EventSubscriptions(); // :348 — null dereference_sessions is std::map<connection_hdl, SessionPtr> (WebSocketServer.h:99), so operator[] on a missing key default-constructs a null SessionPtr and inserts it, and the very next line calls a member on it. Every other read in the class that can miss uses at() in a try (onMessage, :356-360 in the same file).
How a missing key happens
onOpen() (:219-233) is the only insert path, and it can return after websocketpp has accepted the connection:
:223-227—GetConfig()returns null (module unloading, config pointer unavailable) →returnbefore_sessions[hdl] = ...at:231;- the
select_subprotocol/session->Set*calls between accept and insert are on the same path.
onClose() is still called for that connection by the IO thread, so the erase path reads a handle that was never inserted. Today that is a null shared_ptr dereference, i.e. undefined behaviour, on the shutdown path.
Second effect, even when the key exists
Because the insert is invisible, a close for a never-opened handle leaves a null entry in the map that only _sessions.erase(hdl) (the next line) removes — but only after the dereference has already happened. Any GetWebSocketSessions() iteration in between would also dereference null (:189-198).
Fix
Read without inserting and bail out of the bookkeeping the session never had:
auto it = _sessions.find(hdl);
if (it == _sessions.end())
return;
SessionPtr session = it->second;
_sessions.erase(it);and, when the entry is removed, still notify whatever waits for the drain (#2713's _sessionsDrained), otherwise a Stop() concurrent with this path could wait on the full 5 s bound.
Tests
- RED: a Tier 2 / OBS-level case that opens a connection while the config is unavailable and asserts unload completes with no null dereference (a build with
-fsanitize=addressfails today at:348). Cheap alternative if the config-injection path is hard to drive: a unit-level case onWebSocketServerthat callsonClose()with a handle that was never opened (needs the private accessor route the plugin tests already use forPLUGIN_TESTS). - Not claimed: nothing has been run for this report beyond reading the code paths; the file:line references are against
obs-websocket5de9d09(the pointer inmastertoday).
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Oct 28, 2026
/ready
Triaged against the definition of Ready (WORKFLOW.md §Definition of Ready):
onClose()for a handle that was never inserted dereferences a default-constructed nullSessionPtr; an ASan build fails atWebSocketServer.cpp:348.obs-rust/obs-websocket, plus the_sessionsDrainednotification that bug: obs-websocket: WebSocketServer::Stop reads _sessions without the session mutex, and waits on it unbounded #2713 introduced; no public API, no protocol change.obs-websocket5de9d09and again on3253206(the#22merge, which kept_sessions[hdl]in place).zackees commentedon Oct 28, 2026
Claiming: fix
onClose()inserting and dereferencing a null session (_sessions[hdl])Plan, two PRs (the submodule owns the fix; this repo owns the pointer bump):
obs-rust/obs-websocket— replace_sessions[hdl]inonClose()with afind()that returns early when the handle was never inserted, keeping the_sessionsDrainednotification for the removed entry (bug: obs-websocket: WebSocketServer::Stop reads _sessions without the session mutex, and waits on it unbounded #2713'sStop()bound depends on it). Branchfix/onclose-null-session.obs-rust/obs-studio— submodule pointer bump once that merges (carrying libobs: Preserve extensions and UTF-8 boundaries in generated filenames #22 and this fix together), with thedocs/features.mdline in the same PR.First PR expected within the hour.
websocketserver: Do not insert a null session in onClose (#23)
zackees commentedon Oct 28, 2026
Status: the submodule fix is merged — obs-rust/obs-websocket#23 (
fix/onclose-null-session, squash777071b5db3dc3cf0fd62f3cea5a2a3ac7992af2, merged 02:20 UTC).What it does, matching the Fix section here:
onClose()looks the handle up with_sessions.find(hdl)and returns early when it is absent instead of_sessions[hdl]default-constructing (and inserting) a nullSessionPtrthat the next line dereferences.Stop()wait is unchanged.Verification, honestly:
clang-format --dry-run --Werrorclean;Code QualityandLint Markdowngreen. The repo has no compile lane (its workflows are lint/docs/crowdin only) and no test harness, so the RED from this issue (ASan, a close for a never-opened handle) is not in that PR — the compile gate is theobs-studiopointer bump.Pointer bump:
obs-rust/obs-studio#2722already bumps to3253206; I asked its author to move it to777071bso one bump carries both fixes. This issue stays open until that PR lands, then it closes with the bump.zackees commentedon Oct 28, 2026
PR #2723 points
plugins/obs-websocketat777071b(submodule master, obs-rust/obs-websocket#23) — the parent-repo half of this fix.#2722landed the pointer at3253206(libobs: Preserve extensions and UTF-8 boundaries in generated filenames #22, the bounded session drain); the submodule's master then moved to777071bwith libobs/util: Keep UTF-8 valid when truncating formatted filenames #23, so this is the one-commit follow-up bump.uv run --no-project -m unittest ci/test_websocket_submodule.pypasses at the new pointer (5 tests, 1 shallow-checkout skip), so777071bcontains both libobs: Preserve extensions and UTF-8 boundaries in generated filenames #22 and libobs/util: Keep UTF-8 valid when truncating formatted filenames #23.ENABLE_WEBSOCKET=OFF), so the release-gate app build is the compile evidence, as for fix(obs-websocket): Bump the submodule to the bounded session drain #2722.When #2723 merges, this issue's fix is in the parent repo and it can close.
zackees commentedon Oct 28, 2026
Closed: the fix is on
master.777071b5db3dc3cf0fd62f3cea5a2a3ac7992af2) changedonClose()to look the handle up with_sessions.find(hdl)and return early — after unlocking — when the session was never inserted, instead of_sessions[hdl]default-constructing a nullSessionPtrthat the next line dereferenced.plugins/obs-websocket3253206->777071b, which also carries libobs: Preserve extensions and UTF-8 boundaries in generated filenames #22's bounded drain unchanged.git ls-tree origin/master plugins/obs-websocketis777071b, andcompare/777071b...masteron obs-rust/obs-websocket is identical, so no further bump is pending.uv run --no-project -m unittest ci/test_websocket_submodule.pypasses at the new pointer (the bounded-parser, bounded-drain andSessionDrainShapeTestpins), and the features ledger has the matching line indocs/features.md.No compiled lane covers this submodule (Tier 2 configures
ENABLE_WEBSOCKET=OFF), so the release-gate app build is the compile evidence.fix(obs-websocket): Point the submodule at the onClose null-session f…