Skip to content

Intermittent IndexError in pytest-cov during Apache Beam Unit Tests: INTERNALERROR> IndexError: bytearray index out of range #607

Open
@momonala

Description

@momonala

Summary

I get this trackback when running python pytest unit tests using the Apache Beam testing framework.

This is an intermittent issue. It often resolves if I rerun the test suite 1-3 times.

Based on the trackeback, it is related to the pytest_cov/plugin.py and related modules: The traceback shows that the error originates in the pytest_cov plugin, which is responsible for coverage reporting during the test run. This plugin interacts with the coverage library to collect and report coverage data. The error occurs during the flushing of coverage data, when coverage data collected during the test execution is being processed and saved.

All tests in the pytest output will be marked as passed. This issue occurs regardless of passing or failing tests.

I only get this issue on my test suite with Apache Beam. It does not happen on a different test suite with the same pytest/coverage versions installed, but no Beam tests/Beam package is installed. I am using a DirectRunner for Beam, and tests are running sequentially (i.e. no parallelism).

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/_pytest/main.py", line 270, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/_pytest/main.py", line 324, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_hooks.py", line 433, in __call__
INTERNALERROR>     return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_manager.py", line 112, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_callers.py", line 133, in _multicall
INTERNALERROR>     teardown[0].send(outcome)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pytest_cov/plugin.py", line 298, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pytest_cov/engine.py", line 44, in ensure_topdir_wrapper
INTERNALERROR>     return meth(self, *args, **kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pytest_cov/engine.py", line 250, in finish
INTERNALERROR>     self.cov.save()
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/control.py", line 780, in save
INTERNALERROR>     data = self.get_data()
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/control.py", line 860, in get_data
INTERNALERROR>     if self._collector.flush_data():
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/collector.py", line 499, in flush_data
INTERNALERROR>     self.covdata.add_lines(self.mapped_file_dict(line_data))
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/sqldata.py", line [125](https://github.com/GrandperspectiveGmbH/scanfeld-pylib/actions/runs/5924117264/job/16061055736#step:10:126), in _wrapped
INTERNALERROR>     return method(self, *args, **kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/sqldata.py", line 499, in add_lines
INTERNALERROR>     linemap = nums_to_numbits(linenos)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/coverage/numbits.py", line 42, in nums_to_numbits
INTERNALERROR>     b[num//8] |= 1 << num % 8
INTERNALERROR> IndexError: bytearray index out of range

Expected vs actual result

The unit tests should run successfully without encountering the IndexError mentioned above. The coverage reporting process should handle data flushing reliably, regardless of the code paths being executed during the tests.

Reproducer

Versions

Python: 3.10
pytest-cov: 4.1.0
coverage: 7.3.0
apache_beam: 2.46.0
Test environment: Docker container running on top of Ubuntu (Github Actions CI machines)
Test command:

docker compose ... run \
                         --name  container_name \
                         container_name \
                         pytest  \
                         --tb=native \
                         -s \
                         --cache-clear \
                         --no-cov-on-fail \
                         --cov-config=.coveragerc \
                         --cov=pipeline \
                         --cov-report html:reports/cov-html/pipeline-unit \
                         --cov-report xml:reports/cov/pipeline-unit.xml \
                         --junitxml=reports/junit/pipeline-unit.xml

Config

.coveragerc

# .coveragerc to control coverage.py
[run]
branch = False
relative_files = True

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
    # Have to re-enable the standard pragma
    pragma: no cover

    # Don't complain about missing debug-only code:
    def __repr__
    if self\.debug

    # Don't complain if tests don't hit defensive assertion code:
    raise AssertionError
    raise NotImplementedError

    # Don't complain if non-runnable code isn't run:
    if 0:
    if __name__ == .__main__.:

ignore_errors = True

omit =
    python/*
    *test_*
    *i_test_*
    *__init__.py

Code

Link to your repository, gist, pastebin or just paste raw code that illustrates the issue.

If you paste raw code make sure you quote it, eg:

def foobar():
    pass

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