Skip to content

llama: don't let exceptions escape llama_encode/llama_decode - #27184

Closed
bogdanr wants to merge 1 commit into
ggml-org:masterfrom
bogdanr:llama-entry-points-no-throw
Closed

bogdanr wants to merge 1 commit into
ggml-org:masterfrom
bogdanr:llama-entry-points-no-throw

Conversation

@bogdanr

@bogdanr bogdanr commented Aug 16, 2026

Copy link
Copy Markdown

llama_encode and llama_decode are extern "C" and call straight into ctx->encode / ctx->decode with no handler. Anything thrown below them unwinds across a C boundary, which is undefined; in practice the process aborts and the caller never gets a chance to do anything about it.

This is not hypothetical - a Vulkan device loss does it today:

terminate called after throwing an instance of 'vk::DeviceLostError'
  what():  vk::Queue::submit: ErrorDeviceLost

I've sent a separate PR fixing that particular throw in the Vulkan backend (#27183). This one is the belt-and-braces half: no exception should cross these two entry points regardless of which backend threw it, or which future one starts to.

The neighbouring public entry points in llama-context.cpp already do exactly this - try / catch (const std::exception & err), log, return - so this just brings llama_encode and llama_decode in line with them. -4 fits the documented "< -1 - fatal error" range for llama_decode and "< 0 - error" for llama_encode, and doesn't collide with the existing -1 / -2 / -3.

Kept separate from the Vulkan PR on purpose: that one is a backend bug fix, this one has a wider blast radius and you may want to weigh them apart.

Both are extern "C" and call straight into ctx->encode/ctx->decode with
no handler, so anything thrown below them unwinds across a C boundary
and the process aborts. A Vulkan device loss does this today, but any
backend can throw.

Wrap both in try/catch like the other public entry points in this file,
log, and return -4.
@bogdanr
bogdanr requested a review from ggerganov as a code owner August 16, 2026 14:22
@ggml-gh-bot

ggml-gh-bot Bot commented Aug 16, 2026

Copy link
Copy Markdown

Hi @bogdanr, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • Multiple open PRs from a new contributor: We limit new contributors (those without a previously merged PR) to 1 open PR at a time. You currently have 2 open PRs.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Aug 16, 2026
@github-actions
github-actions Bot marked this pull request as draft August 16, 2026 14:27
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Aug 16, 2026
@bogdanr

bogdanr commented Aug 16, 2026

Copy link
Copy Markdown
Author

Closing this for now - I opened two at once and the guidance is one PR at a time for a first contribution. The Vulkan fix in #27183 is the one that matters; I will reopen this after it lands.

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.

1 participant