Socket_read_byte_list leads to "Disconnected from Controller" crash

Hi,

I’m working on an interface to controll the robot through a linux box, using URScript and (currently) the 5.11.8.1010444 ursim image for testing. The concept is to open a socket on the linux machine, connect to it from the robot controller and send instructions through it. This was already working. But now I refactored the URScript code to make it a bit clearer and easier to extend and since then there is a reproducible error I don’t understand.

The idea currently is, to have two threads inside the primary program, where the one will take care for communication and the other will carry out anything that actually needs physical time. More or less like this:

def client():
  thread worker():
  end
  thread socket():
  end
end

As I said, this was working. What I did now, is that I divided the threads into different functions, defined and called from inside it:

def client():
  thread worker():
  end
  thread socket():
    def exec():
      ...
    end
    def read():
      ...
    end
    def open():
      ...
    end
    def close():
      ...
    end
    open()
    while loop:
      cmd = read()
      exec(cmd)
    end
    close()
  end
end

This somehow crashes the controller at the line
c = socket_read_byte_list(1, socket_id)
inside the read() funtion.

In the Polyscope log viewer it reads like this:

2022-04-12 17:29:11.516 Program client started
2022-04-12 17:29:11.522 socket connected to 192.168.56.1:60021
2022-04-12 17:29:13.274 Disconnected from Controller

Wireshark tells me, that the controller sends a FIN,ACK immediately after the ACKnowledging the first instruction. The server sends FIN,ACK in return and after a final ACK by the controller, the socket is closed.

In the simulation images kern.log I find the following line:
Apr 12 17:29:11 ursim kernel: [ 145.308060] RuntimeMachine[1830]: segfault at 6 ip 082862ad sp b3689fe0 error 4 in URControl[8048000+579000]

In polyscope.log there is this (beside a whole lot of other error messages I hope to be not responsible for):

2022-04-12 17:29:12.259 ERROR - Problem with message type: Unknown {thread: RTDE receiver , loggerClass: com.ur.polyscope.pin.rtde.RTDEClient}
java.io.EOFException
	at java.io.DataInputStream.readShort(DataInputStream.java:315) ~[?:1.8.0_292]
	at com.ur.polyscope.pin.rtde.RTDEClient.receive(RTDEClient.java:119) [polyscope-internal-pin-10.12.116.jar:?]
	at com.ur.polyscope.pin.rtde.RTDEClient.access$0(RTDEClient.java:115) [polyscope-internal-pin-10.12.116.jar:?]
	at com.ur.polyscope.pin.rtde.RTDEClient$ProcessReceivedMessage.run(RTDEClient.java:184) [polyscope-internal-pin-10.12.116.jar:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
2022-04-12 17:29:12.263 ERROR - RTDE reception failed {thread: RTDE receiver , loggerClass: com.ur.polyscope.pin.rtde.RTDEClient$ProcessReceivedMessage}
java.io.EOFException
	at java.io.DataInputStream.readShort(DataInputStream.java:315) ~[?:1.8.0_292]
	at com.ur.polyscope.pin.rtde.RTDEClient.receive(RTDEClient.java:119) ~[polyscope-internal-pin-10.12.116.jar:?]
	at com.ur.polyscope.pin.rtde.RTDEClient.access$0(RTDEClient.java:115) ~[polyscope-internal-pin-10.12.116.jar:?]
	at com.ur.polyscope.pin.rtde.RTDEClient$ProcessReceivedMessage.run(RTDEClient.java:184) [polyscope-internal-pin-10.12.116.jar:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
2022-04-12 17:29:12.263 ERROR - RTDE connection closed. {thread: RTDE receiver , loggerClass: com.ur.polyscope.pin.rtde.RTDEClient$ProcessReceivedMessage}
2022-04-12 17:29:12.260 ERROR - ControllerSocket: Error in TCP/IP stream;  {thread: ControllerConnectionThread , loggerClass: com.ur.polyscope.controllerinterface.internal.ControllerSocket$ConnectionThread}
java.io.EOFException
	at java.io.DataInputStream.readInt(DataInputStream.java:392) ~[?:1.8.0_292]
	at com.ur.polyscope.controllerinterface.internal.ControllerSocket$ConnectionThread.parseMessage(ControllerSocket.java:351) ~[polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.ControllerSocket$ConnectionThread.performWhileConnected(ControllerSocket.java:326) [polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.ControllerSocket$ConnectionThread.run(ControllerSocket.java:265) [polyscope-controllerinterface-impl-4.9.4.jar:?]
2022-04-12 17:29:12.254 WARN  - Controller monitoring socket not connected:  {thread: Controller Monitoring , loggerClass: com.ur.polyscope.controllerinterface.internal.flightrecord.ConnectionMonitoring}
java.io.EOFException
	at java.io.DataInputStream.readInt(DataInputStream.java:392) ~[?:1.8.0_292]
	at com.ur.polyscope.controllerinterface.internal.flightrecord.StreamReader.readInt(StreamReader.java:33) ~[polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.flightrecord.RealTimeData.receivePackage(RealTimeData.java:17) ~[polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.flightrecord.ConnectionMonitoring.receiveDataLoop(ConnectionMonitoring.java:79) ~[polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.flightrecord.ConnectionMonitoring.tryReceiveDataLoop(ConnectionMonitoring.java:70) [polyscope-controllerinterface-impl-4.9.4.jar:?]
	at com.ur.polyscope.controllerinterface.internal.flightrecord.ConnectionMonitoring.run(ConnectionMonitoring.java:54) [polyscope-controllerinterface-impl-4.9.4.jar:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
2022-04-12 17:29:12.342 ERROR - File attachment not found: /home/ur/ursim-current/.urcontrol/daemon_ur/log/current {thread: pool-12-thread-1 , loggerClass: com.ur.polyscope.flightrecorder.internal.attachment.FlightRecorderTextFileAttachment}

If I reintegrate the functions into just the one thread function, it is working again. Is there anything I missed about how to use threads and functions or is this a bug?

Sorry for beeing a bit longish. If you would anyway like more details, I will happily try to provide them.

It seems I fixed it. In the main client() namespace I defined some helper functions for string parsing and formatted number to string conversion, which I use in exec(). Although they are only used in special cases and have definitely only been defined but not executed at the point where the script crashes, moving them inside the thread block solves the issue.

Usually I’m programming computers and PLCs or robots are somehow exotic to me. So maybe it’s just my background that makes me running into stuff like this. Anyhow it leaves me a bit sceptical. In the script manual I wasn’t able to find anything about calling functions from threads, especially no constrains. Is there any other documentation to read on this?