Skip to content

Compile abort buffer shared lib#362

Open
danbeibei wants to merge 6 commits into
jameskermode:masterfrom
danbeibei:compile_abort_buffer_shared_lib
Open

Compile abort buffer shared lib#362
danbeibei wants to merge 6 commits into
jameskermode:masterfrom
danbeibei:compile_abort_buffer_shared_lib

Conversation

@danbeibei

@danbeibei danbeibei commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

When many routines are wrapped, compiling the wrapper can take a long time. In our build system, we have set up a way to wrap modules separately, then combine the generated wrappers at a later stage. This allows for parallel compilation and is much faster. However, this approach does not work with the current implementation of the error handler that uses the setjmp/longjmp logic.

Currently, a environment_buffer and an abort_message variable are declared for each separate invocation of f2py-f90wrap, with multiple memory spaces allocated for the same symbols. Combining the shared libraries produced by several f2py-f90wrap invocations creates conflicts and results in segmentation faults.

The fix I suggest is to compile a shared library that defines those variables as extern, and then link the f2py-f90wrap generated library with this shared library. This ensures a single address space is allocated to handle the setjmp/longjmp logic. This library is compiled at f90wrap build time and is installed with the f90wrap package.

The example provided shows how we compile in parallel and combine the generated modules afterward. It also reproduces the segmentation fault with the current implementation.

The environment_buffer and abort_message variables were already declared as extern but were not actually used in that way, which suggests that the issue had already been anticipated, perhaps you had a different implementation in mind?

@danbeibei danbeibei force-pushed the compile_abort_buffer_shared_lib branch 3 times, most recently from 16429a5 to f470c41 Compare January 21, 2026 15:33
@danbeibei

Copy link
Copy Markdown
Contributor Author

@jameskermode is this something you would want to merge?

@jameskermode

Copy link
Copy Markdown
Owner

yes, in principle, sorry for th slow response - just a bit overstreched with teaching this term. Will get to this as soon as I can.

@jameskermode

Copy link
Copy Markdown
Owner

I had a look and in principal will be happy to merge, just a couple of things to fix first:

  1. Fix the new test: add test_abort_buffer to the EXAMPLES list in examples/Makefile, and fix the link error (I needed -Wl,-undefined,dynamic_lookup on macOS, or you could restructure so f90wrap_abort is available at libsrc.so link time).
  2. Clean up abort_buffers.c: remove the redundant extern lines and unused includes.
  3. Consider adding rpath to the monkey-patched meson.build: for robustness, should inject install_rpath and build_rpath into the generated meson.build via the existing patching code, rather than relying solely on LDFLAGS. This caused some issues when I tested locally.

@danbeibei danbeibei force-pushed the compile_abort_buffer_shared_lib branch from f470c41 to fe22371 Compare February 23, 2026 12:52
@danbeibei

Copy link
Copy Markdown
Contributor Author

Thank you for the the review. I have updated the commits.

  • Move f90wrap_abort implementation out of inline f2py-generated C code into abort_buffers.c and link externally
  • Refactor meson monkey-patching to always inject fc.find_library('_abort_buffers'), install_rpath, and build_rpath into generated meson.build
  • Add platform-specific LDFLAGS for Darwin (-bundle -undefined dynamic_lookup) in the test Makefile

@danbeibei danbeibei force-pushed the compile_abort_buffer_shared_lib branch from f9198f9 to 3ad0ffe Compare February 23, 2026 13:34
@danbeibei danbeibei force-pushed the compile_abort_buffer_shared_lib branch from 3ad0ffe to e1c6e10 Compare February 23, 2026 13:44
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

Successfully merging this pull request may close these issues.

2 participants