Skip to content

HACL* update() errors are handled without a thread state #159786

Open

Description

@picnixz

Bug report

gh-135561 made _hacl_convert_errno() attach a thread state with PyGILState_Ensure() before setting an exception, because it may be called while the GIL is released. Since gh-135239, _hacl_hmac_state_update_once() calls _hacl_convert_errno() from inside HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED (in _hmac.new()) and HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED (in _hmac.HMAC.update()).

The following issues arise on DEBUG builds:

  1. If a chunk fails in the chunking loop of _hacl_hmac_state_update(), the loop then runs assert(PyErr_Occurred()). By that point, _hacl_convert_errno() has already called PyGILState_Release(), so PyErr_Occurred() runs without an attached thread state. The loop only runs for buffers larger than UINT32_MAX bytes.
  2. In _hmac.HMAC.update(), PyGILState_Ensure() runs while self->mutex is held, so a thread state is attached while the object's lock is held.

In practice, you need a HACL* update() failure such as MaximumLengthExceeded, and release builds ignore the HACL* exit code of update() anyway. However, since I wrote the module with DEBUG builds in mind just in case, I still want to cover that case.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. self-assigned this
    on Oct 19, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    3.15bugs and security fixes
    3.16new features, bugs and security fixes
    on Oct 19, 2026
  3. added 3 commits that reference this issue on Oct 19, 2026
  4. added a commit that references this issue on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.15bugs and security fixes3.16new features, bugs and security fixesextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions