File size: 2,954 Bytes
55e0599
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Keep regular scratch in the project and multiprocessing sockets local."""
from __future__ import annotations

import hashlib
import multiprocessing as mp
from multiprocessing import util as mp_util
import os
from pathlib import Path
import socket
import stat
import tempfile


def project_temp_directory(project_root: Path) -> Path:
    directory = Path(project_root).resolve(strict=True) / "tmp"
    if directory.is_symlink():
        raise ValueError("Project tmp must be a real directory, not a symlink")
    directory.mkdir(mode=0o700, exist_ok=True)
    return directory


def _private_directory(directory: Path) -> None:
    directory.mkdir(mode=0o700, exist_ok=True)
    info = directory.lstat()
    if (not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid()
            or stat.S_IMODE(info.st_mode) & 0o077):
        raise ValueError(f"IPC directory must be private and owned by this user: {directory}")


def configure_project_temp(project_root: Path) -> dict[str, str]:
    """Call before constructing any DataLoader or starting model initialization.

    Python 3.10 caches the multiprocessing directory in the process config;
    fork/spawn/forkserver workers inherit it. Initialize that cache on the local
    filesystem, then direct ordinary tempfile calls to the project. The stdlib
    registers cleanup for its own per-run pymp directory. No stdlib monkeypatch,
    shared environment edit, or replacement of an already-active IPC path.
    """
    ordinary = project_temp_directory(project_root)
    identity = hashlib.sha256(os.fsencode(ordinary.parent)).hexdigest()[:12]
    ipc_root = Path("/tmp") / f"hfd-lzk-ipc-{os.getuid()}-{identity}"
    # AF_UNIX's 108-byte limit includes the terminating NUL.
    if len(os.fsencode(ipc_root / "pymp-12345678/listener-12345678")) >= 108:
        raise ValueError("IPC directory would exceed the Unix-socket address limit")
    _private_directory(ipc_root)
    existing = mp.current_process()._config.get("tempdir")
    if existing is not None:
        cached = Path(existing)
        if (cached.parent != ipc_root or not cached.name.startswith("pymp-")
                or not cached.is_dir()):
            raise RuntimeError("Multiprocessing already has an unrelated temporary directory")
        _private_directory(cached)
    previous = tempfile.tempdir
    try:
        tempfile.tempdir = str(ipc_root)
        ipc = Path(mp_util.get_temp_dir())
    finally:
        tempfile.tempdir = previous
    # Fail before loading a model if local sockets are unavailable.
    with tempfile.TemporaryDirectory(prefix="probe-", dir=ipc_root) as probe:
        with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener:
            listener.bind(str(Path(probe) / "listener"))
    for variable in ("TMPDIR", "TMP", "TEMP"):
        os.environ[variable] = str(ordinary)
    tempfile.tempdir = str(ordinary)
    return {"ordinary_directory": str(ordinary), "ipc_directory": str(ipc)}