Skip to content

restore_original_limits() restores all libraries, not just those that were set #308

Open

Description

@itamarst

Splitting off from discussion in #300, though it might end up getting fixed there. Original report from @cakedev0, lightly edited:


Claude had this finding:

The second one is similar to the bug of #299 in a sibling code path: restore_original_limits also restores the libraries that this limiter never set. A thread that only limits OpenMP (or passes limits=None) still holds an OpenBLAS snapshot, and on exit it takes control to restore it:

from threading import Event, Thread

import numpy  # noqa: F401
from threadpoolctl import ThreadpoolController, threadpool_limits

lib = ThreadpoolController().select(internal_api="openblas").lib_controllers[0]
print(lib.get_num_threads())  # 14
a_in, b_in, a_out = Event(), Event(), Event()


def A():
    with threadpool_limits(2):
        a_in.set()
        b_in.wait()
    a_out.set()


def B():
    a_in.wait()
    with threadpool_limits(1, user_api="openmp"):  # or threadpool_limits()
        b_in.set()
        a_out.wait()


threads = [Thread(target=A), Thread(target=B)]
for t in threads:
    t.start()
for t in threads:
    t.join()
print(lib.get_num_threads())  # 2

Keeping in _original_info only the libraries that _set_threadpool_limits did set (dropping the entry on the continue path and when num_threads is None) should cover it. WDYT?

Activity

  1. itamarst commented on Nov 17, 2026

    @itamarst
    ContributorAuthor

    So my first thought was that this is a real bug.

    My second thought is that the current status quo is somewhat ambiguous. restore_original_limits() could mean one of two things:

    1. Restore to the status quo upon the creation of the object, the current code.
    2. Or, restore those limits that were explicitly set when the object was created, the proposed change.

    Consider for example the case where one does threadpool_limit(limits=None). This doesn't set/limit anything, yet clearly the expectation (and how it's used) is that restore_original_limits() will restore everything. So even if we go with option 2 above, limits=None needs to be a special case.

  2. itamarst commented on Nov 18, 2026

    @itamarst
    ContributorAuthor

    After some more thought I think the original bug report is reasonable, and the limits=None can be explained not so much as a special case but as "you're setting everything to the current value", so restoring everything makes sense. I'll do this in its own PR I think.

  3. itamarst commented on Nov 18, 2026

    @itamarst
    ContributorAuthor

    I guess that does raise the question of what get_original_num_threads() should do—should it return all limits, or just the ones that were modified?

  4. itamarst commented on Nov 18, 2026

    @itamarst
    ContributorAuthor

    I went with get_original_num_threads() should only return results for APIs that were modified.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions