Skip to content

Segmentation fault when opening already open dataset #1444

Open

Description

@g2knizn

Environment

OS: Ubuntu 24.04 (Docker 28.5.1)
Python: 3.12.3
netcdf4-python: 1.7.3 (1.6.5 also tested)

Steps to reproduce (docker)

docker run --rm -it ubuntu:24.04 bash

In the docker container:

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-pip python3-venv netcdf-bin
python3 -m venv venv
source ./venv/bin/activate
pip3 install netCDF4==1.7.3

cat <<EOF > example.cdl
netcdf example {
  variables:
    string example;
}
EOF

ncgen -4 -b example.cdl

cat <<EOF > main.py
from netCDF4 import Dataset
for i in range(50):
    file = Dataset("example.nc", "r")
print("Success")
file.close()
EOF

Run:

python3 main.py

Output:

Segmentation fault (core dumped)

Description

Although the code in the example is definitely not recommended, I do not believe that the segmentation error should be the result of its execution.
Some observations:

  1. No segmentation fault when type float (instead of string) is used in the example.cdl
  2. I could not reproduce in netcdf-c
  3. Segmentation fault also for netCDF4-python==1.6.5
  4. Valgrind log:
==5042== Invalid read of size 1
==5042==    at 0x5779194: H5F_addr_decode (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59A4E16: H5VL__native_blob_specific (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x599D95E: H5VL_blob_specific (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x598C59B: H5T__vlen_disk_isnull (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x597D009: H5T__conv_vlen (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x58D3490: H5T_convert_with_ctx (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x58D3669: H5T_convert (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5740732: H5D_get_create_plist (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59A5EF4: H5VL__native_dataset_get (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59934A3: H5VL_dataset_get (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5714242: H5Dget_create_plist (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5429E4C: nc4_get_var_meta (in /venv/lib/python3.12/site-packages/netcdf4.libs/libnetcdf-306d3f1b.so.22)
==5042==  Address 0x40 is not stack'd, malloc'd or (recently) free'd
==5042== 
==5042== 
==5042== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==5042==  Access not within mapped region at address 0x40
==5042==    at 0x5779194: H5F_addr_decode (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59A4E16: H5VL__native_blob_specific (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x599D95E: H5VL_blob_specific (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x598C59B: H5T__vlen_disk_isnull (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x597D009: H5T__conv_vlen (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x58D3490: H5T_convert_with_ctx (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x58D3669: H5T_convert (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5740732: H5D_get_create_plist (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59A5EF4: H5VL__native_dataset_get (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x59934A3: H5VL_dataset_get (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5714242: H5Dget_create_plist (in /venv/lib/python3.12/site-packages/netcdf4.libs/libhdf5-49234e76.so.310.5.1)
==5042==    by 0x5429E4C: nc4_get_var_meta (in /venv/lib/python3.12/site-packages/netcdf4.libs/libnetcdf-306d3f1b.so.22)
==5042==  If you believe this happened as a result of a stack
==5042==  overflow in your program's main thread (unlikely but
==5042==  possible), you can try to increase the size of the
==5042==  main thread stack using the --main-stacksize= flag.
==5042==  The main thread stack size used in this run was 8388608.
==5042== 
==5042== HEAP SUMMARY:
==5042==     in use at exit: 8,131,126 bytes in 11,506 blocks
==5042==   total heap usage: 19,922 allocs, 8,416 frees, 48,204,805 bytes allocated

Sorry if duplicated.

Activity

  1. jswhit2 commented on Nov 4, 2025

    @jswhit2

    Cannot reproduce on macos with

    netcdf4-python version: 1.7.3
    HDF5 lib version: 1.14.6
    netcdf lib version: 4.9.3
    numpy version 2.2.5

    what versions of netcdf-c and hdf5 do you have?

  2. g2knizn commented on Nov 5, 2025

    @g2knizn
    Author

    Thanks for a quick response.

    After some 'debugging' with print statements, I can confirm that the issue is non-deterministic and depends on the behaviour of the garbage collector.

    Here is an example of execution that causes segmentation fault (loop 5 times - for i in range(5)). Each line represents a print statement at the beginning of a function.

    Dataset.__init__
    Dataset.__init__
    Dataset.__init__
    Dataset.__dealloc__
    Dataset.__dealloc__
    Dataset.__dealloc__
    Dataset.__init__
    Segmentation fault (core dumped)
    

    So the problem occurs when init is called after dealloc is called. When init is called one after another, the problem does not occur. You can increase the probability of segmentation fault by increasing loop range (e.g. 1000)

    Segmentation fault (in my case) is in this line:

    self.variables = _get_vars(self, self.auto_complex)

    To answer your question:

    netcdf: 4.9.3
    hdf5: 1.14.6
    
  3. jswhit commented on Nov 5, 2025

    @jswhit
    Collaborator

    I can confirm that if I increase the loop iteration count (from 50 to 5000) I do see a segfault.

  4. jswhit commented on Nov 5, 2025

    @jswhit
    Collaborator

    Looks like the segfault occurs when nc_inq_varname is called inside _get_vars. No idea why.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions