Skip to content

Add free threaded Python 3.14t support to build workflow - #316

Closed
cclauss wants to merge 4 commits into
python-lz4:masterfrom
cclauss:patch-1
Closed

cclauss wants to merge 4 commits into
python-lz4:masterfrom
cclauss:patch-1

Conversation

@cclauss

@cclauss cclauss commented Oct 20, 2025 •

Copy link
Copy Markdown
Contributor

% uvx --with=lz4 python3.14t -c"import lz4"

<frozen importlib._bootstrap>:491: RuntimeWarning: The global interpreter lock (GIL) has been enabled
    to load module 'lz4._version', which has not declared that it can run safely without the GIL.
    To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0
    or -Xgil=0.

These are expected to fail. Help needed to fix the failing free threaded builds.
https://py-free-threading.github.io/porting

Related:

These are expected to fail.  ___Help needed___ to fix the failing free threaded builds.
@ogrisel

ogrisel commented Oct 22, 2025

Copy link
Copy Markdown

@cclauss can you explain why you expect those tests to fail?

  ____________________ test_block_decompress_mem_usage[data0] ____________________
  
  data = b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  
      @pytest.mark.thread_unsafe
      def test_block_decompress_mem_usage(data):
          tracemalloc = pytest.importorskip('tracemalloc')
      
          tracemalloc.start()
      
          compressed = lz4.block.compress(data)
          prev_snapshot = None
      
          for i in range(1000):
              decompressed = lz4.block.decompress(compressed)  # noqa: F841
      
              if i % 100 == 0:
                  gc.collect()
                  snapshot = tracemalloc.take_snapshot()
      
                  if prev_snapshot:
                      stats = snapshot.compare_to(prev_snapshot, 'lineno')
  >                   assert stats[0].size_diff < (1024 * 8)
  E                   AssertionError: assert 18184 < (1024 * 8)
  E                    +  where 18184 = <StatisticDiff traceback=<Traceback (<Frame filename='/project/.tox/py/lib/python3.14t/site-packages/coverage/sysmon.py' lineno=348>,)> size=18184 (+18184) count=1 (+1)>.size_diff
  
  compressed = b'\x00\x00\x10\x00\x1fa\x01\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf...\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf6Paaaaa'
  data       = b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  decompressed = b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  i          = 200
  prev_snapshot = <tracemalloc.Snapshot object at 0x33e83dec450>
  snapshot   = <tracemalloc.Snapshot object at 0x33e83decf90>
  stats      = [<StatisticDiff traceback=<Traceback (<Frame filename='/project/.tox/py/lib/python3.14t/site-packages/coverage/sysmon..../_internal/cpython-3.14.0rc3-nogil/lib/python3.14t/tracemalloc.py' lineno=498>,)> size=576 (+576) count=12 (+12)>, ...]
  tracemalloc = <module 'tracemalloc' from '/opt/_internal/cpython-3.14.0rc3-nogil/lib/python3.14t/tracemalloc.py'>
  
  tests/block/test_block_3.py:39: AssertionError

This test is the only failure (both on ubuntu and windows). I fail to see how free-threading can be expected to impact the memory usage of sequential operations.

This test is decorated with @pytest.mark.thread_unsafe so pytest-run-parallel should not run several instances of this particular in concurrent threads. But maybe I miss something about the side effects of running a full test suite with pytest-run-parallel enabled. Maybe @ngoldbaum has an idea.

@ogrisel

ogrisel commented Oct 22, 2025

Copy link
Copy Markdown

Maybe we could just skip this test if the pytest-run-parallel plugin is active.

@cclauss

cclauss commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor Author

We should not be surprised when functions marked with @pytest.mark.thread_unsafe fail in a free-threaded environment.

def free_threading() -> bool:
    return sys.version_info >= (3, 13) and not sys._is_gil_enabled()


@pytest.mark.xfail(free_threading(), reason="May fail without a Global Interpreter Lock")

https://pypi.org/project/pytest-run-parallel

Added a conditional xfail marker to test_block_decompress_mem_usage to handle cases without a Global Interpreter Lock.
@ogrisel

ogrisel commented Oct 22, 2025

Copy link
Copy Markdown

@pytest.mark.thread_unsafe is used to tell the pytest-run-parallel plugin to run that particular test only once, and therefore sequentially. Therefore I do not expect it to fail. However, it might be the case that the threadpool of the pytest-run-parallel plugin still has some delayed side effects on memory usage that cause this test to fail?

@ogrisel

ogrisel commented Oct 22, 2025

Copy link
Copy Markdown

Note that the pytest-run-parallel plugin is meant to stress test a library to look for free-threading related bugs, but nothing would prevent it to stress test a library when the GIL is enabled.

@ngoldbaum

Copy link
Copy Markdown
Contributor

I think the failing test probably indicates that the memory usage pattern is different on 3.14t and the test failure is probably "real" in the sense that the upper bound is being violated for whatever reason. There were some pretty substantial changes to the free-threaded GC in 3.14t, maybe we're hitting a corner case? I'll see if Neil wants to take a closer look at this...

@cclauss just because a test is marked as thread-unsafe doesn't mean that the test is expected to fail on the free-threaded build. Only that it is an inherently thread-unsafe test and should be run on one core when running under pytest-run-parallel. You can also tell pytest-run-parallel to skip thread-unsafe tests, but that's not the default.

@ogrisel one thing that might help future readers to interpret the thread-unsafe markers is to also specify a reason. In this case, it would have something like:

@pytest.mark.thread_unsafe(reason="Test uses tracemalloc to estimate global memory usage")

The reason will also get propagated to the verbose output, allowing people to see why an unsafe test is skipped.

We should probably make pytest-run-parallel warn when people use the thread-unsafe marker without a reason. I tried to figure out how to do that at one point but got stuck and haven't come back to it.

@cclauss cclauss changed the title DRAFT: Add free threaded Python 3.14t support to build workflow Add free threaded Python 3.14t support to build workflow Oct 22, 2025

@ogrisel ogrisel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as it is. The test failure on ubuntu and windows with free-threading might be revealing a real problem w.r.t. memory usage expectations, but it's indeed hard to tell, as @ngoldbaum explained above. I could not trigger the problem locally on macOS to investigate myself.

However, the core functionality of lz4 seems to work as expected on cp314t, so I would not condition the merge of this PR to the results of the investigation of the higher than expected memory usage reported by tracemalloc in this test.

Having cp314t wheels for python-lz4 would make it easier for downstream projects to get their own CI running on cp314t and subsequently unlock free-threading adoption throughout the Python ecosystem.

@cclauss

cclauss commented Oct 25, 2025 •

Copy link
Copy Markdown
Contributor Author

@jonathanunderwood @smorin @ngoldbaum

Can I please get your reviews on this PR and #317 so that we can get a new release on PyPI that supports Python 3.14?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants