Skip to contentSkip to conversation

server/process: fix exit racing between process and thread

PATCH hongxu 2 participants 5 messages Aug 21, 2020 – Aug 24, 2020 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

hongxu

author
#1 lore raw

Hi RP,

I have another solution to fix this issue, how about drop thread invoking, directly call sync_caches in shutdown? But there are some else multiprocessing.util.Finalize calling in bitbake/lib/bb/cooker.py, I am not sure about the side effect

bitbake/lib/bb/cooker.py +0 -0
@@ -2130,10 +2130,7 @@ class CookerParser(object):

             for c in self.bb_caches.values():
                 c.sync()

-        sync = threading.Thread(target=sync_caches)
-        sync.start()
-        # While multiprocess exit, it should wait thread terminates -        multiprocessing.util.Finalize(None, sync.join, exitpriority=-100) +        sync_caches()
         bb.codeparser.parser_cache_savemerge() bb.fetch.fetcher_parse_done()
         if self.cooker.configuration.profile:

//Hongxu

Quoted 81 lines · hongxu

Since commit [d214e55c4 server/process: Remove pointless process forking][1] applied, there is an exit racing between process and thread which causing bb_cache.dat generation to be broken

Here is a simple way to reproduce on poky

1) Edit poky
[Edit poky]

|--- a/bitbake/lib/bb/cooker.py
|+++ b/bitbake/lib/bb/cooker.py
|@@ -2126,11 +2126,13 @@ class CookerParser(object):
|                 process.join()
|
|         def sync_caches():
|+            time.sleep(2)
|             for c in self.bb_caches.values():
|                 c.sync()
|
|         sync = threading.Thread(target=sync_caches)
|         sync.start()
|+        # While multiprocess exit, it should wait thread to terminate
|         multiprocessing.util.Finalize(None, sync.join, exitpriority=-100)
|         bb.codeparser.parser_cache_savemerge()
|         bb.fetch.fetcher_parse_done()

[Edit poky]

2) Run in build

$ rm cache/ sstate-cache/  tmp/* -rf
$ bitbake -p
$ sleep 2
$ ls tmp/cache/default-glibc/qemux86-64/x86_64/bb_cache.dat* -sh
|0 tmp/cache/default-glibc/qemux86-64/x86_64/bb_cache.dat

Only a dying symlink, no real bb_cache.dat file was generated. When shutdown, it calls sync_caches in a thread to generate bb_cache.dat, and the thread is broken without finish

Get multiprocessing.Process back, then above multiprocessing.util.Finalize will work, while process exit, it will wait the thread to terminate

After applying the fix, bb_cache.dat is generated correctly:

$ ls tmp/cache/default-glibc/qemux86-64/x86_64/bb_cache.dat* -sh
|  0 tmp/cache/default-glibc/qemux86-64/x86_64/bb_cache.dat
|16M tmp/cache/default-glibc/qemux86-64/x86_64/bb_cache.dat.d2a12348d0bc9d63f396ffe81c752a4cfa21bc9ef64466305384767f375e052a

[1] http://git.openembedded.org/bitbake/commit/?id=d214e55c45f9733b3289138feec0ae3361a4a48b

---
  lib/bb/server/process.py | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
lib/bb/server/process.py +3 -2
index 65e1eab52..63b82458a 100644
@@ -34,11 +34,12 @@ logger = logging.getLogger('BitBake')
  class ProcessTimeout(SystemExit):
      pass
 
-class ProcessServer():
+class ProcessServer(multiprocessing.Process):
      profile_filename = "profile.log"
      profile_processed_filename = "profile.log.processed"
 
      def __init__(self, lock, sock, sockname, server_timeout, xmlrpcinterface):
+        multiprocessing.Process.__init__(self)
          self.command_channel = False
          self.command_channel_reply = False
          self.quit = False
@@ -487,7 +488,7 @@ class BitBakeServer(object):
              print("Started bitbake server pid %d" % os.getpid())
              sys.stdout.flush()
 
-            server.run()
+            server.start()
          finally:
              # Flush any ,essages/errors to the logfile before exit
              sys.stdout.flush()
#2 lore raw
Quoted 21 lines · Hongxu Jia

Hi RP,

I have another solution to fix this issue, how about drop thread invoking, directly call sync_caches in shutdown? But there are some else multiprocessing.util.Finalize calling in bitbake/lib/bb/cooker.py, I am not sure about the side effect

bitbake/lib/bb/cooker.py +1 -4
@@ -2130,10 +2130,7 @@ class CookerParser(object):
             for c in self.bb_caches.values():
                 c.sync()
 
-        sync = threading.Thread(target=sync_caches)
-        sync.start()
-        # While multiprocess exit, it should wait thread terminates
-        multiprocessing.util.Finalize(None, sync.join,
 exitpriority=-100)
+        sync_caches()
         bb.codeparser.parser_cache_savemerge()
         bb.fetch.fetcher_parse_done()
Quoted 1 line

if self.cooker.configuration.profile:

Well spotted!

We use a thread here to try and avoid delays whilst the cache is written out so its a performance optimisation.

I wonder if we could use atexit here to register the call to sync.join() ?

We just need to be careful this is only called in the cooker process and not any others we may fork() off. We therefore may need a wrapper to ensure that getpid() is the correct (saved) value?

Cheers,

Richard

author
#3 lore raw
Quoted 25 lines · Richard Purdie
Quoted 21 lines · Hongxu Jia

Hi RP,

I have another solution to fix this issue, how about drop thread invoking, directly call sync_caches in shutdown? But there are some else multiprocessing.util.Finalize calling in bitbake/lib/bb/cooker.py, I am not sure about the side effect

bitbake/lib/bb/cooker.py +1 -4
@@ -2130,10 +2130,7 @@ class CookerParser(object):
              for c in self.bb_caches.values():
                  c.sync()
 
-        sync = threading.Thread(target=sync_caches)
-        sync.start()
-        # While multiprocess exit, it should wait thread terminates
-        multiprocessing.util.Finalize(None, sync.join,
 exitpriority=-100)
+        sync_caches()
          bb.codeparser.parser_cache_savemerge()
          bb.fetch.fetcher_parse_done()
Quoted 1 line

if self.cooker.configuration.profile:

Well spotted!

Quoted 5 lines

We use a thread here to try and avoid delays whilst the cache is written out so its a performance optimisation.

I wonder if we could use atexit here to register the call to sync.join() ?

Yes, the original commit [1] called atexit, but commit [2] changed

it to multiprocessing.util.Finalize, I've tried [1], but it did not work for

above case any more, it seems the thread join not work in atexit

while fork daemon used.

bitbake/lib/bb/cooker.py +0 -0
@@ -28,6 +28,7 @@ import json

 import pickle
 import codecs
 import hashserv
+import atexit

 logger      = logging.getLogger("BitBake") collectlog  = logging.getLogger("BitBake.Collection") @@ -2132,8 +2133,7 @@ class CookerParser(object):

         sync = threading.Thread(target=sync_caches) sync.start()
-        multiprocessing.util.Finalize(None, sync.join, exitpriority=-100) +        atexit.register(lambda: sync.join()) bb.codeparser.parser_cache_savemerge() bb.fetch.fetcher_parse_done()
         if self.cooker.configuration.profile:

[1] http://git.openembedded.org/bitbake/commit/?id=97ce57e6f860d3e6f34cc7a603ed1eeac4f423d3
[2] http://git.openembedded.org/bitbake/commit/?id=588d339f2bde8ab6547bc0c1c4e7b83310b1318f

Quoted 4 lines

We just need to be careful this is only called in the cooker process and not any others we may fork() off. We therefore may need a wrapper to ensure that getpid() is the correct (saved) value?

Yes, after applying solution 1 fix, I check the content of bitbake.lock,

the saved getpid is the same with cooker process. But it differs

with the log message in bitbake-cookerdaemon.log (the log message

is before multiprocessing.start)

So I think the fix of get multiprocessing.Process back is the best choice

we have which reduces unknown risk

//Hongxu

Quoted 4 lines

Cheers,

Richard

#4 lore raw
Quoted 5 lines · Hongxu Jia

Yes, after applying solution 1 fix, I check the content of bitbake.lock,

the saved getpid is the same with cooker process. But it differs

Quoted 8 lines

with the log message in bitbake-cookerdaemon.log (the log message

is before multiprocessing.start)

So I think the fix of get multiprocessing.Process back is the best choice

we have which reduces unknown risk

I don't like creating extra processes just because it seems to fix an issue so I don't really want to add the multiprocessing call back, we need to understand why its breaking instead.

I went digging and found that the os._exit() call from the server was why the atexit and Finalize handlers weren't running. I sent a patch for that.

I also realised that the write for the cache file could happen after bitbake shuts down. That could be bad for a lot of reasons and means Finalize and atexit are the wrong way to do this, it has to happen before we drop the lock.

I've therefore sent a patch to fix that too.

Cheers,

Richard

author
#5 lore raw
Quoted 22 lines · Richard Purdie
Quoted 5 lines · Hongxu Jia

Yes, after applying solution 1 fix, I check the content of bitbake.lock,

the saved getpid is the same with cooker process. But it differs

Quoted 8 lines

with the log message in bitbake-cookerdaemon.log (the log message

is before multiprocessing.start)

So I think the fix of get multiprocessing.Process back is the best choice

we have which reduces unknown risk

I don't like creating extra processes just because it seems to fix an issue so I don't really want to add the multiprocessing call back, we need to understand why its breaking instead.

I went digging and found that the os._exit() call from the server was why the atexit and Finalize handlers weren't running. I sent a patch for that.

Quoted 6 lines

I also realised that the write for the cache file could happen after bitbake shuts down. That could be bad for a lot of reasons and means Finalize and atexit are the wrong way to do this, it has to happen before we drop the lock.

I've therefore sent a patch to fix that too.

Got it

//Hongxu

Quoted 4 lines

Cheers,

Richard