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

Calling external library that catches exception internally causes crash of julia #10273

Closed
ibell opened this issue Feb 21, 2015 · 27 comments
Closed
Labels
bug Indicates an unexpected problem or unintended behavior building Build system, or building Julia or its dependencies

Comments

@ibell
Copy link

ibell commented Feb 21, 2015

I have a C++ library (CoolProp) that can be called using a shared library. When I call the function with inputs that cause an exception to be caught and bubbled in the library, it crashes julia, while the same set of inputs work fine in python and return HUGE as the output value.

Julia module:

module CoolProp

export PropsSI

function PropsSI(Output::String, Name1::String, Value1::Float64, Name2::String, Value2::Float64, Fluid::String)
  return ccall( (:PropsSI, "CoolProp"), Cdouble, (Ptr{Uint8},Ptr{Uint8},Cdouble,Ptr{Uint8},Cdouble,Ptr{Uint8}), Output,Name1,Value1,Name2,Value2,Fluid)
end

end #module

and in julia

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.6 (2015-01-08 22:33 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  x86_64-linux-gnu

julia> import CoolProp

julia> CoolProp.PropsSI("T","P",101325.0,"Q",0.0,"Water")
373.12429584768836

julia> CoolProp.PropsSI("T","P",-101325.0,"Q",0.0,"Water")

signal (6): Aborted
gsignal at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
abort at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_Unwind_Resume at /lib/x86_64-linux-gnu/libgcc_s.so.1 (unknown line)
_ZN8CoolProp16_PropsSI_outputsERNSt3tr110shared_ptrINS_13AbstractStateEEESt6vectorINS_16output_parameterESaIS6_EENS_11input_pairsERKS5_IdSaIdEESD_RS5_ISB_SaISB_EE at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp13_PropsSImultiERKSt6vectorISsSaISsEERKSsRKS0_IdSaIdEES6_SA_S6_S4_SA_RS0_IS8_SaIS8_EE at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp7PropsSIERKSsS1_dS1_dS1_ at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
PropsSI at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
PropsSI at /home/ian/Code/coolprop/build/so/CoolProp.jl:14
Aborted (core dumped)

whereas in python, calling the same shared library, yields the infinite output values and no crash:

from ctypes import *

CoolProp = cdll.LoadLibrary('/home/ian/Code/coolprop/build/so/CoolProp.so')

PropsSI = CoolProp.PropsSI
PropsSI.restype = c_double

print PropsSI(c_char_p("T"), c_char_p("P"),c_double(101325),c_char_p("Q"),c_double(1.0),c_char_p("Water"))
print PropsSI(c_char_p("T"), c_char_p("P"),c_double(-101325),c_char_p("Q"),c_double(1.0),c_char_p("Water"))
print PropsSI(c_char_p("P"), c_char_p("T"),c_double(700),c_char_p("Q"),c_double(1.0),c_char_p("Water"))

yields

373.124295848
inf
inf
@ihnorton
Copy link
Member

It looks like the CoolProp C API is not exception safe. Julia doesn't know anything about C++ exceptions, so you need to wrap your internal calls in a try block and propagate the fact that an exception was thrown some other way.

The CoolProp Cython wrapper explicitly annotates the fact that these functions may throw, so you get exception handling for free from Cython.

@ibell
Copy link
Author

ibell commented Feb 22, 2015

The CoolProp::PropsSI function catches all exceptions and does not allow
them to bubble through the shared library interface. So no exceptions can
pass through the top-level PropsSI function

On Sat, Feb 21, 2015 at 6:26 PM, Isaiah [email protected] wrote:

It looks like the CoolProp C API not exception safe
https://github.com/CoolProp/CoolProp/blob/master/src/CoolPropLib.cpp#L190-L196.
Julia doesn't know anything about C++ exceptions, so you need to wrap your
internal calls in a try block and propagate the fact that an exception
was thrown some other way.

The CoolProp Cython wrapper explicitly annotates
https://github.com/CoolProp/CoolProp/blob/master/wrappers/Python/CoolProp/CoolProp.pxd#L18-L20
the fact that these functions may throw, so you get exception handling for
free from Cython
http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#exceptions.


Reply to this email directly or view it on GitHub
#10273 (comment).

@ibell
Copy link
Author

ibell commented Feb 22, 2015

BTW, I am one of the main developers of CoolProp, so I have a pretty good
idea what is going on internally.

On Sat, Feb 21, 2015 at 6:51 PM, Ian Bell [email protected] wrote:

The CoolProp::PropsSI function catches all exceptions and does not allow
them to bubble through the shared library interface. So no exceptions can
pass through the top-level PropsSI function

On Sat, Feb 21, 2015 at 6:26 PM, Isaiah [email protected] wrote:

It looks like the CoolProp C API not exception safe
https://github.com/CoolProp/CoolProp/blob/master/src/CoolPropLib.cpp#L190-L196.
Julia doesn't know anything about C++ exceptions, so you need to wrap your
internal calls in a try block and propagate the fact that an exception
was thrown some other way.

The CoolProp Cython wrapper explicitly annotates
https://github.com/CoolProp/CoolProp/blob/master/wrappers/Python/CoolProp/CoolProp.pxd#L18-L20
the fact that these functions may throw, so you get exception handling for
free from Cython
http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#exceptions
.


Reply to this email directly or view it on GitHub
#10273 (comment).

@ihnorton
Copy link
Member

I built the library and can't reproduce this in either 0.4 or 0.3.6.

note: built with ccmake ../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON, otherwise all defaults, on Ubuntu 14.04.

@ihnorton
Copy link
Member

julia> push!(DL_LOAD_PATH, ".")
1-element Array{Union(UTF8String,ASCIIString),1}:
 "."
julia> import CoolProp
julia> CoolProp.PropsSI("T","P",101325.0,"Q",0.0,"Water")
373.12429584768836
julia> CoolProp.PropsSI("T","P",-101325.0,"Q",0.0,"Water")
Inf

@ibell
Copy link
Author

ibell commented Feb 22, 2015

How did you build the library?

On Sat, Feb 21, 2015 at 8:09 PM, Isaiah [email protected] wrote:

julia> push!(DL_LOAD_PATH, ".")
1-element Array{Union(UTF8String,ASCIIString),1}:
"."
julia> import CoolProp
julia> CoolProp.PropsSI("T","P",101325.0,"Q",0.0,"Water")
373.12429584768836
julia> CoolProp.PropsSI("T","P",-101325.0,"Q",0.0,"Water")
Inf


Reply to this email directly or view it on GitHub
#10273 (comment).

@ihnorton
Copy link
Member

ccmake ../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON, otherwise all defaults. Then make. On Ubuntu 14.04.

@ibell
Copy link
Author

ibell commented Feb 22, 2015

Hmm, myself and another developer have used a similar procedure to cause
the failure. And I'm on Xubuntu 14.04, julia 0.3.6 in the failing case
above. Weird.

On Sat, Feb 21, 2015 at 8:17 PM, Isaiah [email protected] wrote:

ccmake ../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON, otherwise all
defaults. Then make. On Ubuntu 14.04.


Reply to this email directly or view it on GitHub
#10273 (comment).

@ibell
Copy link
Author

ibell commented Feb 22, 2015

Out of curiosity, what compiler do you use?

On Sat, Feb 21, 2015 at 8:22 PM, Ian Bell [email protected] wrote:

Hmm, myself and another developer have used a similar procedure to cause
the failure. And I'm on Xubuntu 14.04, julia 0.3.6 in the failing case
above. Weird.

On Sat, Feb 21, 2015 at 8:17 PM, Isaiah [email protected] wrote:

ccmake ../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON, otherwise all
defaults. Then make. On Ubuntu 14.04.


Reply to this email directly or view it on GitHub
#10273 (comment).

@ihnorton
Copy link
Member

gcc

@JonWel
Copy link

JonWel commented Feb 22, 2015

I thought it may have been the -O3 that would make the difference, but I still have the crash without.
@ihnorton Which wrapper did you use? Where are the wrapper files and library placed?

The way I first compiled:

git clone https://github.com/CoolProp/CoolProp --recursive
cd CoolProp
mkdir build && cd build
cmake .. -DCOOLPROP_64BIT_SHARED_LIBRARY=ON
cmake --build .

The way I tested seeing @ihnorton's commands:

git clone https://github.com/CoolProp/CoolProp --recursive
cd CoolProp
mkdir build && cd build
ccmake ../../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON # c c g # I'm not used to ccmake, so this may not be the good way
make

Then:

sudo cp ./CoolProp/build/libCoolProp.so /usr/lib/x86_64-linux-gnu/julia/CoolProp.so

And the wrapper (from the github) is placed in

/home/johndoe/.julia/v0.3/CoolProp/src/CoolProp.jl

And I keep having the crash instead of an Inf value.

@ihnorton
Copy link
Member

Yes, exactly the same build steps (c c g). I tried the Julia wrapper in the CoolProp source as well as the mini module in the top comment. I start Julia from the CoolProp build directory, and add the current directory to the DLL search path (push!(DL_LOAD_PATH,".")) before loading the module.

@JonWel
Copy link

JonWel commented Feb 22, 2015

I tested on a fresh virtual machine with Ubuntu gnome 14.10 64bit up to date, French system language.
(I previously tested on my everyday Ubuntu, same version but less fresh).

All the command below where send on a same terminal one after the other, starting in the home directory and continue in the ./CoolProp folder after entering it. It thus should be fully reproducible.

I started to install Julia:

sudo add-apt-repository ppa:staticfloat/julianightlies
sudo add-apt-repository ppa:staticfloat/julia-deps
sudo apt install julia

Than the tool for ccmake:

sudo apt install cmake-curses-gui

Than download and build CoolProp:

git clone https://github.com/CoolProp/CoolProp --recursive
cd CoolProp
ccmake ../CoolProp -DCOOLPROP_64BIT_SHARED_LIBRARY=ON # c c g
make

Than I ran Julia:

user@vm-user:~/CoolProp$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.0-dev+3472 (2015-02-20 05:10 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 8c87a32* (2 days old master)
|__/                   |  x86_64-linux-gnu
julia> push!(DL_LOAD_PATH,".")
1-element Array{Union(ASCIIString,UTF8String),1}:
"."

julia> module CoolProp

       export PropsSI

       function PropsSI(Output::String, Name1::String, Value1::Float64, Name2::String, Value2::Float64, Fluid::String)
         return ccall( (:PropsSI, "libCoolProp"), Cdouble, (Ptr{Uint8},Ptr{Uint8},Cdouble,Ptr{Uint8},Cdouble,Ptr{Uint8}), Output,Name1,Value1,Name2,Value2,Fluid)
       end

       end #module

julia> import CoolProp

julia> CoolProp.PropsSI("T","P",101325.0,"Q",0.0,"Water")
373.12429584768836

julia> CoolProp.PropsSI("T","P",-101325.0,"Q",0.0,"Water")

signal (6): Abandon
gsignal at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
abort at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_Unwind_Resume at /lib/x86_64-linux-gnu/libgcc_s.so.1 (unknown line)
_ZN8CoolProp16_PropsSI_outputsERNSt3tr110shared_ptrINS_13AbstractStateEEESt6vectorINS_16output_parameterESaIS6_EENS_11input_pairsERKS5_IdSaIdEESD_RS5_ISB_SaISB_EE at ./libCoolProp.so (unknown line)
_ZN8CoolProp13_PropsSImultiERKSt6vectorISsSaISsEERKSsRKS0_IdSaIdEES6_SA_S6_S4_SA_RS0_IS8_SaIS8_EE at ./libCoolProp.so (unknown line)
_ZN8CoolProp7PropsSIERKSsS1_dS1_dS1_ at ./libCoolProp.so (unknown line)
PropsSI at ./libCoolProp.so (unknown line)
PropsSI at none:6
Abandon (core dumped)
user@vm-user:~/CoolProp$

Apart from the system language and the graphical interface that could be different, I do not see anything different from what you done in the steps I wrote above.

Have any idea of where the problem hides?

@ihnorton
Copy link
Member

I was using the generic linux download. I can reproduce this with the binary from the ppa. Will investigate.

@tkelman
Copy link
Contributor

tkelman commented Feb 23, 2015

The issue with the PPA Julia looks similar to jump-dev/Ipopt.jl#9 - something about the version of libunwind in the PPA isn't working quite right.

@ihnorton ihnorton added bug Indicates an unexpected problem or unintended behavior building Build system, or building Julia or its dependencies labels Feb 27, 2015
@ibell
Copy link
Author

ibell commented Mar 10, 2015

bump.

@tkelman
Copy link
Contributor

tkelman commented Mar 10, 2015

How recent is your ppa build? See jump-dev/Ipopt.jl#9 (comment) which should have addressed this at least for the ppa build of Julia?

@ibell
Copy link
Author

ibell commented Mar 10, 2015

@JonWel, can you try it out?
On Mar 9, 2015 11:42 PM, "Tony Kelman" [email protected] wrote:

How recent is your ppa build? See jump-dev/Ipopt.jl#9 (comment)
jump-dev/Ipopt.jl#9 (comment)
which should have addressed this at least for the ppa build of Julia?


Reply to this email directly or view it on GitHub
#10273 (comment).

@JonWel
Copy link

JonWel commented Mar 10, 2015

Release:
ppa version: julia 0.3.6-depsfix2-utopic.
Julia version: 0.3.6 (2015-01-08 22:33 UTC)
Still crash

Nightly:
ppa version: 0.4.0-1953~ubuntu14.10.1
Julia version: 0.4.0-dev+3727 (2015-03-08 23:04 UTC)
Works correctly

So the problem is corrected for the development version, but not yet for the release.

@JonWel
Copy link

JonWel commented Mar 14, 2015

This is now fixed for me for both ppa since:
0.3.6-depsfix9-utopic
0.4.0-1953~ubuntu14.10.1

None of them crash any more.

@ihnorton
Copy link
Member

Great. (thanks @staticfloat!)

@JonWel
Copy link

JonWel commented May 27, 2016

@staticfloat Could this problem have happened again?

I ran a code working correctly on Windows 7 64bit, but running it on Ubuntu 16.04 64bit with Julia Version 0.4.5 (2016-03-18 00:58 UTC):

signal (6): Abandon
gsignal at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
abort at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_Unwind_Resume at /lib/x86_64-linux-gnu/libgcc_s.so.1 (unknown line)
_ZN8CoolProp17SaturationSolvers23successive_substitutionERNS_26HelmholtzEOSMixtureBackendEdddRKSt6vectorIdSaIdEERS5_RNS0_14mixture_VLE_IOE at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp13FlashRoutines8PQ_flashERNS_26HelmholtzEOSMixtureBackendE at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp26HelmholtzEOSMixtureBackend6updateENS_11input_pairsEdd at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp20AbstractCubicBackend10saturationENS_11input_pairsE at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
_ZN8CoolProp20AbstractCubicBackend6updateENS_11input_pairsEdd at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
AbstractState_update at /usr/bin/../lib/x86_64-linux-gnu/julia/CoolProp.so (unknown line)
AbstractState_update at /home/johndoe/.julia/v0.4/CoolProp/src/CoolProp.jl:199
Julia has stopped: null, SIGABRT

Which is extremely similar to the message we had here.

Running apt-cache policy julia gives:

julia:
  Installed: 0.4.5-3
  Candidate: 0.4.5-3
  Version table:
 *** 0.4.5-3 500
        500 http://be.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
        100 /var/lib/dpkg/status

So the package is installed from the Ubuntu repository (I don't know how the communication with your own repository is done, and maybe installing from ppa:staticfloat/juliareleases may solve the problem).

@tkelman
Copy link
Contributor

tkelman commented May 27, 2016

Are you using the PPA still? Try the generic linux binaries.

@JonWel
Copy link

JonWel commented May 27, 2016

@tkelman I've just updated my comment to add more information about that. I'm not using staticfloat's ppa but the version in Ubuntu repositories.

There are in deed good chances the generic binaries doesn't have this problem.

@tkelman
Copy link
Contributor

tkelman commented May 27, 2016

If that ends up being the case, then report this as a bug in the debian packaging. They're probably linking libunwind as a shared library, or using a version of libunwind that doesn't have patches we need.

@JonWel
Copy link

JonWel commented May 27, 2016

The crash do not occur with staticfloat's ppa.
This is clearly a problem with ubuntu's julia-common ppa.

When doing the install -- reinstall, I had:

The following packages were automatically installed and are no longer required:
  julia-common libdsfmt-19937-1 libopenlibm2 libopenspecfun1 libpcre2-8-0
  libspqr2.0.2 libutf8proc1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libamd2.2.0 libcholmod1.7.1 libcolamd2.7.1 libfftw3-3 libfftw3-long3
  librmath-julia-base librmath-julia-dev libumfpack5.4.0
Suggested packages:
  ess julia-doc libfftw3-bin libfftw3-dev
The following NEW packages will be installed:
  libamd2.2.0 libcholmod1.7.1 libcolamd2.7.1 libfftw3-3 libfftw3-long3
  librmath-julia-base librmath-julia-dev libumfpack5.4.0
The following packages will be upgraded:
  julia

(and julia-common had to be removed manually before the package julia could be installed).

How do I report that correctly to Ubuntu's package managers?

@tkelman
Copy link
Contributor

tkelman commented May 27, 2016

Launchpad for Ubuntu I think, unless you can verify it also happens in debian. Might be a problem with both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior building Build system, or building Julia or its dependencies
Projects
None yet
Development

No branches or pull requests

4 participants