Skip to contentSkip to conversation

event: Prevent bitbake from executing event handler for wrong multiconfig target

PATCH Superseded Tomasz Dziendzielski 2 participants 3 messages Feb 6, 2021 – Feb 9, 2021

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 event: Prevent bitbake from executing event handler for wrong multiconfig target

When multiconfig is used bitbake might try to run events that don't exist for specific mc target. In cooker.py we pass self.databuilder.mcdata[mc] data that contains names of events' handlers per mc target, but fire_class_handlers uses global _handlers variable that is created during parsing of all the targets.

This leads to a problem where bitbake runs event handler that don't exist for a target or even overrides them - if multiple targets use event handler with the same name but different code then only one version will be executed for all targets.

See [YOCTO #13071] for detailed bug information.

Add mc target name as a prefix to event handler name so there won't be two different handlers with the same name.

--- v2 compared to previous patch I added mc prefix before putting to __BBHANDLERS, so hashes are not changed during the build and oe-selftest paseses
--- v3 fix for test_stoptask_behavior, _bb_diskmonitor handler is an exception that is not created during parsing and was not added to __BBHANDLERS where I check if a handler should be executed. In this change I add it manually, so it is detected and executed

---
 lib/bb/cookerdata.py | 5 ++++-
 lib/bb/event.py      | 2 ++
 lib/bb/parse/ast.py  | 7 ++++++-
 lib/bb/runqueue.py   | 3 +++
 4 files changed, 15 insertions(+), 2 deletions(-)
lib/bb/cookerdata.py +4 -1
index c39b5681..02995c8f 100644
@@ -424,12 +424,15 @@ class CookerDataBuilder(object):
         # Nomally we only register event handlers at the end of parsing .bb files
         # We register any handlers we've found so far here...
         for var in data.getVar('__BBHANDLERS', False) or []:
+            nvar=var
+            if mc:
+                var = nvar[len(mc):]
             handlerfn = data.getVarFlag(var, "filename", False)
             if not handlerfn:
                 parselog.critical("Undefined event handler function '%s'" % var)
                 raise bb.BBHandledException()
             handlerln = int(data.getVarFlag(var, "lineno", False))
-            bb.event.register(var, data.getVar(var, False),  (data.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
+            bb.event.register(nvar, data.getVar(var, False),  (data.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
 
         data.setVar('BBINCLUDED',bb.parse.get_file_depends(data))
 
lib/bb/event.py +2 -0
index 694b4705..afef7dbe 100644
@@ -118,6 +118,8 @@ def fire_class_handlers(event, d):
             if _eventfilter:
                 if not _eventfilter(name, handler, event, d):
                     continue
+            if d and name not in (d.getVar("__BBHANDLERS") or []):
+                    continue
             execute_handler(name, handler, event, d)
 
 ui_queue = []
lib/bb/parse/ast.py +6 -1
index 0714296a..b38978d3 100644
@@ -261,6 +261,8 @@ class BBHandlerNode(AstNode):
     def eval(self, data):
         bbhands = data.getVar('__BBHANDLERS', False) or []
         for h in self.hs:
+            if data.getVar('BB_CURRENT_MC', False):
+                h = data.getVar('BB_CURRENT_MC', False) + h
             bbhands.append(h)
             data.setVarFlag(h, "handler", 1)
         data.setVar('__BBHANDLERS', bbhands)
@@ -331,11 +333,14 @@ def finalize(fn, d, variant = None):
     try:
         for var in d.getVar('__BBHANDLERS', False) or []:
             # try to add the handler
+            nvar=var
+            if d.getVar('BB_CURRENT_MC', False):
+                var = nvar[len(d.getVar('BB_CURRENT_MC', False)):]
             handlerfn = d.getVarFlag(var, "filename", False)
             if not handlerfn:
                 bb.fatal("Undefined event handler function '%s'" % var)
             handlerln = int(d.getVarFlag(var, "lineno", False))
-            bb.event.register(var, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
+            bb.event.register(nvar, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
 
         bb.event.fire(bb.event.RecipePreFinalise(fn), d)
 
lib/bb/runqueue.py +3 -0
index 28bdadb4..848ae574 100644
@@ -1464,6 +1464,9 @@ class RunQueue:
             bb.event.fire(bb.event.DepTreeGenerated(depgraph), self.cooker.data)
 
             if not self.dm_event_handler_registered:
+                 bbhands = self.cfgData.getVar('__BBHANDLERS', False) or []
+                 bbhands.append(self.dm_event_handler_name)
+                 self.cfgData.setVar('__BBHANDLERS', bbhands)
                  res = bb.event.register(self.dm_event_handler_name,
                                          lambda x: self.dm.check(self) if self.state in [runQueueRunning, runQueueCleanUp] else False,
                                          ('bb.event.HeartbeatEvent',))
Signature · 1 line
2.30.0
#1 lore raw
Quoted 18 lines · Tomasz Dziendzielski

When multiconfig is used bitbake might try to run events that don't exist for specific mc target. In cooker.py we pass self.databuilder.mcdata[mc] data that contains names of events' handlers per mc target, but fire_class_handlers uses global _handlers variable that is created during parsing of all the targets.

This leads to a problem where bitbake runs event handler that don't exist for a target or even overrides them - if multiple targets use event handler with the same name but different code then only one version will be executed for all targets.

See [YOCTO #13071] for detailed bug information.

Add mc target name as a prefix to event handler name so there won't be two different handlers with the same name.

Quoted 8 lines

--- v2 compared to previous patch I added mc prefix before putting to __BBHANDLERS, so hashes are not changed during the build and oe-selftest paseses
--- v3 fix for test_stoptask_behavior, _bb_diskmonitor handler is an exception that is not created during parsing and was not added to __BBHANDLERS where I check if a handler should be executed. In this change I add it manually, so it is detected and executed
---

 lib/bb/cookerdata.py | 5 ++++-
 lib/bb/event.py      | 2 ++
 lib/bb/parse/ast.py  | 7 ++++++-
 lib/bb/runqueue.py   | 3 +++
 4 files changed, 15 insertions(+), 2 deletions(-)

This seemed to trigger a different problem, seen in a few different builds/tests:

https://autobuilder.yoctoproject.org/typhoon/#/builders/83/builds/1831

Cheers,

Richard

#2 lore raw
Quoted 4 lines

This seemed to trigger a different problem, seen in a few different builds/tests:

https://autobuilder.yoctoproject.org/typhoon/#/builders/83/builds/1831

The error here is:

Quoted 1 line

ERROR: Undefined event handler function 'kmonitor_139920531531424'

which means I did not add the multiconfig prefix to _bb_diskmonitor_ handler and next step wanted to remove that prefix and wrongly cut the name. I submitted another patch where correctly I added the prefix to handler name:
https://lists.openembedded.org/g/bitbake-devel/message/12002

Best regards,
Tomasz Dziendzielski