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

Vector-valued input parameters may be replaced by uninitialized data #4911

Closed
jngrad opened this issue Apr 25, 2024 · 1 comment
Closed

Vector-valued input parameters may be replaced by uninitialized data #4911

jngrad opened this issue Apr 25, 2024 · 1 comment

Comments

@jngrad
Copy link
Member

jngrad commented Apr 25, 2024

In the ESPResSo 4.2 line, we generalized the use of the script interface methods make_Vector3d() and make_Vector3i, but we forgot to add the except * keyword to propagate exceptions. When we call methods with arguments that are not homologous to a list of size 3, such as a list of size 2 or a string of length 2 or an integer or a float, a TypeError is raised and immediately silenced with a message printed to the terminal. The returned Vector3d or Vector3i then contains uninitialized data and the simulation script continues running.

Here is a MWE:

import espressomd.lb
system = espressomd.System(box_l=3*[10.])
system.time_step = 0.01
lb_fluid = espressomd.lb.LBFluid(
    agrid=1.0, dens=1.0, visc=1.0, tau=0.01,
    ext_force_density=1., kT=1.0, seed=32)
system.actors.add(lb_fluid)
print("script continues")
print(lb_fluid.get_params()["ext_force_density"])  # shows random values

Output:

TypeError: 'float' object is not iterable
Exception ignored in: 'espressomd.utils.make_Vector3d'
Traceback (most recent call last):
  File "/work/jgrad/espresso/build/src/python/espressomd/actors.py", line 59, in add
    actor._activate()
TypeError: 'float' object is not iterable
script continues
[1.00000000e-002 1.00000000e+000 1.10940916e-310]

Expected output:

Traceback (most recent call last):
  File "/work/jgrad/espresso/build/mwe.py", line 7, in <module>
    system.actors.add(lb_fluid)
  File "/work/jgrad/espresso/build/src/python/espressomd/actors.py", line 59, in add
    actor._activate()
  File "lb.pyx", line 69, in espressomd.lb.FluidActor._activate
  File "lb.pyx", line 556, in espressomd.lb.LBFluid._activate_method
  File "lb.pyx", line 293, in espressomd.lb.HydrodynamicInteraction._set_params_in_es_core
  File "lb.pyx", line 489, in espressomd.lb.HydrodynamicInteraction.ext_force_density.__set__
  File "utils.pyx", line 246, in espressomd.utils.make_Vector3d
TypeError: 'float' object is not iterable

While the bug was also present in the 4.1 line, all call sites would first check whether the argument was homologous to a vector of size 3 before passing it to make_Vector3d(). In the python branch, these functions were replaced by a safer alternative that raises a RuntimeError.

Many thanks to @pm-blanco for reporting this issue to us.

@jngrad jngrad added this to the ESPResSo 4.2.2 milestone Apr 25, 2024
@jngrad jngrad self-assigned this Apr 25, 2024
jngrad added a commit to jngrad/espresso that referenced this issue Apr 25, 2024
Release 4.2.0 generalized the use of utility functions to convert
Python vector-valued objects of size 3 to the corresponding C++
script interface Vector3d or Vector3i types. Exceptions raised in
these functions were not propagated to the Python interpreter,
printing "Exception ignored in: 'espressomd.utils.make_Vector3d'"
to the screen and leaving the call site with a fully or partially
uninitialized object. This is now resolved.
@jngrad
Copy link
Member Author

jngrad commented Apr 25, 2024

Fixed in 4.2 by 89737a8.

@jngrad jngrad closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant