"""Harmless POSIX fixture: a documented GIL-held foreign call.""" import ctypes import json import os import sys import threading import time def emit(event): print(json.dumps({"event": event, "monotonic": time.monotonic()}), file=sys.stderr, flush=True) def main(): if os.name != "posix": raise RuntimeError("This fixture was scoped to POSIX libc sleep") stop = threading.Event() def heartbeat(): while not stop.wait(0.05): emit("worker_thread_heartbeat") reporter = threading.Thread(target=heartbeat, daemon=True) reporter.start() time.sleep(0.15) library = ctypes.PyDLL(None) library.sleep.argtypes = [ctypes.c_uint] library.sleep.restype = ctypes.c_uint emit("gil_hold_begin") remaining = library.sleep(2) emit("gil_hold_end") if remaining: raise RuntimeError("Fixture sleep was interrupted; observation invalid") time.sleep(0.15) stop.set() reporter.join(timeout=1) print("worker-finished") if __name__ == "__main__": main()