Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Process based server. |
| 3 | # |
| 4 | # Copyright (C) 2010 Bob Foerster <robert@erafx.com> |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | """ |
| 10 | This module implements a multiprocessing.Process based server for bitbake. |
| 11 | """ |
| 12 | |
| 13 | import bb |
| 14 | import bb.event |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import logging |
| 16 | import multiprocessing |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 17 | import threading |
| 18 | import array |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | import sys |
| 21 | import time |
| 22 | import select |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | import socket |
| 24 | import subprocess |
| 25 | import errno |
| 26 | import re |
| 27 | import datetime |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 28 | import pickle |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 29 | import bb.server.xmlrpcserver |
| 30 | from bb import daemonize |
| 31 | from multiprocessing import queues |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 33 | logger = logging.getLogger('BitBake') |
| 34 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 35 | class ProcessTimeout(SystemExit): |
| 36 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 38 | def serverlog(msg): |
| 39 | print(str(os.getpid()) + " " + datetime.datetime.now().strftime('%H:%M:%S.%f') + " " + msg) |
| 40 | sys.stdout.flush() |
| 41 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 42 | class ProcessServer(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | profile_filename = "profile.log" |
| 44 | profile_processed_filename = "profile.log.processed" |
| 45 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 46 | def __init__(self, lock, lockname, sock, sockname, server_timeout, xmlrpcinterface): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 47 | self.command_channel = False |
| 48 | self.command_channel_reply = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | self.quit = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | self.heartbeat_seconds = 1 # default, BB_HEARTBEAT_EVENT will be checked once we have a datastore. |
| 51 | self.next_heartbeat = time.time() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 53 | self.event_handle = None |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 54 | self.hadanyui = False |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | self.haveui = False |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 56 | self.maxuiwait = 30 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | self.xmlrpc = False |
| 58 | |
| 59 | self._idlefuns = {} |
| 60 | |
| 61 | self.bitbake_lock = lock |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 62 | self.bitbake_lock_name = lockname |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 63 | self.sock = sock |
| 64 | self.sockname = sockname |
| 65 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 66 | self.server_timeout = server_timeout |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 67 | self.timeout = self.server_timeout |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 68 | self.xmlrpcinterface = xmlrpcinterface |
| 69 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 70 | def register_idle_function(self, function, data): |
| 71 | """Register a function to be called while the server is idle""" |
| 72 | assert hasattr(function, '__call__') |
| 73 | self._idlefuns[function] = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
| 75 | def run(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 76 | |
| 77 | if self.xmlrpcinterface[0]: |
| 78 | self.xmlrpc = bb.server.xmlrpcserver.BitBakeXMLRPCServer(self.xmlrpcinterface, self.cooker, self) |
| 79 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 80 | serverlog("Bitbake XMLRPC server address: %s, server port: %s" % (self.xmlrpc.host, self.xmlrpc.port)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 81 | |
| 82 | try: |
| 83 | self.bitbake_lock.seek(0) |
| 84 | self.bitbake_lock.truncate() |
| 85 | if self.xmlrpc: |
| 86 | self.bitbake_lock.write("%s %s:%s\n" % (os.getpid(), self.xmlrpc.host, self.xmlrpc.port)) |
| 87 | else: |
| 88 | self.bitbake_lock.write("%s\n" % (os.getpid())) |
| 89 | self.bitbake_lock.flush() |
| 90 | except Exception as e: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 91 | serverlog("Error writing to lock file: %s" % str(e)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 92 | pass |
| 93 | |
| 94 | if self.cooker.configuration.profile: |
| 95 | try: |
| 96 | import cProfile as profile |
| 97 | except: |
| 98 | import profile |
| 99 | prof = profile.Profile() |
| 100 | |
| 101 | ret = profile.Profile.runcall(prof, self.main) |
| 102 | |
| 103 | prof.dump_stats("profile.log") |
| 104 | bb.utils.process_profilelog("profile.log") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 105 | serverlog("Raw profiling information saved to profile.log and processed statistics to profile.log.processed") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 106 | |
| 107 | else: |
| 108 | ret = self.main() |
| 109 | |
| 110 | return ret |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | |
| 112 | def main(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 113 | self.cooker.pre_serve() |
| 114 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 115 | bb.utils.set_process_name("Cooker") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 116 | |
| 117 | ready = [] |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 118 | newconnections = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 119 | |
| 120 | self.controllersock = False |
| 121 | fds = [self.sock] |
| 122 | if self.xmlrpc: |
| 123 | fds.append(self.xmlrpc) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 124 | seendata = False |
| 125 | serverlog("Entering server connection loop") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 126 | |
| 127 | def disconnect_client(self, fds): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 128 | serverlog("Disconnecting Client") |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 129 | if self.controllersock: |
| 130 | fds.remove(self.controllersock) |
| 131 | self.controllersock.close() |
| 132 | self.controllersock = False |
| 133 | if self.haveui: |
| 134 | fds.remove(self.command_channel) |
| 135 | bb.event.unregister_UIHhandler(self.event_handle, True) |
| 136 | self.command_channel_reply.writer.close() |
| 137 | self.event_writer.writer.close() |
| 138 | self.command_channel.close() |
| 139 | self.command_channel = False |
| 140 | del self.event_writer |
| 141 | self.lastui = time.time() |
| 142 | self.cooker.clientComplete() |
| 143 | self.haveui = False |
| 144 | ready = select.select(fds,[],[],0)[0] |
| 145 | if newconnections: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 146 | serverlog("Starting new client") |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 147 | conn = newconnections.pop(-1) |
| 148 | fds.append(conn) |
| 149 | self.controllersock = conn |
| 150 | elif self.timeout is None and not ready: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 151 | serverlog("No timeout, exiting.") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 152 | self.quit = True |
| 153 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 154 | self.lastui = time.time() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | while not self.quit: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 156 | if self.sock in ready: |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 157 | while select.select([self.sock],[],[],0)[0]: |
| 158 | controllersock, address = self.sock.accept() |
| 159 | if self.controllersock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 160 | serverlog("Queuing %s (%s)" % (str(ready), str(newconnections))) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 161 | newconnections.append(controllersock) |
| 162 | else: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 163 | serverlog("Accepting %s (%s)" % (str(ready), str(newconnections))) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 164 | self.controllersock = controllersock |
| 165 | fds.append(controllersock) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 166 | if self.controllersock in ready: |
| 167 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 168 | serverlog("Processing Client") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 169 | ui_fds = recvfds(self.controllersock, 3) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 170 | serverlog("Connecting Client") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 171 | |
| 172 | # Where to write events to |
| 173 | writer = ConnectionWriter(ui_fds[0]) |
| 174 | self.event_handle = bb.event.register_UIHhandler(writer, True) |
| 175 | self.event_writer = writer |
| 176 | |
| 177 | # Where to read commands from |
| 178 | reader = ConnectionReader(ui_fds[1]) |
| 179 | fds.append(reader) |
| 180 | self.command_channel = reader |
| 181 | |
| 182 | # Where to send command return values to |
| 183 | writer = ConnectionWriter(ui_fds[2]) |
| 184 | self.command_channel_reply = writer |
| 185 | |
| 186 | self.haveui = True |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 187 | self.hadanyui = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 188 | |
| 189 | except (EOFError, OSError): |
| 190 | disconnect_client(self, fds) |
| 191 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 192 | if not self.timeout == -1.0 and not self.haveui and self.timeout and \ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 193 | (self.lastui + self.timeout) < time.time(): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 194 | serverlog("Server timeout, exiting.") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 195 | self.quit = True |
| 196 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 197 | # If we don't see a UI connection within maxuiwait, its unlikely we're going to see |
| 198 | # one. We have had issue with processes hanging indefinitely so timing out UI-less |
| 199 | # servers is useful. |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 200 | if not self.hadanyui and not self.xmlrpc and not self.timeout and (self.lastui + self.maxuiwait) < time.time(): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 201 | serverlog("No UI connection within max timeout, exiting to avoid infinite loop.") |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 202 | self.quit = True |
| 203 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 204 | if self.command_channel in ready: |
| 205 | try: |
| 206 | command = self.command_channel.get() |
| 207 | except EOFError: |
| 208 | # Client connection shutting down |
| 209 | ready = [] |
| 210 | disconnect_client(self, fds) |
| 211 | continue |
| 212 | if command[0] == "terminateServer": |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 213 | self.quit = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 214 | continue |
| 215 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 216 | serverlog("Running command %s" % command) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 217 | self.command_channel_reply.send(self.cooker.command.runCommand(command)) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 218 | serverlog("Command Completed") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 219 | except Exception as e: |
| 220 | logger.exception('Exception in server main event loop running command %s (%s)' % (command, str(e))) |
| 221 | |
| 222 | if self.xmlrpc in ready: |
| 223 | self.xmlrpc.handle_requests() |
| 224 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 225 | if not seendata and hasattr(self.cooker, "data"): |
| 226 | heartbeat_event = self.cooker.data.getVar('BB_HEARTBEAT_EVENT') |
| 227 | if heartbeat_event: |
| 228 | try: |
| 229 | self.heartbeat_seconds = float(heartbeat_event) |
| 230 | except: |
| 231 | bb.warn('Ignoring invalid BB_HEARTBEAT_EVENT=%s, must be a float specifying seconds.' % heartbeat_event) |
| 232 | |
| 233 | self.timeout = self.server_timeout or self.cooker.data.getVar('BB_SERVER_TIMEOUT') |
| 234 | try: |
| 235 | if self.timeout: |
| 236 | self.timeout = float(self.timeout) |
| 237 | except: |
| 238 | bb.warn('Ignoring invalid BB_SERVER_TIMEOUT=%s, must be a float specifying seconds.' % self.timeout) |
| 239 | seendata = True |
| 240 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 241 | ready = self.idle_commands(.1, fds) |
| 242 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 243 | if len(threading.enumerate()) != 1: |
| 244 | serverlog("More than one thread left?: " + str(threading.enumerate())) |
| 245 | |
| 246 | serverlog("Exiting") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 247 | # Remove the socket file so we don't get any more connections to avoid races |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 248 | try: |
| 249 | os.unlink(self.sockname) |
| 250 | except: |
| 251 | pass |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 252 | self.sock.close() |
| 253 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 254 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 255 | self.cooker.shutdown(True) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 256 | self.cooker.notifier.stop() |
| 257 | self.cooker.confignotifier.stop() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 258 | except: |
| 259 | pass |
| 260 | |
| 261 | self.cooker.post_serve() |
| 262 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 263 | # Flush logs before we release the lock |
| 264 | sys.stdout.flush() |
| 265 | sys.stderr.flush() |
| 266 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 267 | # Finally release the lockfile but warn about other processes holding it open |
| 268 | lock = self.bitbake_lock |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 269 | lockfile = self.bitbake_lock_name |
| 270 | |
| 271 | def get_lock_contents(lockfile): |
| 272 | try: |
| 273 | with open(lockfile, "r") as f: |
| 274 | return f.readlines() |
| 275 | except FileNotFoundError: |
| 276 | return None |
| 277 | |
| 278 | lockcontents = get_lock_contents(lockfile) |
| 279 | serverlog("Original lockfile contents: " + str(lockcontents)) |
| 280 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 281 | lock.close() |
| 282 | lock = None |
| 283 | |
| 284 | while not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 285 | i = 0 |
| 286 | lock = None |
| 287 | while not lock and i < 30: |
| 288 | lock = bb.utils.lockfile(lockfile, shared=False, retry=False, block=False) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 289 | if not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 290 | newlockcontents = get_lock_contents(lockfile) |
| 291 | if newlockcontents != lockcontents: |
| 292 | # A new server was started, the lockfile contents changed, we can exit |
| 293 | serverlog("Lockfile now contains different contents, exiting: " + str(newlockcontents)) |
| 294 | return |
| 295 | time.sleep(0.1) |
| 296 | i += 1 |
| 297 | if lock: |
| 298 | # We hold the lock so we can remove the file (hide stale pid data) |
| 299 | # via unlockfile. |
| 300 | bb.utils.unlockfile(lock) |
| 301 | serverlog("Exiting as we could obtain the lock") |
| 302 | return |
| 303 | |
| 304 | if not lock: |
| 305 | # Some systems may not have lsof available |
| 306 | procs = None |
| 307 | try: |
| 308 | procs = subprocess.check_output(["lsof", '-w', lockfile], stderr=subprocess.STDOUT) |
| 309 | except subprocess.CalledProcessError: |
| 310 | # File was deleted? |
| 311 | continue |
| 312 | except OSError as e: |
| 313 | if e.errno != errno.ENOENT: |
| 314 | raise |
| 315 | if procs is None: |
| 316 | # Fall back to fuser if lsof is unavailable |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 317 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 318 | procs = subprocess.check_output(["fuser", '-v', lockfile], stderr=subprocess.STDOUT) |
| 319 | except subprocess.CalledProcessError: |
| 320 | # File was deleted? |
| 321 | continue |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 322 | except OSError as e: |
| 323 | if e.errno != errno.ENOENT: |
| 324 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 325 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 326 | msg = "Delaying shutdown due to active processes which appear to be holding bitbake.lock" |
| 327 | if procs: |
| 328 | msg += ":\n%s" % str(procs.decode("utf-8")) |
| 329 | serverlog(msg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 330 | |
| 331 | def idle_commands(self, delay, fds=None): |
| 332 | nextsleep = delay |
| 333 | if not fds: |
| 334 | fds = [] |
| 335 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 336 | for function, data in list(self._idlefuns.items()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | try: |
| 338 | retval = function(self, data, False) |
| 339 | if retval is False: |
| 340 | del self._idlefuns[function] |
| 341 | nextsleep = None |
| 342 | elif retval is True: |
| 343 | nextsleep = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 344 | elif isinstance(retval, float) and nextsleep: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 345 | if (retval < nextsleep): |
| 346 | nextsleep = retval |
| 347 | elif nextsleep is None: |
| 348 | continue |
| 349 | else: |
| 350 | fds = fds + retval |
| 351 | except SystemExit: |
| 352 | raise |
| 353 | except Exception as exc: |
| 354 | if not isinstance(exc, bb.BBHandledException): |
| 355 | logger.exception('Running idle function') |
| 356 | del self._idlefuns[function] |
| 357 | self.quit = True |
| 358 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 359 | # Create new heartbeat event? |
| 360 | now = time.time() |
| 361 | if now >= self.next_heartbeat: |
| 362 | # We might have missed heartbeats. Just trigger once in |
| 363 | # that case and continue after the usual delay. |
| 364 | self.next_heartbeat += self.heartbeat_seconds |
| 365 | if self.next_heartbeat <= now: |
| 366 | self.next_heartbeat = now + self.heartbeat_seconds |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 367 | if hasattr(self.cooker, "data"): |
| 368 | heartbeat = bb.event.HeartbeatEvent(now) |
| 369 | bb.event.fire(heartbeat, self.cooker.data) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 370 | if nextsleep and now + nextsleep > self.next_heartbeat: |
| 371 | # Shorten timeout so that we we wake up in time for |
| 372 | # the heartbeat. |
| 373 | nextsleep = self.next_heartbeat - now |
| 374 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 375 | if nextsleep is not None: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 376 | if self.xmlrpc: |
| 377 | nextsleep = self.xmlrpc.get_timeout(nextsleep) |
| 378 | try: |
| 379 | return select.select(fds,[],[],nextsleep)[0] |
| 380 | except InterruptedError: |
| 381 | # Ignore EINTR |
| 382 | return [] |
| 383 | else: |
| 384 | return select.select(fds,[],[],0)[0] |
| 385 | |
| 386 | |
| 387 | class ServerCommunicator(): |
| 388 | def __init__(self, connection, recv): |
| 389 | self.connection = connection |
| 390 | self.recv = recv |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 391 | |
| 392 | def runCommand(self, command): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 393 | self.connection.send(command) |
| 394 | if not self.recv.poll(30): |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 395 | logger.info("No reply from server in 30s") |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 396 | if not self.recv.poll(30): |
| 397 | raise ProcessTimeout("Timeout while waiting for a reply from the bitbake server (60s)") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 398 | ret, exc = self.recv.get() |
| 399 | # Should probably turn all exceptions in exc back into exceptions? |
| 400 | # For now, at least handle BBHandledException |
| 401 | if exc and ("BBHandledException" in exc or "SystemExit" in exc): |
| 402 | raise bb.BBHandledException() |
| 403 | return ret, exc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 404 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 405 | def updateFeatureSet(self, featureset): |
| 406 | _, error = self.runCommand(["setFeatures", featureset]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 407 | if error: |
| 408 | logger.error("Unable to set the cooker to the correct featureset: %s" % error) |
| 409 | raise BaseException(error) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 410 | |
| 411 | def getEventHandle(self): |
| 412 | handle, error = self.runCommand(["getUIHandlerNum"]) |
| 413 | if error: |
| 414 | logger.error("Unable to get UI Handler Number: %s" % error) |
| 415 | raise BaseException(error) |
| 416 | |
| 417 | return handle |
| 418 | |
| 419 | def terminateServer(self): |
| 420 | self.connection.send(['terminateServer']) |
| 421 | return |
| 422 | |
| 423 | class BitBakeProcessServerConnection(object): |
| 424 | def __init__(self, ui_channel, recv, eq, sock): |
| 425 | self.connection = ServerCommunicator(ui_channel, recv) |
| 426 | self.events = eq |
| 427 | # Save sock so it doesn't get gc'd for the life of our connection |
| 428 | self.socket_connection = sock |
| 429 | |
| 430 | def terminate(self): |
| 431 | self.socket_connection.close() |
| 432 | self.connection.connection.close() |
| 433 | self.connection.recv.close() |
| 434 | return |
| 435 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 436 | start_log_format = '--- Starting bitbake server pid %s at %s ---' |
| 437 | start_log_datetime_format = '%Y-%m-%d %H:%M:%S.%f' |
| 438 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 439 | class BitBakeServer(object): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 440 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 441 | def __init__(self, lock, sockname, featureset, server_timeout, xmlrpcinterface): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 442 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 443 | self.server_timeout = server_timeout |
| 444 | self.xmlrpcinterface = xmlrpcinterface |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 445 | self.featureset = featureset |
| 446 | self.sockname = sockname |
| 447 | self.bitbake_lock = lock |
| 448 | self.readypipe, self.readypipein = os.pipe() |
| 449 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 450 | # Place the log in the builddirectory alongside the lock file |
| 451 | logfile = os.path.join(os.path.dirname(self.bitbake_lock.name), "bitbake-cookerdaemon.log") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 452 | self.logfile = logfile |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 453 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 454 | startdatetime = datetime.datetime.now() |
| 455 | bb.daemonize.createDaemon(self._startServer, logfile) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 456 | self.bitbake_lock.close() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 457 | os.close(self.readypipein) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 458 | |
| 459 | ready = ConnectionReader(self.readypipe) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 460 | r = ready.poll(5) |
| 461 | if not r: |
| 462 | bb.note("Bitbake server didn't start within 5 seconds, waiting for 90") |
| 463 | r = ready.poll(90) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 464 | if r: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 465 | try: |
| 466 | r = ready.get() |
| 467 | except EOFError: |
| 468 | # Trap the child exitting/closing the pipe and error out |
| 469 | r = None |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 470 | if not r or r[0] != "r": |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 471 | ready.close() |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 472 | bb.error("Unable to start bitbake server (%s)" % str(r)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 473 | if os.path.exists(logfile): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 474 | logstart_re = re.compile(start_log_format % ('([0-9]+)', '([0-9-]+ [0-9:.]+)')) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 475 | started = False |
| 476 | lines = [] |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 477 | lastlines = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 478 | with open(logfile, "r") as f: |
| 479 | for line in f: |
| 480 | if started: |
| 481 | lines.append(line) |
| 482 | else: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 483 | lastlines.append(line) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 484 | res = logstart_re.search(line.rstrip()) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 485 | if res: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 486 | ldatetime = datetime.datetime.strptime(res.group(2), start_log_datetime_format) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 487 | if ldatetime >= startdatetime: |
| 488 | started = True |
| 489 | lines.append(line) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 490 | if len(lastlines) > 60: |
| 491 | lastlines = lastlines[-60:] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 492 | if lines: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 493 | if len(lines) > 60: |
| 494 | bb.error("Last 60 lines of server log for this session (%s):\n%s" % (logfile, "".join(lines[-60:]))) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 495 | else: |
| 496 | bb.error("Server log for this session (%s):\n%s" % (logfile, "".join(lines))) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 497 | elif lastlines: |
| 498 | bb.error("Server didn't start, last 60 loglines (%s):\n%s" % (logfile, "".join(lastlines))) |
| 499 | else: |
| 500 | bb.error("%s doesn't exist" % logfile) |
| 501 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 502 | raise SystemExit(1) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 503 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 504 | ready.close() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 505 | |
| 506 | def _startServer(self): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 507 | os.close(self.readypipe) |
| 508 | os.set_inheritable(self.bitbake_lock.fileno(), True) |
| 509 | os.set_inheritable(self.readypipein, True) |
| 510 | serverscript = os.path.realpath(os.path.dirname(__file__) + "/../../../bin/bitbake-server") |
| 511 | os.execl(sys.executable, "bitbake-server", serverscript, "decafbad", str(self.bitbake_lock.fileno()), str(self.readypipein), self.logfile, self.bitbake_lock.name, self.sockname, str(self.server_timeout), str(self.xmlrpcinterface[0]), str(self.xmlrpcinterface[1])) |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 512 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 513 | def execServer(lockfd, readypipeinfd, lockname, sockname, server_timeout, xmlrpcinterface): |
| 514 | |
| 515 | import bb.cookerdata |
| 516 | import bb.cooker |
| 517 | |
| 518 | serverlog(start_log_format % (os.getpid(), datetime.datetime.now().strftime(start_log_datetime_format))) |
| 519 | |
| 520 | try: |
| 521 | bitbake_lock = os.fdopen(lockfd, "w") |
| 522 | |
| 523 | # Create server control socket |
| 524 | if os.path.exists(sockname): |
| 525 | os.unlink(sockname) |
| 526 | |
| 527 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 528 | # AF_UNIX has path length issues so chdir here to workaround |
| 529 | cwd = os.getcwd() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 530 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 531 | os.chdir(os.path.dirname(sockname)) |
| 532 | sock.bind(os.path.basename(sockname)) |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 533 | finally: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 534 | os.chdir(cwd) |
| 535 | sock.listen(1) |
| 536 | |
| 537 | server = ProcessServer(bitbake_lock, lockname, sock, sockname, server_timeout, xmlrpcinterface) |
| 538 | writer = ConnectionWriter(readypipeinfd) |
| 539 | try: |
| 540 | featureset = [] |
| 541 | cooker = bb.cooker.BBCooker(featureset, server.register_idle_function) |
| 542 | except bb.BBHandledException: |
| 543 | return None |
| 544 | writer.send("r") |
| 545 | writer.close() |
| 546 | server.cooker = cooker |
| 547 | serverlog("Started bitbake server pid %d" % os.getpid()) |
| 548 | |
| 549 | server.run() |
| 550 | finally: |
| 551 | # Flush any ,essages/errors to the logfile before exit |
| 552 | sys.stdout.flush() |
| 553 | sys.stderr.flush() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 554 | |
| 555 | def connectProcessServer(sockname, featureset): |
| 556 | # Connect to socket |
| 557 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 558 | # AF_UNIX has path length issues so chdir here to workaround |
| 559 | cwd = os.getcwd() |
| 560 | |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 561 | readfd = writefd = readfd1 = writefd1 = readfd2 = writefd2 = None |
| 562 | eq = command_chan_recv = command_chan = None |
| 563 | |
| 564 | sock.settimeout(10) |
| 565 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 566 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 567 | try: |
| 568 | os.chdir(os.path.dirname(sockname)) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 569 | finished = False |
| 570 | while not finished: |
| 571 | try: |
| 572 | sock.connect(os.path.basename(sockname)) |
| 573 | finished = True |
| 574 | except IOError as e: |
| 575 | if e.errno == errno.EWOULDBLOCK: |
| 576 | pass |
Richard Purdie | 3da1114 | 2019-02-05 21:34:37 +0000 | [diff] [blame] | 577 | raise |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 578 | finally: |
| 579 | os.chdir(cwd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 580 | |
| 581 | # Send an fd for the remote to write events to |
| 582 | readfd, writefd = os.pipe() |
| 583 | eq = BBUIEventQueue(readfd) |
| 584 | # Send an fd for the remote to recieve commands from |
| 585 | readfd1, writefd1 = os.pipe() |
| 586 | command_chan = ConnectionWriter(writefd1) |
| 587 | # Send an fd for the remote to write commands results to |
| 588 | readfd2, writefd2 = os.pipe() |
| 589 | command_chan_recv = ConnectionReader(readfd2) |
| 590 | |
| 591 | sendfds(sock, [writefd, readfd1, writefd2]) |
| 592 | |
| 593 | server_connection = BitBakeProcessServerConnection(command_chan, command_chan_recv, eq, sock) |
| 594 | |
| 595 | # Close the ends of the pipes we won't use |
| 596 | for i in [writefd, readfd1, writefd2]: |
| 597 | os.close(i) |
| 598 | |
| 599 | server_connection.connection.updateFeatureSet(featureset) |
| 600 | |
| 601 | except (Exception, SystemExit) as e: |
| 602 | if command_chan_recv: |
| 603 | command_chan_recv.close() |
| 604 | if command_chan: |
| 605 | command_chan.close() |
| 606 | for i in [writefd, readfd1, writefd2]: |
| 607 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 608 | if i: |
| 609 | os.close(i) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 610 | except OSError: |
| 611 | pass |
| 612 | sock.close() |
| 613 | raise |
| 614 | |
| 615 | return server_connection |
| 616 | |
| 617 | def sendfds(sock, fds): |
| 618 | '''Send an array of fds over an AF_UNIX socket.''' |
| 619 | fds = array.array('i', fds) |
| 620 | msg = bytes([len(fds) % 256]) |
| 621 | sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)]) |
| 622 | |
| 623 | def recvfds(sock, size): |
| 624 | '''Receive an array of fds over an AF_UNIX socket.''' |
| 625 | a = array.array('i') |
| 626 | bytes_size = a.itemsize * size |
| 627 | msg, ancdata, flags, addr = sock.recvmsg(1, socket.CMSG_LEN(bytes_size)) |
| 628 | if not msg and not ancdata: |
| 629 | raise EOFError |
| 630 | try: |
| 631 | if len(ancdata) != 1: |
| 632 | raise RuntimeError('received %d items of ancdata' % |
| 633 | len(ancdata)) |
| 634 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 635 | if (cmsg_level == socket.SOL_SOCKET and |
| 636 | cmsg_type == socket.SCM_RIGHTS): |
| 637 | if len(cmsg_data) % a.itemsize != 0: |
| 638 | raise ValueError |
| 639 | a.frombytes(cmsg_data) |
| 640 | assert len(a) % 256 == msg[0] |
| 641 | return list(a) |
| 642 | except (ValueError, IndexError): |
| 643 | pass |
| 644 | raise RuntimeError('Invalid data received') |
| 645 | |
| 646 | class BBUIEventQueue: |
| 647 | def __init__(self, readfd): |
| 648 | |
| 649 | self.eventQueue = [] |
| 650 | self.eventQueueLock = threading.Lock() |
| 651 | self.eventQueueNotify = threading.Event() |
| 652 | |
| 653 | self.reader = ConnectionReader(readfd) |
| 654 | |
| 655 | self.t = threading.Thread() |
| 656 | self.t.setDaemon(True) |
| 657 | self.t.run = self.startCallbackHandler |
| 658 | self.t.start() |
| 659 | |
| 660 | def getEvent(self): |
| 661 | self.eventQueueLock.acquire() |
| 662 | |
| 663 | if len(self.eventQueue) == 0: |
| 664 | self.eventQueueLock.release() |
| 665 | return None |
| 666 | |
| 667 | item = self.eventQueue.pop(0) |
| 668 | |
| 669 | if len(self.eventQueue) == 0: |
| 670 | self.eventQueueNotify.clear() |
| 671 | |
| 672 | self.eventQueueLock.release() |
| 673 | return item |
| 674 | |
| 675 | def waitEvent(self, delay): |
| 676 | self.eventQueueNotify.wait(delay) |
| 677 | return self.getEvent() |
| 678 | |
| 679 | def queue_event(self, event): |
| 680 | self.eventQueueLock.acquire() |
| 681 | self.eventQueue.append(event) |
| 682 | self.eventQueueNotify.set() |
| 683 | self.eventQueueLock.release() |
| 684 | |
| 685 | def send_event(self, event): |
| 686 | self.queue_event(pickle.loads(event)) |
| 687 | |
| 688 | def startCallbackHandler(self): |
| 689 | bb.utils.set_process_name("UIEventQueue") |
| 690 | while True: |
| 691 | try: |
| 692 | self.reader.wait() |
| 693 | event = self.reader.get() |
| 694 | self.queue_event(event) |
| 695 | except EOFError: |
| 696 | # Easiest way to exit is to close the file descriptor to cause an exit |
| 697 | break |
| 698 | self.reader.close() |
| 699 | |
| 700 | class ConnectionReader(object): |
| 701 | |
| 702 | def __init__(self, fd): |
| 703 | self.reader = multiprocessing.connection.Connection(fd, writable=False) |
| 704 | self.rlock = multiprocessing.Lock() |
| 705 | |
| 706 | def wait(self, timeout=None): |
| 707 | return multiprocessing.connection.wait([self.reader], timeout) |
| 708 | |
| 709 | def poll(self, timeout=None): |
| 710 | return self.reader.poll(timeout) |
| 711 | |
| 712 | def get(self): |
| 713 | with self.rlock: |
| 714 | res = self.reader.recv_bytes() |
| 715 | return multiprocessing.reduction.ForkingPickler.loads(res) |
| 716 | |
| 717 | def fileno(self): |
| 718 | return self.reader.fileno() |
| 719 | |
| 720 | def close(self): |
| 721 | return self.reader.close() |
| 722 | |
| 723 | |
| 724 | class ConnectionWriter(object): |
| 725 | |
| 726 | def __init__(self, fd): |
| 727 | self.writer = multiprocessing.connection.Connection(fd, readable=False) |
| 728 | self.wlock = multiprocessing.Lock() |
| 729 | # Why bb.event needs this I have no idea |
| 730 | self.event = self |
| 731 | |
| 732 | def send(self, obj): |
| 733 | obj = multiprocessing.reduction.ForkingPickler.dumps(obj) |
| 734 | with self.wlock: |
| 735 | self.writer.send_bytes(obj) |
| 736 | |
| 737 | def fileno(self): |
| 738 | return self.writer.fileno() |
| 739 | |
| 740 | def close(self): |
| 741 | return self.writer.close() |