From: Loïc Estève <loic.esteve@ymail.com>
Subject: MNT Vendor CPython resource tracker code for easier maintainability (#472)
 Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
 Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Origin: backport, https://github.com/joblib/loky/commit/5c28e6c1f6be265f9337630efc1b8349b0360d94
Bug: https://github.com/joblib/loky/pull/472
Index: python-loky/loky/backend/resource_tracker.py
===================================================================
--- python-loky.orig/loky/backend/resource_tracker.py
+++ python-loky/loky/backend/resource_tracker.py
@@ -38,6 +38,8 @@
 # resources once all processes connected to the resource tracker have exited.
 
 
+import base64
+import json
 import os
 import shutil
 import sys
@@ -91,6 +93,30 @@ if os.name == "posix":
 VERBOSE = False
 
 
+def _decode_message(line):
+    """Decode a message sent by a resource tracker client.
+
+    Python 3.15's multiprocessing.resource_tracker._send() always encodes
+    messages as base64-JSON instead of the legacy colon-separated format.
+    """
+    if line.startswith(b"{"):
+        obj = json.loads(line.decode("ascii"))
+        cmd, rtype = obj["cmd"], obj["rtype"]
+        name = base64.urlsafe_b64decode(obj.get("base64_name", "")).decode(
+            "utf-8", "surrogateescape"
+        )
+    else:
+        splitted = line.strip().decode("ascii").split(":")
+        # name can potentially contain separator symbols (for
+        # instance folders on Windows)
+        cmd, name, rtype = (
+            splitted[0],
+            ":".join(splitted[1:-1]),
+            splitted[-1],
+        )
+    return cmd, name, rtype
+
+
 class ResourceTracker(_ResourceTracker):
     """Resource tracker with refcounting scheme.
 
@@ -284,14 +310,7 @@ def main(fd, verbose=0):
         with open(fd, "rb") as f:
             for line in f:
                 try:
-                    splitted = line.strip().decode("ascii").split(":")
-                    # name can potentially contain separator symbols (for
-                    # instance folders on Windows)
-                    cmd, name, rtype = (
-                        splitted[0],
-                        ":".join(splitted[1:-1]),
-                        splitted[-1],
-                    )
+                    cmd, name, rtype = _decode_message(line)
 
                     if rtype not in _CLEANUP_FUNCS:
                         raise ValueError(
