Skip to content

make threads which stuck in blocking call interruptable #137958

@FeelUsM

Description

@FeelUsM

Feature or enhancement

Proposal:

If we need to kill one thread from another we can use standard python trick:

def raise_in_thread(tid, exctype):
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
        ctypes.c_long(tid),
        ctypes.py_object(exctype)
    )
    if res == 0:
        raise ValueError("Invalid thread ID")
    elif res > 1:
        ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), None)
        raise SystemError("PyThreadState_SetAsyncExc failed")

but if thread stuck in blocking call it raise exception only after blocking call returs. So we need interrupt blocking call inside thread.

I created safe hack solution that exploits signals but it works only with custom input functions. To make the solution universal (compatible with CPython’s read() function) , for example, we need to force CPython’s PyErr_CheckSignals() somehow to return True.

Is there standard methods to do that?

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/how-to-kill-thread-in-python-which-stuck-in-blocking-call/102431

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions