This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Localbus COPAS Bug: Multithreading Issue with 2 Thread Levels
#1
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.
Reply
#2
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.
Reply
#3
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().
Reply
#4
Here's an example of correct usage of copas together with localbus: https://forum.logicmachine.net/showthrea...4#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.
Reply


Forum Jump: