Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault with very simple code #1279

Closed
dbolser opened this issue Apr 11, 2024 · 4 comments
Closed

Segmentation fault with very simple code #1279

dbolser opened this issue Apr 11, 2024 · 4 comments

Comments

@dbolser
Copy link

dbolser commented Apr 11, 2024

I was looking to fix the incorrect documentation here:
https://pysam.readthedocs.io/en/latest/usage.html#working-with-tabix-indexed-files

(Just a couple of minor issues).

I thought I'd better run the latest version of the code from git, and I found that my 'simple test' turned up a segfault...

import pysam

tbx = pysam.TabixFile("tests/tabix_data/example_0v26.bed.gz")

for row in tbx.fetch("chr1", 1000, 2000):
    # OK
    print(str(row))

for row in tbx.fetch("chr1", 1000, 2000):
    # Gives "chromosome is c"
    print("chromosome is", row[0])

for row in tbx.fetch("chr1", 1000, 2000, parser=pysam.asTuple()):
    # Gives AttributeError: 'pysam.libctabixproxies.TupleProxy' object has no attribute 'contig'
    print("chromosome is", row.contig)

    # OK
    print("first field (chrom)=", row[0])

print("OK, I'm going to run this")
for row in tbx.fetch("chr1", 1000, 2000, parser=pysam.asBed()):
    print("here we go...")
    # Segmentation fault (core dumped) 😂
    print("name is", row.name)

print("Done!")

The output is:

$ python tests/test_simple_tabix.py 
chr1	1737	2090
chr1	1737	4275
chr1	1873	1920
chr1	1873	3533
chromosome is c
chromosome is c
chromosome is c
chromosome is c
first field (chrom)= chr1
first field (chrom)= chr1
first field (chrom)= chr1
first field (chrom)= chr1
OK, I'm going to run this
here we go...
Segmentation fault (core dumped)
@dbolser
Copy link
Author

dbolser commented Apr 11, 2024

This happens on cdc0ed1. I did the following to install:

git clone [email protected]:pysam-developers/pysam.git
cd pysam/
python -m venv .venv
source .venv/bin/activate
pip install -e .

I guess you need to know my libc and stuff? Is there a handy file I can attach?

@dbolser
Copy link
Author

dbolser commented Apr 11, 2024

$ pip install -e .
Obtaining file:///home/dan/Build/pysam
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Installing backend dependencies ... done
  Preparing editable metadata (pyproject.toml) ... done
Building wheels for collected packages: pysam
  Building editable for pysam (pyproject.toml) ... \

done
  Created wheel for pysam: filename=pysam-0.22.0-0.editable-cp310-cp310-linux_x86_64.whl size=5106 sha256=522a5eaababdd2e259da9d1fbd7257a614a878444f9ae8ea1e6f14fd6e26cdb1
  Stored in directory: /tmp/pip-ephem-wheel-cache-pqsis2yg/wheels/cc/c5/bb/65151f5f844856e0716e962c6e8ead0aac4977a7bd496495c3
Successfully built pysam
Installing collected packages: pysam
Successfully installed pysam-0.22.0

@jmarshall
Copy link
Member

jmarshall commented Apr 19, 2024

The file example_0v26.bed.gz has three columns, which asBed() calls contig, start, and end.

Accessing fields that aren't present in the file is supposed to raise a KeyError. So printing row.score would have raised that exception, but printing row.name — the first absent field — crashes.

This is an off-by-one error in the presence checking that has been present in this code since 2011. Thanks for the easily reproducible bug report.

@dbolser
Copy link
Author

dbolser commented Apr 20, 2024

Awesome! My pleasure! Thanks for the fix and for the great library!

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

No branches or pull requests

2 participants