Repository: stepjam/RLBench
Issue: RuntimeError: The call failed on the V-REP side / Found invalid joint handle(s)
Root cause: External Dependency Breakage

### **Summary**
While attempting to generate demos for some tasks I experience an error at the PyRep/CoppeliaSim level which I believe has to do with joint handles.

On the PyRep side, there is: 
```
/pyrep/backend/sim.py", line 28, in _check_return
    'The call failed on the V-REP side. Return value: %d' % ret)
RuntimeError: The call failed on the V-REP side. Return value: -1
```
And in the display for CoppeliaSim reads `Found invalid joint handle(s)`
![image](https://user-images.githubusercontent.com/32983801/108300713-d3e0d300-716e-11eb-8870-4d832e635c9e.png)

I believe the two are related - I haven't replicated either alone - though I'm not familiar enough with either to be sure.  I also believe that it has to do with certain tasks: ReachTarget with the Panda works every time but PickUpCup fails.

### **Details**

I'm posting this here instead of in the PyRep repository because I believe the errors may be a result of the task's or robot's .ttm file.  But I am still learning all 3 of these software packages so let me know if this issue should be moved to PyRep.

I noticed this after switching to the `1.1.0` branch and updating PyRep.  As a first attempt to resolve this I switched back to master but this did not change anything.  

The easiest way to replicate is by changing the task in `examples/imitation_learning.py`.

I've noticed this specifically with the following tasks:
- Pick Up Cups
- Put Plate In Colored Dish Rack
- PickAndLift

The first two seem to fail immediately each time.  I just tested PickAndLift for the first time and it ran a few episodes before CoppeliaSim displayed `Found invalid joint handle(s)` and quit.  Running the program a second time resulted in the same issue. And now running the program seems to display this immediately.

I've also noticed that when starting with the default Reach Target task CoppeliaSim has begun displaying the following warning:

![image](https://user-images.githubusercontent.com/32983801/108302597-499a6e00-7172-11eb-9896-f61c5f84467f.png)


And the error seems to come from a call to PyRep's `sim.GetConfigForTipPose` method. 

Any advice on how to resolve this would be appreciated! 

Using:
|Software|Version|
|---|---|
| Ubuntu | 18.04 |
| PyRep | 4.1.0.1 |
| RLBench | 1.0.10 |
| CoppeliaSim | 4.1.0 |

Here's the full error message I recive when using PickUpCups:

```
andrew@GERTY:~/research/RLBench/examples$ py3 imitation_learning.py 
Traceback (most recent call last):
  File "imitation_learning.py", line 35, in <module>
    demos = task.get_demos(10, live_demos=live_demos)  # -> List[List[Observation]]
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/task_environment.py", line 347, in get_demos
    amount, callable_each_step, max_attempts)
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/task_environment.py", line 360, in _get_live_demos
    self.reset()
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/task_environment.py", line 82, in reset
    randomly_place=not self._static_positions)
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/backend/scene.py", line 117, in init_episode
    self._active_task.validate()
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/backend/task.py", line 252, in validate
    self._waypoints = self._get_waypoints()
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/backend/task.py", line 388, in _get_waypoints
    feasible, way_i = self._feasible(waypoints)
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/backend/task.py", line 341, in _feasible
    path = point.get_path(ignore_collisions=True)
  File "/home/andrew/.local/lib/python3.6/site-packages/rlbench/backend/waypoints.py", line 56, in get_path
    ignore_collisions))
  File "/home/andrew/.local/lib/python3.6/site-packages/pyrep/robots/arms/arm.py", line 452, in get_path
    relative_to)
  File "/home/andrew/.local/lib/python3.6/site-packages/pyrep/robots/arms/arm.py", line 384, in get_nonlinear_path
    max_configs, distance_threshold, max_time_ms, relative_to)
  File "/home/andrew/.local/lib/python3.6/site-packages/pyrep/robots/arms/arm.py", line 148, in solve_ik_via_sampling
    metric, collision_pairs, joint_options, low_limits, max_limits)
  File "/home/andrew/.local/lib/python3.6/site-packages/pyrep/backend/sim.py", line 1396, in simGetConfigForTipPose
    _check_return(ret)
  File "/home/andrew/.local/lib/python3.6/site-packages/pyrep/backend/sim.py", line 28, in _check_return
    'The call failed on the V-REP side. Return value: %d' % ret)
RuntimeError: The call failed on the V-REP side. Return value: -1
QObject::~QObject: Timers cannot be stopped from another thread
QMutex: destroying locked mutex
```

Thanks for reporting; I am able to reproduce. I'll take a look and report back!

I am getting identical error messages (both Dynamics Content pop up when opening the reach task and the RuntimeError: The call failed on the V-REP side. Return value: -1 with the trace starting at get_demos) when running the reach tasks. Strangely enough, it gathers somewhere between 10-20 demos with no problem before the error crashes the program. 

Are there any updates on how to fix this? Thank you in advance

For me, the error was completely resolved by following the changes in the [commit that closed the issue](https://github.com/stepjam/RLBench/commit/3fa86e37fa1f41e2dfede1ee6fef0d6142a46704). I haven't run into anything like it since.

If you are still getting this issue *with* that change, then I'm afraid something else that I don't know about might be wrong.

Interesting. I've pulled the code from the repo this week, and I've verified that the above changes are correct, but I'm still getting this error. I do manage to avoid the error if I run everything in headless mode with xvfb. If there's no quick fix suggestions, I will produce a minimum reproducible example and post a new issue later today