Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Record which process holds the installation lock when generation cannot start

Bowen Xue5df0fea151b8
diff --git a/freevideo_engine/failure_details.py b/freevideo_engine/failure_details.py
index 14b8531..99ded67 100644
--- a/freevideo_engine/failure_details.py
+++ b/freevideo_engine/failure_details.py
@@ -375,6 +375,7 @@ def launcher_failure(value, *, zh=False, other_disk=False, disk_name='', can_rep
return dict(title='', detail='', action='', kind='')
text = _without_ready_curl_diagnostics(str(value))
# Match only the cause position, never an echoed input or a log line below it.
+ # Holder diagnostics follow this exact cause; they are not part of its signature.
causes = [line.strip() for line in text.splitlines() if line.strip()]
index = 1 if causes and re.fullmatch(r'FreeVideo generation failed(?: \(exit -?\d+\))?', causes[0]) else 0
cause = re.sub(r'^(?:[A-Za-z_][\w.]*(?:Error|Exception)):\s*', '', causes[index]) if len(causes) > index else ''
@@ -897,10 +898,12 @@ def generation_failure(run, exit_code=None, *, repair=None):
report = _report(run)
redactor.structured(report) # Collect prompt values without showing the request.
# Read the whole bounded log, not a tail that could hide other output.
- # A legacy managed failure has exactly this one nonempty line and exit 1.
- if exit_code == 1 and [line for line in _read(run / 'generate.log', 8192).splitlines()
- if line.strip()] == [ENGINE_BUSY]:
- lines.append(ENGINE_BUSY)
+ # Managed failures contain only the exact busy sentence and optional holder
+ # diagnostics. Other output must never acquire a busy classification.
+ busy_lines = [line for line in _read(run / 'generate.log', 8192).splitlines() if line.strip()]
+ if exit_code == 1 and busy_lines[:1] == [ENGINE_BUSY] \
+ and all(line.startswith('Lock holder:') for line in busy_lines[1:]):
+ lines.extend(busy_lines)
# What the worker found when cuDNN refused its own files, ahead of the trace.
explained = _runtime_explanation(report, REPAIR_MARK)
if explained:
diff --git a/freevideo_engine/locking.py b/freevideo_engine/locking.py
index 9524bc6..9748aa9 100644
--- a/freevideo_engine/locking.py
+++ b/freevideo_engine/locking.py
@@ -11,6 +11,7 @@ import os
from pathlib import Path
import re
import sys
+import threading
import time
@@ -22,23 +23,31 @@ LOCK_ENV = 'FREEVIDEO_RUNTIME_LOCK_FD'
ENGINE_BUSY = 'Setup, testing or generation is already using this installation/GPU lock. Retry after it finishes.'
-def lock_holders(paths):
+def lock_holders(paths, *, deadline=None):
"""Read-only Linux diagnostics, including orphans retaining inherited FDs."""
if windows() or not Path('/proc/self/fd').is_dir():
return []
identities = set()
for path in paths:
+ if deadline is not None and time.monotonic() >= deadline:
+ break
try:
item = Path(path).stat()
identities.add((item.st_dev, item.st_ino))
except OSError:
pass
holders = []
+ if not identities:
+ return []
for directory in Path('/proc').iterdir():
+ if deadline is not None and time.monotonic() >= deadline:
+ break
if not directory.name.isdigit():
continue
try:
for fd in (directory / 'fd').iterdir():
+ if deadline is not None and time.monotonic() >= deadline:
+ break
try:
item = fd.stat()
if (item.st_dev, item.st_ino) in identities:
@@ -51,6 +60,190 @@ def lock_holders(paths):
return sorted(holders)
+# Only these small, public command identifiers may leave process diagnostics.
+_COMMAND_WORD = re.compile(r'[a-z][a-z0-9_.-]{0,40}\Z')
+_EXECUTABLE = re.compile(r'[A-Za-z0-9_][A-Za-z0-9_.-]{0,80}\Z')
+_HOLDER_TIMEOUT = 2.0
+
+
+def _executable_basename(value):
+ name = value.replace('\\', '/').rsplit('/', 1)[-1]
+ return name if _EXECUTABLE.fullmatch(name) else 'unknown'
+
+
+def _lock_command(cmdline):
+ """Pure allowlist parser: no scripts, option values, prompts or environment."""
+ arguments = cmdline.decode('utf-8', errors='replace').rstrip('\0').split('\0')
+ executable = _executable_basename(arguments[0])
+ index = 1
+ # Do not mistake a script/-c argument or an option's value for Python's -m.
+ while index < len(arguments) and arguments[index] in (
+ '-B', '-u', '-E', '-s', '-S', '-I', '-O', '-OO', '-q', '-P'):
+ index += 1
+ words = []
+ if index < len(arguments) and arguments[index] == '-m':
+ for word in arguments[index + 1:index + 4]:
+ if not _COMMAND_WORD.fullmatch(word):
+ break # Never skip an option/path to expose its value as a word.
+ words.append(word)
+ return executable, ' '.join(words)
+
+
+def _holder_line(pid, executable, command='', seconds=None):
+ line = f'Lock holder: pid {int(pid)}, {_executable_basename(executable)}'
+ if command:
+ line += f' ({command})'
+ if seconds is not None and math.isfinite(seconds) and seconds >= 0:
+ line += f', running {int(seconds)} s'
+ return line
+
+
+def _linux_holder(pid):
+ executable, command, seconds = 'unknown', '', None
+ try:
+ with (Path('/proc') / str(pid) / 'cmdline').open('rb') as stream:
+ executable, command = _lock_command(stream.read(65536))
+ except Exception:
+ pass
+ try:
+ # comm may itself contain spaces and parentheses; starttime is field 22.
+ stat = (Path('/proc') / str(pid) / 'stat').read_text()
+ ticks = int(stat.rsplit(')', 1)[1].split()[19])
+ uptime = float(Path('/proc/uptime').read_text().split()[0])
+ seconds = uptime - ticks / os.sysconf('SC_CLK_TCK')
+ except Exception:
+ pass
+ return _holder_line(pid, executable, command, seconds)
+
+
+def _windows_lock_holders(paths, deadline, lines):
+ """Restart Manager is read-only here: never request a shutdown or restart."""
+ import ctypes
+ from ctypes import POINTER, c_int32, c_uint32, c_void_p, c_wchar, c_wchar_p
+
+ class FileTime(ctypes.Structure):
+ _fields_ = [('low', c_uint32), ('high', c_uint32)]
+
+ class UniqueProcess(ctypes.Structure):
+ _fields_ = [('pid', c_uint32), ('started', FileTime)]
+
+ class ProcessInfo(ctypes.Structure):
+ _fields_ = [('process', UniqueProcess), ('app', c_wchar * 256),
+ ('service', c_wchar * 64), ('kind', c_int32),
+ ('status', c_uint32), ('session', c_uint32), ('restartable', c_int32)]
+
+ manager = ctypes.WinDLL('rstrtmgr.dll', use_last_error=True)
+ kernel = ctypes.WinDLL('kernel32.dll', use_last_error=True)
+ signatures = (
+ (manager.RmStartSession, [POINTER(c_uint32), c_uint32, c_wchar_p], c_uint32),
+ (manager.RmRegisterResources, [c_uint32, c_uint32, POINTER(c_wchar_p), c_uint32,
+ POINTER(UniqueProcess), c_uint32, POINTER(c_wchar_p)], c_uint32),
+ (manager.RmGetList, [c_uint32, POINTER(c_uint32), POINTER(c_uint32),
+ POINTER(ProcessInfo), POINTER(c_uint32)], c_uint32),
+ (manager.RmEndSession, [c_uint32], c_uint32),
+ (kernel.OpenProcess, [c_uint32, c_int32, c_uint32], c_void_p),
+ (kernel.QueryFullProcessImageNameW, [c_void_p, c_uint32, c_wchar_p, POINTER(c_uint32)], c_int32),
+ (kernel.CloseHandle, [c_void_p], c_int32),
+ )
+ for function, arguments, result in signatures:
+ function.argtypes, function.restype = arguments, result
+ session = c_uint32()
+ if time.monotonic() >= deadline or manager.RmStartSession(ctypes.byref(session), 0, ctypes.create_unicode_buffer(33)):
+ return
+ try:
+ resources = (c_wchar_p * len(paths))(*(str(Path(path).absolute()) for path in paths))
+ if not resources or time.monotonic() >= deadline:
+ return
+ if manager.RmRegisterResources(session, len(resources), resources, 0, None, 0, None):
+ return
+ needed, count, reason = c_uint32(), c_uint32(), c_uint32()
+ items = None
+ # The list can grow between calls. Bound both allocation and retries.
+ for _ in range(3):
+ if time.monotonic() >= deadline:
+ return
+ status = manager.RmGetList(session, ctypes.byref(needed), ctypes.byref(count), items, ctypes.byref(reason))
+ if status == 0:
+ break
+ if status != 234 or not 0 < needed.value <= 4096: # ERROR_MORE_DATA
+ return
+ count.value = needed.value
+ items = (ProcessInfo * count.value)()
+ else:
+ return
+ holders = {item.process.pid: item for item in (items or ())[:count.value]
+ if item.process.pid and item.process.pid != os.getpid()}
+ for pid in sorted(holders)[:5]:
+ if time.monotonic() >= deadline:
+ return
+ item = holders[pid]
+ executable = _executable_basename(item.app)
+ seconds = None
+ try:
+ ticks = (item.process.started.high << 32) | item.process.started.low
+ if ticks:
+ seconds = time.time() - (ticks / 10_000_000 - 11_644_473_600)
+ except Exception:
+ pass
+ # RM's app name is the fallback when the process has exited or access
+ # is denied. QueryFullProcessImageNameW returns no command arguments.
+ position = len(lines)
+ lines.append(_holder_line(pid, executable, seconds=seconds))
+ handle = None
+ try:
+ handle = kernel.OpenProcess(0x1000, False, pid) # QUERY_LIMITED_INFORMATION
+ if handle and time.monotonic() < deadline:
+ image = ctypes.create_unicode_buffer(32768)
+ size = c_uint32(len(image))
+ if kernel.QueryFullProcessImageNameW(handle, 0, image, ctypes.byref(size)):
+ lines[position] = _holder_line(pid, image.value, seconds=seconds)
+ except Exception:
+ pass
+ finally:
+ if handle:
+ kernel.CloseHandle(handle)
+ if len(holders) > 5:
+ lines.append(f'Lock holder: and {len(holders) - 5} more')
+ finally:
+ manager.RmEndSession(session)
+
+
+def describe_lock_holders(paths) -> list[str]:
+ """Best-effort, redacted diagnostics; even a stalled OS call gets two seconds.
+
+ A daemon thread is deliberate: native calls have no timeout, and an attempt
+ to cancel Restart Manager must not delay the busy exit or interpreter exit.
+ Completed rows remain available if a later process/API query fails or hangs.
+ """
+ lines = []
+
+ def query():
+ try:
+ if windows():
+ _windows_lock_holders(paths, deadline, lines)
+ else:
+ holders = sorted(set(lock_holders(paths, deadline=deadline)) - {os.getpid()})
+ for pid in holders[:5]:
+ if time.monotonic() >= deadline:
+ return
+ lines.append(_linux_holder(pid))
+ if len(holders) > 5:
+ lines.append(f'Lock holder: and {len(holders) - 5} more')
+ except Exception:
+ pass
+ finally:
+ done.set()
+
+ try:
+ deadline = time.monotonic() + _HOLDER_TIMEOUT
+ done = threading.Event()
+ threading.Thread(target=query, daemon=True, name='lock-holder-diagnostics').start()
+ done.wait(max(0, deadline - time.monotonic()))
+ except Exception:
+ pass
+ return lines[:]
+
+
def device_lock_path(device, path=None):
"""The lease for one device, beside the machine lease it shares a root with."""
base = Path(path or os.environ.get('FREEVIDEO_LOCK_PATH', data_root() / 'engine.lock'))
diff --git a/freevideo_engine/managed.py b/freevideo_engine/managed.py
index 25ef5a8..a19e17e 100644
--- a/freevideo_engine/managed.py
+++ b/freevideo_engine/managed.py
@@ -9,7 +9,7 @@ import sys
from .agent import enabled as agent_enabled
from .bootstrap import DEFAULT_ROOT
from . import __version__
-from .locking import runtime_lock, LOCK_ENV, ENGINE_BUSY
+from .locking import runtime_lock, describe_lock_holders, LOCK_ENV, ENGINE_BUSY
from . import processes
from .system import windows
@@ -39,7 +39,7 @@ def command_for(machine, command, arguments):
return [machine['python'], '-m', 'freevideo_engine', command, *extra, *arguments]
-def _main(argv=None):
+def _main(argv=None, *, lock_paths=None):
argv = list(sys.argv[1:] if argv is None else argv)
parser = argparse.ArgumentParser(prog='./freevideo', add_help=False)
parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)))
@@ -117,6 +117,8 @@ def _main(argv=None):
or (command == 'doctor' and '--probe' not in tail) or '--help' in tail or '-h' in tail)
snapshot = json.loads(config.read_text(encoding='utf-8'))
lock_path = os.environ.get('FREEVIDEO_LOCK_PATH', str(Path(snapshot['root']) / 'engine.lock'))
+ if lock_paths is not None:
+ lock_paths.append(lock_path)
with nullcontext() if readonly else runtime_lock(lock_path, wait_seconds=3 if command == 'generate' else 0) as descriptor:
machine = json.loads(config.read_text(encoding='utf-8'))
if machine != snapshot:
@@ -137,10 +139,31 @@ def _main(argv=None):
def main(argv=None):
+ lock_paths = []
try:
- return _main(argv)
+ return _main(argv, lock_paths=lock_paths)
except BlockingIOError:
print(ENGINE_BUSY, file=sys.stderr)
+ try:
+ if not lock_paths:
+ # Normally _main supplies the configured path. Keep failures
+ # before that point best-effort, without reading configuration.
+ path = os.environ.get('FREEVIDEO_LOCK_PATH')
+ if path:
+ lock_paths.append(path)
+ else:
+ arguments = list(sys.argv[1:] if argv is None else argv)
+ root = os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)
+ for index, argument in enumerate(arguments):
+ if argument == '--root':
+ root = arguments[index + 1]
+ elif argument.startswith('--root='):
+ root = argument.partition('=')[2]
+ lock_paths.append(Path(root) / 'engine.lock')
+ for line in describe_lock_holders(lock_paths):
+ print(line, file=sys.stderr)
+ except Exception:
+ pass
return 1
except KeyboardInterrupt:
print('Cancelled. Workers stopped; existing outputs and reports retained.', file=sys.stderr)
diff --git a/web/error_panel.js b/web/error_panel.js
index ac8efa2..e5792a4 100644
--- a/web/error_panel.js
+++ b/web/error_panel.js
@@ -89,6 +89,8 @@ const PLAIN_ERRORS = [
// line counts (after an optional "ValueError: "), or the line right under the
// worker's "FreeVideo generation failed" header, so an input echoed inside or
// below another error, even one line of a longer prompt, never turns into this advice.
+// Optional "Lock holder:" diagnostic lines follow the engine-busy cause; keep
+// matching its exact first line, so those details cannot become a new signature.
function knownError(text) {
const lines = String(text).slice(0, 65536).split(/\r?\n/).map(line => line.trim()).filter(Boolean);
const index = /^FreeVideo generation failed(?: \(exit -?\d+\))?$/.test(lines[0] ?? '') ? 1 : 0;