Skip to content

[OpenAL] Fix sourceSeek crash on stopped sources - #27426

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:openal_fix
Open

[OpenAL] Fix sourceSeek crash on stopped sources#27426
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:openal_fix

Conversation

@sbc100

@sbc100 sbc100 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Fix a crash in sourceSeek when called on a stopped source by resetting bufsProcessed = 0 before evaluating buffer durations.

This replaces #26770. Unlike #26770, which added a top-level check on src.bufQueue[0], this change handles zero/dummy buffers and empty queues safely anywhere in bufQueue via for .. of iteration and audioBuf?.duration ?? 0.0. It also adds assertions to test_openal_error.c to verify state correctness after seeking.

See: #26770

@sbc100
sbc100 requested a review from kripken July 28, 2026 04:41
@sbc100 sbc100 changed the title [openal] Fix sourceSeek crash on stopped sources [OpenAL] Fix sourceSeek crash on stopped sources Jul 28, 2026
Comment thread src/lib/libopenal.js
AL.setSourceState(src, {{{ cDefs.AL_INITIAL }}});
}

if (src.bufQueue[src.bufsProcessed].audioBuf !== null) {

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.

Why is this If not needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The removal of the outer if (src.bufQueue[src.bufsProcessed].audioBuf !== null) here is an important part of the fix.

When an OpenAL source is stopped (or finishes playback), libopenal.js sets src.bufsProcessed = src.bufQueue.length. This would make the outer if condition crash with
TypeError: Cannot read properties of undefined (reading 'audioBuf') crash when attempting to seek on a stopped source.

The spirit of the check is still here but in the format the ? notation in buf.audioBuf?.duration ?? 0.0.

It also important that the src.bufsProcessed get before the loop and out side of the if condition here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What this code is basically doing is resetting the seek potions by resetting both bufsProcessed (which buffer are currently playing) and bufOffset (how far into the currnent buffer are we).

So this loops is first resetting the bufsProcessed and then incrementing until we have accumulated enough "duration" to satisfy the desired seek offset.

Comment thread src/lib/libopenal.js Outdated
Fix a crash in `sourceSeek` when called on a stopped source by resetting
`bufsProcessed = 0` before evaluating buffer durations.

This replaces emscripten-core#26770. Unlike emscripten-core#26770, which added a top-level check on
`src.bufQueue[0]`, this change handles zero/dummy buffers and empty
queues safely anywhere in `bufQueue` via `for .. of` iteration and
`audioBuf?.duration ?? 0.0`. It also adds assertions to
`test_openal_error.c` to verify state correctness after seeking.

See: emscripten-core#26770
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants