Skip to content

[Python] Dataset.to_batches() / ParquetFileFragment.to_batches() hang forever #45214

Closed as duplicate

Description

@lhoestq

In the datasets library we are using ParquetFileFragment.to_batches() to stream batches of data while applying filters file-per-file. We create fragments from file-like objects (because files can be local or remote).

However @AlexKoff88 reported that for some datasets like phiyodr/InpaintCOCO it causes the code to hang at huggingface/datasets#7357.

I managed to make a reproducible example:

wget https://huggingface.co/datasets/phiyodr/InpaintCOCO/resolve/c56e31947190173d2d6373c4833b0a9889ff6eee/data/test-00000-of-00003.parquet

file info here

import pyarrow.dataset as ds

file = "test-00000-of-00003.parquet"
with open(file, "rb") as f:
    parquet_fragment = ds.ParquetFileFormat().make_fragment(f)
    for record_batch in parquet_fragment.to_batches():
        print(len(record_batch))  # 100
        break  # hangs forever

Environment:

  • python 3.12.2
  • pyarrow 18.1.0
  • macbook pro m2

Most of the time the code hangs, and in some (rare) random cases it is able to terminate.

The issue appears when running the python script and also in a python shell / ipython in exit().

The issue also appears for example for eltorio/ROCOv2-radiology and bigcode/the-stack

In the original issue in datasets this message was also reported:

Fatal Python error: PyGILState_Release: thread state 0x7fa1f409ade0 must be current when releasing
Python runtime state: finalizing (tstate=0x0000000000ad2958)

Thread 0x00007fa33d157740 (most recent call first):
  <no Python frame>

Component(s)

Parquet, Python

Activity

  1. changed the title [-][Python] ParquetFragment.to_batches() hangs forever[/-] [+][Python] ParquetFileFragment.to_batches() hangs forever[/+] on Jan 9, 2025
  2. lhoestq commented on Apr 24, 2025

    @lhoestq
    Author

    This also causes pyarrow.dataset.Dataset.to_batches() to make the python script hang at the end:

    import pyarrow.dataset as ds
    from huggingface_hub import HfFileSystem
    
    
    file = "hf://datasets/vevotx/Tahoe-100M/data/train-00000-of-03388.parquet"
    dataset = ds.dataset(file, format="parquet", filesystem=HfFileSystem())
    for record_batch in dataset.to_batches(batch_size=100):
        print(len(record_batch))  # 100
        break
    print("now hanging...")

    PS: if you run in ipython, call exit() to observe the hanging

  3. changed the title [-][Python] ParquetFileFragment.to_batches() hangs forever[/-] [+][Python] Dataset.to_batches() / ParquetFileFragment.to_batches() hang forever[/+] on Apr 24, 2025
  4. pitrou commented on Apr 28, 2025

    @pitrou
    Member

    Given that we're talking about a hang or crash when destroying a partially read dataset, this could be the same as #43604

    Unfortunately, my attempt at fixing it hit other issues: #43756

  5. lhoestq commented on Oct 15, 2025

    @lhoestq
    Author

    Has there be any update on this by any chance ? I'm also happy to try out ideas if you think there is a workaround (maybe wrapping the reader and wait for it to be ready for deletion or something like that ?)

  6. liningpan commented on Dec 27, 2025

    @liningpan

    I did some testing. It appears that this only happens when calling with filesystem=HfFileSystem(). The program exits normally when using a file on disk. Additionally, when using the HF filesystem, if I pass use_threads=False when calling to_batches, the program hung for a long time but eventually exited.

    I am really familiar with Cython and async C++ though.

  7. lhoestq commented on May 4, 2026

    @lhoestq
    Author

    Here is another reproducible example using pyarrow 24.0.0 that was shared in huggingface/huggingface_hub#4178:

    from datasets import load_dataset
    
    from datasets import __version__ as ds_version
    from huggingface_hub import __version__ as hf_version
    from pyarrow import __version__ as pa_version
    
    print(f"datasets: {ds_version}")
    print(f"huggingface_hub: {hf_version}")
    print(f"pyarrow: {pa_version}")
    
    ds = load_dataset("IRIIS-RESEARCH/Nepali-Text-Corpus", split="train", streaming=True)
    print(next(iter(ds)))

    which prints this and then hangs

    datasets: 4.8.5
    huggingface_hub: 1.13.0
    pyarrow: 24.0.0
    Resolving data files: 100%|████████████████████████████████████| 46/46 [00:00<00:00, 20887.52it/s]
    Resolving data files: 100%|████████████████████████████████████| 46/46 [00:00<00:00, 36191.71it/s]
    {'index': 5176754, 'Article': ' बिहीबार दिउँसो खोला [...] नकारी दिए ।', 'Source': 'nepalkhabar.com'}
    

    a workaround could be to del the fragment iterator instead of relying on gc, will try it out

  8. pitrou commented on May 5, 2026

    @pitrou
    Member

    Is it possible to get a C-level backtrace of where the hang happens?

  9. lhoestq commented on May 6, 2026

    @lhoestq
    Author

    here is the backtrace for the last example I shared (comment)

    (lldb) process attach --pid 19530
    Process 19530 stopped
    * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
        frame #0: 0x000000018ad0f3cc libsystem_kernel.dylib`__psynch_cvwait + 8
    libsystem_kernel.dylib`__psynch_cvwait:
    ->  0x18ad0f3cc <+8>:  b.lo   0x18ad0f3ec    ; <+40>
        0x18ad0f3d0 <+12>: pacibsp 
        0x18ad0f3d4 <+16>: stp    x29, x30, [sp, #-0x10]!
        0x18ad0f3d8 <+20>: mov    x29, sp
    Target 0: (python3.12) stopped.
    Executable binary set to "/Users/quentinlhoest/.pyenv/versions/3.12.2/bin/python3.12".
    Architecture set to: arm64-apple-macosx-.
    (lldb) bt all
    * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
      * frame #0: 0x000000018ad0f3cc libsystem_kernel.dylib`__psynch_cvwait + 8
        frame #1: 0x000000018ad4e0e0 libsystem_pthread.dylib`_pthread_cond_wait + 984
        frame #2: 0x000000018ac7e298 libc++.1.dylib`std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 32
        frame #3: 0x000000010971e694 libarrow.2300.dylib`arrow::internal::ThreadPool::Shutdown(bool) + 168
        frame #4: 0x000000010971e538 libarrow.2300.dylib`arrow::internal::ThreadPool::~ThreadPool() + 56
        frame #5: 0x000000010971e81c libarrow.2300.dylib`arrow::internal::ThreadPool::~ThreadPool() + 12
        frame #6: 0x000000010948f72c libarrow.2300.dylib`std::__1::shared_ptr<arrow::internal::ThreadPool>::~shared_ptr[abi:ue170006]() + 56
        frame #7: 0x000000018ac05944 libsystem_c.dylib`__cxa_finalize_ranges + 480
        frame #8: 0x000000018ac05704 libsystem_c.dylib`exit + 44
        frame #9: 0x000000018ad57dc8 libdyld.dylib`dyld4::LibSystemHelpers::exit(int) const + 20
        frame #10: 0x000000018a9aec60 dyld`dyld4::LibSystemHelpersWrapper::exit(int) const + 172
        frame #11: 0x000000018a9aeb7c dyld`start + 6048
      thread #2
        frame #0: 0x000000018ad0d8b0 libsystem_kernel.dylib`__workq_kernreturn + 8
      thread #3
        frame #0: 0x000000018ad0f3cc libsystem_kernel.dylib`__psynch_cvwait + 8
        frame #1: 0x000000018ad4e0e0 libsystem_pthread.dylib`_pthread_cond_wait + 984
        frame #2: 0x000000018ac7e298 libc++.1.dylib`std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 32
        frame #3: 0x00000001097234d0 libarrow.2300.dylib`void* std::__1::__thread_proxy[abi:ue170006]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, arrow::internal::ThreadPool::LaunchWorkersUnlocked(int)::$_8>>(void*) + 756
        frame #4: 0x000000018ad4dc0c libsystem_pthread.dylib`_pthread_start + 136
    (lldb) quit
    
  10. pitrou commented on May 6, 2026

    @pitrou
    Member

    Hmm, can you post the tracebacks for all threads? (that would be thread apply all bt with gdb, not sure about lldb)

  11. lhoestq commented on May 6, 2026

    @lhoestq
    Author

    I used bt all which shows the info for all threads, am I missing something ? 👀

  12. pitrou commented on May 6, 2026

    @pitrou
    Member

    Oops, sorry, I had misread. I was expecting the output to be much larger visually 😳

  13. pitrou commented on May 6, 2026

    @pitrou
    Member

    Hmm, on Ubuntu and Python 3.10 I get a crash with the original reproducer, rather than a hang.

  14. pitrou commented on May 6, 2026

    @pitrou
    Member

    I've submitted #49943 which fixes the original reproducer for me on Linux, but I have no idea whether it'll work on macOS or fix the other snippets.

  15. lhoestq commented on May 6, 2026

    @lhoestq
    Author

    I built PyArrow from your PR and it does fix the issue on my side for all three snippets ! 🥳

  16. pitrou commented on May 6, 2026

    @pitrou
    Member

    Ah, interesting. Thanks for testing!

  17. added a commit that references this issue on May 7, 2026
  18. added a commit that references this issue on May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions