Repository navigation
Open
Labels
Description
Describe the bug, including details regarding any error messages, version, and platform.
I am getting reproducible segfaults when trying to close a CSVWriter in the simplest code possible.
import pyarrow as pa
from pyarrow import csv
import argparse
parser = argparse.ArgumentParser(description="Converts a potentially bz2/gz compressed csv file to a tsv/smi file with no header and just 'smiles' and 'id' columns.")
parser.add_argument("input_file", help="Path to the input bz2 file")
parser.add_argument("output_file", help="Path to the output csv file")
parser.add_argument("--chunksize", type=int, default=2**30, help="Size of block to process at a time")
parser.add_argument("--num_chunks", type=int, default=0, help="Number of chunks to process")
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
chunksize = args.chunksize
readopt = csv.ReadOptions(block_size=chunksize)
parseopt = csv.ParseOptions(delimiter="\t")
convertopt = csv.ConvertOptions(column_types={"smiles": pa.string(), "id": pa.string()}, include_columns=["smiles", "id"])
reader = csv.open_csv(
input_file,
read_options=readopt,
parse_options=parseopt,
convert_options=convertopt
)
writeopt = csv.WriteOptions(include_header=False, delimiter="\t", quoting_style="none")
# schema is just smiles and id column, both strings
schema = pa.schema([
('smiles', pa.string()),
('id', pa.string())
])
with csv.CSVWriter(sink=output_file, schema=schema, write_options=writeopt) as writer:
cnt = 0
while cnt < args.num_chunks or args.num_chunks == 0:
try:
writer.write_batch(reader.read_next_batch())
cnt += 1
print("Finished chunk ", cnt)
except StopIteration:
break
print("Done. Closing file.")Output for num_chunks = 2 e.g.:
It hangs for a long time after "Closing file."
Finished chunk 1
Finished chunk 2
Done. Closing file.
Segmentation fault (core dumped)
linux aarch64, pyarrow 17 (tried both pip and conda-forge), also tried many combinations of filesystem, pa.OSFile, batch_sizes, closing manually vs contexts, etc.
My input file is b2zipped.
The culprit seems to be the limiting of the number of chunks that I added for testing. Writing ALL chunks seems to work.
Component(s)
Python
Activity
jorisvandenbossche commentedon Aug 7, 2024
@jpfeuffer thanks for the report!
Could you also share a (snippet of) the csv file (or a similar one with dummy data) to see if we can reproduce this?
jpfeuffer commentedon Aug 7, 2024
Unfortunately nothing small enough to upload. And since it is not working I cannot produce a subset easily. (Not sure a 22 MB subset for GitHub would be very representative either).
Data is from here. Warning 15GB.
https://ftp.enamine.net/download/REAL/Enamine_REAL_HAC_11_21_666M_CXSMILES.cxsmiles.bz2
jpfeuffer commentedon Aug 7, 2024
By the way, I just saw that even if I do not limit the chunks (and the process succeeds without segfault), the written file is always corrupt statrting from a few thousand lines in, with lines being mixed together etc.
Is something wrong with my code? Regular decompression works fine.
jpfeuffer commentedon Aug 8, 2024
Hmm so I found the "error". If I use exactly the schema from the first read batch, it works (no segfaults, no garbled output).
But if I print the schema:
So, what is different from manually specifying the schema like I did in the beginning?
jorisvandenbossche commentedon Aug 13, 2024
@jpfeuffer I was able to reproduce the segfault with the big file you provided. Running with gdb gives the following backtrace:
Details
If I unpack the archive, and read the csv file directly (so without the automatic decompression), it doesn't segfault.
Based on that, trying to create a simpler reproducer with some generated data:
Reading this compressed file with a similar script as your original file:
also segfaults (similarly, if I increase the
num_chunksso that the file is read until the end, it doesn't segfault).jpfeuffer commentedon Aug 13, 2024
That's great (I guess 😅). Btw: Did you see my comment with the update? It seems like it has something to do with the schema used.
jorisvandenbossche commentedon Aug 13, 2024
Yes, was still trying to reproduce that observation ;) Now, as you mentioned, the schema is exactly the same, so it should be something else in the restructured code that causes the change.
That's not something I see with my reproducer above, I think (with mixed together, you mean that there are newline markers missing?)
jorisvandenbossche commentedon Aug 13, 2024
Also, with the original file, after how rows/chunks do you see this? (to see if I can reproduce this part with the original file)
jpfeuffer commentedon Aug 13, 2024
Mmh I am not sure if it was 'just' missing newlines. It felt like it was even more garbled as if the concurrent streams are not flushed at the right time.
It was about after 1500 rows. I think if you do a few chunks and then tail the file you should see it.
jorisvandenbossche commentedon Aug 13, 2024
With the original bz2 file, reading 2 chunks with a 2**30 blocksize (as in your original example), and then running
tail -n 50 test_out.csvgives:Details
Not being familiar with the file, but that looks OK?
Also, do you see that issue with garbled written file with the simplified reproducer with generated data that I posted above?
jorisvandenbossche commentedon Aug 13, 2024
(FWIW, the incremental
open_csvfunction is single threaded, see https://arrow.apache.org/docs/python/csv.html#incremental-reading, so there should be no concurrent streams)jorisvandenbossche commentedon Aug 13, 2024
I also still see the segfault when using the restructured read/write code from #43604 (comment) (using the schema of the first batch to create the writer)
[-]Reproducible segfaults closing CSVWriter[/-][+][Python][C++] Segfault reading bz2 compressed csv file with CSVStreamingReader[/+]jorisvandenbossche commentedon Aug 13, 2024
Further simplication (no need to also write the data out, just reading a subset is enough to trigger the segfault):
Generate some data:
Read part of the file:
Backtrace from running the above with gdb:
Details
The backtrace points to the threadpool / task spawning code (not very familiar with this part).
cc @pitrou
pitrou commentedon Aug 19, 2024
Ok, I looked a bit into this and it's quite complex.
The crash happens at shutdown after the CSV streaming reader was destroyed. Since the streaming reader does some readahead by default (
ReadOptions.use_threads = true), some read tasks are still running on the thread pool. This is driven by theReadaheadGeneratorwhich doesn't appear to be able to cleanup when all references to it are lost. @westonpacepitrou commentedon Aug 19, 2024
Hmm, I might have a fix actually.
apacheGH-43604: [C++] Let ReadaheadGenerator wait for readahead tasks…