"""Private cgroup fixture. Never run resource commands outside a bounded group.""" import json import os import time allocations = [] for line in iter(input, None): message = json.loads(line) method = message["method"] if method == "initialize": params = message["params"] result = { "protocol": params["protocol"], "session": params["session"], "commands": [{"id": "echo", "title": "Resource fixture", "description": ""}], } elif method == "initialized": continue elif method == "shutdown": break elif method == "command.invoke": action = message["params"]["arguments"].get("action", "location") if action == "detach": childPid = os.fork() if childPid == 0: os.setsid() time.sleep(300) os._exit(0) # Synchronize with the detach; inherited stdout stays open. while os.getsid(childPid) != childPid: time.sleep(0.001) result = childPid elif action == "tasks": childPids = [] for _ in range(96): try: childPid = os.fork() except BlockingIOError: break if childPid == 0: time.sleep(300) os._exit(0) childPids.append(childPid) result = len(childPids) elif action == "memory": allocation = bytearray(message["params"]["arguments"]["bytes"]) for offset in range(0, len(allocation), 4096): allocation[offset] = 1 allocations.append(allocation) result = True elif action == "cpu": result = True else: with open("/proc/self/cgroup", encoding="ascii") as source: result = source.read().strip() else: raise RuntimeError("Unexpected method") print(json.dumps({"jsonrpc": "2.0", "id": message["id"], "result": result}), flush=True) if method == "command.invoke" and action == "cpu": while True: pass