Skip to content

pdb: cls._pluginmanager, config etc might be None #5228

@blueyed

Description

@blueyed

This can happen when debugging threads after pytest finished already (and the debugging plugin was unconfigured).

A failing test:

def test_pdb_in_thread_after_exit(testdir):
    """It should be imported in pytest_configure or later only."""
    p1 = testdir.makepyfile(
        """
        import threading


        def test():
            evt = threading.Event()
            evt2 = threading.Event()

            def target():
                evt.set()
                evt2.wait()
                print("target_set_trace")
                __import__('pdb').set_trace()
                print("target_end")

            thread = threading.Thread(target=target)
            thread.start()

            evt.wait()
            evt2.set()
        """,
    )
    child = testdir.spawn_pytest(str(p1) + " -s")
    child.expect("target_set_trace")
    rest = child.read().decode("utf8")
    assert "Exception in thread" not in rest
    assert child.exitstatus == 0

I think it should not pop the last set of attributes, but hard to say when the last time is maybe?!
IIRC it does not use pytest_unconfigure intentionally.

The test causes this:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/tmp/pytest-of-user/pytest-61/test_pdb_in_thread_after_exit0/test_pdb_in_thread_after_exit.py", line 12, in target
    __import__('pdb').set_trace()
  File "…/Vcs/pytest/src/_pytest/debugging.py", line 221, in set_trace
    _pdb = cls._init_pdb(*args, **kwargs)
  File "…/Vcs/pytest/src/_pytest/debugging.py", line 212, in _init_pdb
    cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
AttributeError: 'NoneType' object has no attribute 'hook'

I've seen this initially:

(Pdb++[sync-to-async-_0]) c
Exception in worker
Traceback (most recent call last):
  File "/usr/lib/python3.7/concurrent/futures/thread.py", line 79, in _worker
    if work_item is not None:
  File "/usr/lib/python3.7/concurrent/futures/thread.py", line 79, in _worker
    if work_item is not None:
  File "/usr/lib/python3.7/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python3.7/bdb.py", line 112, in dispatch_line
    self.user_line(frame)
  File "/usr/lib/python3.7/pdb.py", line 261, in user_line
    self.interaction(frame, None)
  File "…/project/.venv/lib/python3.7/site-packages/pdb.py", line 554, in interaction
    ret = self._interaction(frame, traceback)
  File "…/project/.venv/lib/python3.7/site-packages/pdb.py", line 599, in _interaction
    self._cmdloop()
  File "/usr/lib/python3.7/pdb.py", line 321, in _cmdloop
    self.cmdloop()
  File "…/project/.venv/lib/python3.7/site-packages/pdb.py", line 667, in cmdloop
    return self._inner_cmdloop(intro)
  File "…/project/.venv/lib/python3.7/site-packages/pdb.py", line 701, in _inner_cmdloop
    ret = super(Pdb, self).cmdloop(intro="\n".join(wrapped_intro))
  File "/usr/lib/python3.7/cmd.py", line 138, in cmdloop
    stop = self.onecmd(line)
  File "/usr/lib/python3.7/pdb.py", line 418, in onecmd
    return cmd.Cmd.onecmd(self, line)
  File "/usr/lib/python3.7/cmd.py", line 217, in onecmd
    return func(arg)
  File "…/Vcs/pytest/src/_pytest/debugging.py", line 159, in do_continue
    tw = _pytest.config.create_terminal_writer(cls._config)
  File "…/Vcs/pytest/src/_pytest/config/__init__.py", line 1085, in create_terminal_writer
    if config.option.color == "yes":
AttributeError: 'NoneType' object has no attribute 'option'

JFI: #5244 worked around this, by keeping pytest alive - which I am moving to my fork to close stale PRs on the main repo (blueyed#17).

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: debuggingrelated to the debugging builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions