Skip to content

Segmentation fault when using both eccodes and pygrib in the same Python script #281

@zhenkunl

Description

@zhenkunl

Description:
I am experiencing a segmentation fault or ECCODES runtime errors when using both eccodes (Python bindings) and pygrib in the same script. The crash/error occurs depending on the order of operations.

Steps to reproduce:

Scenario 1 (segmentation fault):

from eccodes import *
import pygrib

f = open("1.grib", 'rb')
gid = codes_grib_new_from_file(f)
codes_release(gid)
f.close()

grbs = pygrib.open("2.grib")   # Segmentation fault here

Scenario 2 (ECCODES runtime error):

from eccodes import *
import pygrib

grbs = pygrib.open("2.grib")   # This succeeds
f = open("1.grib", 'rb')
gid = codes_grib_new_from_file(f)   # Error occurs here

Error output for Scenario 2:

ECCODES ERROR   :  STL exception: unordered_map::at
ECCODES ERROR   :  Runtime error
ECCODES ERROR   :  grib_handle_new_from_file_no_multi: Cannot create handle

Expected behavior:
Both libraries should be usable in the same script without conflicts, regardless of the order of file operations.

Actual behavior:

  • If eccodes is used first, a segmentation fault occurs when later calling pygrib.open().

  • If pygrib is used first, eccodes operations fail with an ECCODES internal error (unordered_map::at).

Environment:

  • eccodes Python bindings version: 2.47.0

  • eccodeslib version: 2.46.2.19

  • pygrib version: 2.1.8

  • Operating system: Ubuntu 24.04

  • Python version: 3.12/3.13

Additional context:
Both packages are wrappers around the ECCODES C library. The errors suggest that the two bindings are not isolated – they interfere with each other's internal state (e.g., global contexts, error queues, or C++ static objects). This makes it impossible to use both packages together in the same Python process.

Possible workaround:
Use only one of the libraries in a given Python session. Alternatively, one might try to load them in separate subprocesses, but that is not a real solution for interactive workflows.

Please investigate whether the Python bindings for ECCODES can be made safe for co-existence with pygrib. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions