Skip to content

test_bytes: test_free_threading_bytearray() allocates 1 GiB on free threaded build #148605

@vstinner

Description

@vstinner

Last February, the commit a05433f added FreeThreadingTest to test_bytes. The irepeat() test calls a *= 2 10 times on a 4 MiB bytearray:

        def irepeat(b, a):  # MODIFIES!
            b.wait()
            a *= 2

I modified irepeat() to log the new size in bytes:

8388608
16777216
33554432
67108864
134217728
268435456
536870912
0
0
1073741824

The two 0 lines is because the clear() thread can also be called in the meanwhile: it calls bytearray.clear().

So after 8 irepeat() calls, the final size is 1 GiB (1073741824 bytes). That's a lot of memory!

The test logs a MemoryError if the system doesn't have 1 GiB of memory available:

test_free_threading_bytearray (test.test_bytes.FreeThreadingTest.test_free_threading_bytearray) ...

Warning -- Uncaught thread exception: MemoryErrorWarning -- Uncaught thread exception: MemoryError

Exception in thread Exception in thread Thread-212 (irepeat):
Thread-219 (irepeat):
Traceback (most recent call last):
Traceback (most recent call last):
  File "/builddir/cpython/Lib/threading.py", line 1075, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/builddir/cpython/Lib/threading.py", line 1017, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/builddir/cpython/Lib/test/test_bytes.py", line 2705, in irepeat
    a *= 2
MemoryError
  File "/builddir/cpython/Lib/threading.py", line 1075, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/builddir/cpython/Lib/threading.py", line 1017, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/builddir/cpython/Lib/test/test_bytes.py", line 2705, in irepeat
    a *= 2
MemoryError

ok

In that case, test_bytes is marked as "env_changed".

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions