Skip to content

Crash when using typing and daemon threads #116514

Open
@nineteendo

Description

@nineteendo

Crash report

What happened?

When I run this code on Python 3.11 it runs fine, but if I import typing_minimal (or typing) in reader.py I get this error:

Fatal Python error: _enter_buffered_busy: could not acquire lock for <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly due to daemon threads
Python runtime state: finalizing (tstate=0x00007fff13ab6960)

Current thread 0x00003944 (most recent call first):
  <no Python frame>

main.py:

import reader
from typing_minimal import Generic, TypeVar

VALUE = TypeVar("VALUE")


class BaseClass(Generic[VALUE]):
    def __init__(self):
        pass


class SubClass(BaseClass[VALUE]):
    pass


reader.thread.start()

reader.py:

from sys import stdin
from threading import Thread

# BUG: I get an error when I assign this:
# typing_minimal = __import__("typing_minimal")


thread = Thread(target=lambda: stdin.buffer.read(), daemon=True)

typing_minimal.py:

from functools import lru_cache, wraps


def _tp_cache(func):
    cached = lru_cache()(func)

    @wraps(func)
    def inner(*args, **kwds):
        return cached(*args, **kwds)

    return inner


class TypeVar:
    def __init__(self, _):
        pass


class _GenericAlias:
    def __init__(self, origin):
        self.origin = origin

    def __eq__(self, other):
        if isinstance(other, _GenericAlias):
            return self.origin is other.origin

        return NotImplemented

    def __hash__(self):
        return hash(self.origin)

    def __mro_entries__(self, _):
        return (self.origin,)


class Generic:
    @_tp_cache
    def __class_getitem__(cls, *_):
        return _GenericAlias(cls)

I don't understand how this is even possible.

CPython versions tested on:

3.11

Operating systems tested on:

macOS, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-typingtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions