Check fork() in main thread works while a subthread is doing an import
(self)
| 22 | class ForkTest(ForkWait): |
| 23 | @warnings_helper.ignore_fork_in_thread_deprecation_warnings() |
| 24 | def test_threaded_import_lock_fork(self): |
| 25 | """Check fork() in main thread works while a subthread is doing an import""" |
| 26 | import_started = threading.Event() |
| 27 | fake_module_name = "fake test module" |
| 28 | partial_module = "partial" |
| 29 | complete_module = "complete" |
| 30 | def importer(): |
| 31 | imp.acquire_lock() |
| 32 | sys.modules[fake_module_name] = partial_module |
| 33 | import_started.set() |
| 34 | time.sleep(0.01) # Give the other thread time to try and acquire. |
| 35 | sys.modules[fake_module_name] = complete_module |
| 36 | imp.release_lock() |
| 37 | t = threading.Thread(target=importer) |
| 38 | t.start() |
| 39 | import_started.wait() |
| 40 | exitcode = 42 |
| 41 | pid = os.fork() |
| 42 | try: |
| 43 | # PyOS_BeforeFork should have waited for the import to complete |
| 44 | # before forking, so the child can recreate the import lock |
| 45 | # correctly, but also won't see a partially initialised module |
| 46 | if not pid: |
| 47 | m = __import__(fake_module_name) |
| 48 | if m == complete_module: |
| 49 | os._exit(exitcode) |
| 50 | else: |
| 51 | if support.verbose > 1: |
| 52 | print("Child encountered partial module") |
| 53 | os._exit(1) |
| 54 | else: |
| 55 | t.join() |
| 56 | # Exitcode 1 means the child got a partial module (bad.) No |
| 57 | # exitcode (but a hang, which manifests as 'got pid 0') |
| 58 | # means the child deadlocked (also bad.) |
| 59 | self.wait_impl(pid, exitcode=exitcode) |
| 60 | finally: |
| 61 | try: |
| 62 | os.kill(pid, signal.SIGKILL) |
| 63 | except OSError: |
| 64 | pass |
| 65 | |
| 66 | @warnings_helper.ignore_fork_in_thread_deprecation_warnings() |
| 67 | def test_nested_import_lock_fork(self): |