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

Matlab function not responding after certain times of calls. #255

Open
lshenglin opened this issue Jun 16, 2017 · 6 comments
Open

Matlab function not responding after certain times of calls. #255

lshenglin opened this issue Jun 16, 2017 · 6 comments

Comments

@lshenglin
Copy link

First of all, I have to say this python-matlab-bridge is great, very easy to use, very helpful examples. However I am met with this issue that I think has not been raised by other users. I'm trying to call a Matlab function over and over and I find it always fails at a certain numbers of calls. To illustrate, I attach the code and output below:
Code starts*************************
import sys
import os

from pymatbridge import Matlab

mlab = Matlab(executable='/Applications/MATLAB_R2017a.app/bin/matlab')
mlab.start()

matlab_call_cnt = 0

while True:
matlab_call_cnt+=1
print(matlab_call_cnt)
res = mlab.run_func('/Users/shenglinli/PycharmProjects/pymat_test/jk.m', {'arg1': 3, 'arg2': 5})
print(res['result'])
mlab.stop()
Code ends*************************

And it will print number of Matlab function calls and the result of the "jk.m" function which is 8. So it goes like:
Output starts*************************
Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-849ff32d-88b6-4a38-8509-7f59d3a705c1
Send 'exit' command to kill the server
........MATLAB started and connected!
1
8
2
8
3
8
4
8
...
269
8
270
8
271
Output ends*************************
Basically I find that it always stuck at 271st call of Matlab function, 100% repeatable. It seems like python sends request to Matlab server and does not get a response on that function call. I'd really appreaciate it if someone can share some insight on this issue.

And below are my settings:
OSX
Matlab R2017a
Current libzmq version is 4.1.6
Current pyzmq version is 16.0.2

Thanks!

@arokem
Copy link
Owner

arokem commented Jun 18, 2017

I am mystified. I have no idea where that kind of behavior would arise.

@lshenglin
Copy link
Author

Hi Arokem, thanks for replying. I'm surprised to be met with this kind of issue, too. Have you got any chance to try the scripts in my original post? It is nothing but calling the example "jk.m" function over and over in a loop. I'd like to understand if it is anyway reproducible in other user's environment.

@matthias-k
Copy link

Hi @arokem, thanks for this really helpful library!

I have a similar problem to the one described in this issue and I was able to break it down a bit more: I don't even need to call any special matlab function for that. All I have to do is to send numpy arrays to matlab.

mlab = Matlab(log=False)
mlab.start()

for i in range(1000):
    print(i)
    mlab.set_variable('foo', np.array([42]))

This fails reproducibly in the 207th interation (although sometimes I have experienced the hang to occur a few iterations earlier, but always after the 200th iteration). It doesn't seem to depend on the size of the arrays: mlab.set_variable('foo', np.random.randn(1000, 2000)) leads to exactly the same behaviour. It hangs in the 207th iteration, as does mlab.set_variable('foo', np.random.randn(1000, 1000))). However, mlab.set_variable('foo', 42) doesn't seem to fail at all (or at least only way later). Also, mlab.run_code('a=randn(1000,1000);') doesn't fail (although it stalled for a like 30s inbetween). And interestingly, mlab.set_variable('foo', np.array(42)) fails immediately. But that might be a completely different bug.

For comparison, I also tried to retrieve variables in stead of setting them:

mlab.run_code('a =randn(1000, 1000);')
for i in range(10000):
    a = mlab.get_variable('a')

This also fails reproducibly, but already after 106 iterations. It again seems to be independent of the shape of the variable: a=randn(10,10); lead to exactly the same behaviour.

The first idea that comes to my mind is that the library is filling up some buffer that doesn't get deleted? Maybe in zmq?

When I then interrupt (in the jupyter notebook, but it is reproducible in ipython), I get this stacktrace:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-9-0cd81b6f315d> in <module>()
      1 # fails in iteration i==
      2 for i in tqdm(range(1000)):
----> 3     mlab.set_variable('foo', np.array([42]))

~/anaconda/envs/masc/lib/python3.6/site-packages/pymatbridge/pymatbridge.py in set_variable(self, varname, value)
    326         if isinstance(value, spmatrix):
    327             return self._set_sparse_variable(varname, value)
--> 328         return self.run_func('assignin', 'base', varname, value, nargout=0)
    329 
    330     def set_plot_settings(self, width=512, height=384, inline=True):

~/anaconda/envs/masc/lib/python3.6/site-packages/pymatbridge/pymatbridge.py in run_func(self, func_path, *func_args, **kwargs)
    307                                    func_args=func_args or '',
    308                                    dname=dname,
--> 309                                    nargout=nargout)
    310 
    311     def run_code(self, code):

~/anaconda/envs/masc/lib/python3.6/site-packages/pymatbridge/pymatbridge.py in _json_response(self, **kwargs)
    270 
    271     def _json_response(self, **kwargs):
--> 272         return json.loads(self._response(**kwargs), object_hook=decode_pymat)
    273 
    274     def run_func(self, func_path, *func_args, **kwargs):

~/anaconda/envs/masc/lib/python3.6/site-packages/pymatbridge/pymatbridge.py in _response(self, **kwargs)
    228         req = json.dumps(kwargs, cls=PymatEncoder)
    229         self.socket.send_string(req)
--> 230         resp = self.socket.recv_string()
    231         return resp
    232 

~/anaconda/envs/masc/lib/python3.6/site-packages/zmq/sugar/socket.py in recv_string(self, flags, encoding)
    455             The Python unicode string that arrives as encoded bytes.
    456         """
--> 457         msg = self.recv(flags=flags)
    458         return self._deserialize(msg, lambda buf: buf.decode(encoding))
    459 

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7683)()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7460)()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:2344)()

~/anaconda/envs/masc/lib/python3.6/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:9621)()

KeyboardInterrupt: 

and the notebook server itself additionally prints

Operation terminated by user during json_load>base64decode (line 240)


In json_load>parse_data_ (line 128)
          arr = typecast(base64decode(value.data), 'double');

In json_load>parse_data_ (line 106)
      value{i} = parse_data_(node.get(i-1), options);

In json_load>parse_data_ (line 122)
      value.(safe_field) = parse_data_(node.get(javaObject('java.lang.String',
      key)), ...

In json_load (line 75)
  value = parse_data_(node, options);

In matlabserver (line 13)
    req = json_load(msg_in);

I'm using ubuntu 16.04, Matlab R2016b. If there is anything I can do to further help debugging this issue, I would be happy to try it.

@matthias-k
Copy link

This happens both with version 0.5.2 from pypi and the master branch from this repository.

@matthias-k
Copy link

By the way, I did not yet manage to access the matlab log. If I set log=True in mlab=Matlab(log=True), then mlab.startup_options reads ' -nodesktop -nosplash -logfile ./pymatbridge/logs/matlablog_python-matlab-bridge.txt'. To me this reads as the logfile should be in a subdirectory pymatbridge of the current working directory, but it doesn't exist. I thought the logfile might help in debugging where exactly this problem is rooted.

@migperfer
Copy link

migperfer commented Jul 5, 2021

Hi! I'm having a similar problem with Matlab2021a and Ubuntu 18.04, it freezes when executing the same function many times. However, if I stop and start the session every few (let say 10) iterations it takes way much more time to freeze. I'm not sure how it works in the background this matlab bridge, but maybe this information is useful for you.

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

No branches or pull requests

4 participants