Page 1 of 1

Sometimes weird values from API call

Posted: 05 Nov 2013, 14:03
by SeveQ
Hi folks,

sometimes, sporadically but (talking about several ten seconds to minutes), I get weird values from a remote API call. Sometimes it's a NaN, sometimes values that are completely wrong. Is there a known issue regarding reliability of remote API calls? This is also expressing in UI events that fire although I don't do anything with the custom UI. In all of these cases I don't get an error from the API call. It's returning simx_error_noerror as expected.

//edit: I've switched to regular API calls and using signals for data transfer and with that I'm getting the same wrong values. So, it's not remote API related!

Any idea?

Cheers,
Hendrik

Re: Sometimes weird values from API call

Posted: 06 Nov 2013, 13:56
by coppelia
Hello Hendrik,

that is weird. Actually this can happen if you provide object positions that are faulty, for example (e.g. x/0 or similar). Those are not always checked when the call is made, and can propagate. Can you give us a concrete code example where this happens, also sporadically?

Cheers

Re: Sometimes weird values from API call

Posted: 09 Nov 2013, 09:59
by SeveQ
Hmm... I've investigated this issue a little further and after all it seems to be related to the remote API in some way.

I've inserted some ifs into my code to check results for NaN or out of range values. But they don't, umh, fire. Despite that I get NaNs and out of range values on the other side of the signal channel (remote API client). I use the remoteApi.dll in a .NET wrapper class via P/Invoke (as mentioned in another thread). Maybe it's related to that somehow.

Another possible cause might be a race condition between setting a signal and accessing the value through the remote API. Or have you already taken measures to prevent this?

I can give you code examples when we've narrowed down the problem further. For now I'd probably have to give you my whole project to reproduce the misbehaviour.

Re: Sometimes weird values from API call

Posted: 09 Nov 2013, 15:57
by coppelia
Race conditions are normally prevented by mutexes, and should not occure. Other things you should be aware of:
  • when a remote API function returns a data pointer, the data remains valid until another remote API function is called.
  • when a remote API function somehow fails, or reports a failure, then the promised data will not be valid. Make sure to always check for call success or failure.
Cheers

Re: Sometimes weird values from API call

Posted: 16 Nov 2013, 16:08
by SeveQ
Alright, so it's possible that the race condition (invalidating a pointer by another remote API call before its value can be read) is on my side as I'm using threads to fetch the data from several sources by using the remote API. So, have I got it right that the remote API isn't thread safe?

Re: Sometimes weird values from API call

Posted: 16 Nov 2013, 16:23
by coppelia
The remote API is capable of multithreaded operation, however, a given client ID should not be accessed from several different threads.

Cheers

Re: Sometimes weird values from API call

Posted: 16 Nov 2013, 23:29
by SeveQ
Alright then... That might be the cause. I'll have a look into it. Thanks a lot!

Re: Sometimes weird values from API call

Posted: 17 Nov 2013, 23:09
by coppelia
For reference, this topic includes the resolution.