Logic Machine Forum
Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels (/showthread.php?tid=5170)



Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels - Blooddah - 21.12.2023

Description:
I am encountering an issue with my Lua script. The problem is illustrated here with a simplified version to expose the general architecture, but it's in my more complex and extensive code that the bug manifests. During increased CPU load, the complete script seems to enter an infinite loop with CPU usage exceeding 100%, specifically stalling on the 'copas.step()' instruction.

Context:
This ticket presents a simplified version of the code to illustrate the architecture and facilitate discussion. However, the problem occurs in the larger and complete version of the script.

Code snippet (simplified for illustration):
Code:
if not client then
  client = require('localbus').new(0.1)

  function eventhandler(event)
    local eventObject = grp.find(event.dst)
    eventObject.data = knxdatatype.decode(event.datahex, eventObject.datatype)
    copas.addthread(function()
      log("first thread launching other threads")
      -- do some stuff
      copas.addthread(function()
        log("second level thread 1")
        -- do some stuff
      end)
      copas.addthread(function()
        log("second level thread 2")
        -- do some stuff
      end)
    end)
  end

  client:sethandler('groupwrite', eventhandler)
end

log("bug lb")
client:loop(1)
log("bug copas")
copas.step()

[Image: Microsoft-Teams-image-29.png]

Problem Encountered:

High CPU usage and entering an infinite loop in the complete version of the script during periods of load.
Specific stalling on the 'copas.step()' instruction.

Impact:
This situation completely blocks the operation of the script.

Request:
I am seeking assistance to diagnose and resolve this issue in the context of the complete version of my script. Advice on the general architecture as well as specific suggestions to avoid this kind of stalling would be extremely useful.


RE: Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels - admin - 21.12.2023

Please explain what is the purpose of your script and why do you need to spawn several copas threads for every received bus telegram?

There's no point in using Copas the way you do since step() runs once per second after localbus loop finishes.


RE: Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels - Blooddah - 21.12.2023

This is a simplified example, as not all objects automatically trigger threads. I will explain my usage in more detail. First, I check if the event-triggering object contains a specific tag, such as ZONE_1. If so, I start the function zone:run() in a new thread. At the end of zone:run(), the instruction zone:run_units() is executed. This instruction initiates several threads containing the function unit:run(). Within these threads, there may be calls to copas.sleep().


RE: Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels - admin - 21.12.2023

Here's an example of correct usage of copas together with localbus: https://forum.logicmachine.net/showthread.php?tid=1893&pid=11774#pid11774

As for other improvements, you should cache as much data as possible (data types, tags etc) instead of calling grp.find() for every object change.