From 5d659f421996dffa57f2744da347d47b30f76d4c Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 15 Nov 2021 22:16:11 -0800 Subject: [PATCH 1/2] Document that cfunctions should not throw errors This was not documented, see https://discourse.julialang.org/t/what-happens-when-you-throw-an-error-from-a-cfunction/71510?u=simonbyrne --- doc/src/manual/calling-c-and-fortran-code.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/src/manual/calling-c-and-fortran-code.md b/doc/src/manual/calling-c-and-fortran-code.md index 9fb3a14d0749b..7ba7e12e39320 100644 --- a/doc/src/manual/calling-c-and-fortran-code.md +++ b/doc/src/manual/calling-c-and-fortran-code.md @@ -186,6 +186,11 @@ Julia function. The arguments to [`@cfunction`](@ref) are: function on 32-bit Windows, but can be used on WIN64 (where `stdcall` is unified with the C calling convention). +!!! note + Callback functions exposed via `@cfunction` should not throw errors, as that will + return control to the Julia runtime and may leave the library invoking the function in + an undefined state. + A classic example is the standard C library `qsort` function, declared as: ```c From f9ad1f2b142e32ae609eb159305d070779a94930 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 16 Nov 2021 11:33:54 -0800 Subject: [PATCH 2/2] Update doc/src/manual/calling-c-and-fortran-code.md Co-authored-by: Jameson Nash --- doc/src/manual/calling-c-and-fortran-code.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/src/manual/calling-c-and-fortran-code.md b/doc/src/manual/calling-c-and-fortran-code.md index 7ba7e12e39320..5529018217c1a 100644 --- a/doc/src/manual/calling-c-and-fortran-code.md +++ b/doc/src/manual/calling-c-and-fortran-code.md @@ -188,8 +188,7 @@ Julia function. The arguments to [`@cfunction`](@ref) are: !!! note Callback functions exposed via `@cfunction` should not throw errors, as that will - return control to the Julia runtime and may leave the library invoking the function in - an undefined state. + return control to the Julia runtime unexpectedly and may leave the program in an undefined state. A classic example is the standard C library `qsort` function, declared as: