fix: avoid VTK worker shutdown crashes

This commit is contained in:
NiJingzhe
2026-08-20 14:54:27 +08:00
parent bc0a967c85
commit ebff7e53d7
3 changed files with 31 additions and 30 deletions
+11 -11
View File
@@ -86,13 +86,6 @@ def _detach_offscreen_renderers() -> None:
_OFFSCREEN_WINDOW.RemoveRenderer(renderer)
def _finalize_offscreen_window() -> None:
global _OFFSCREEN_WINDOW
if _OFFSCREEN_WINDOW is None:
return
_detach_offscreen_renderers()
_OFFSCREEN_WINDOW.Finalize()
_OFFSCREEN_WINDOW = None
def _offscreen_window(width: int, height: int):
@@ -177,14 +170,21 @@ def _run_render_worker(
timeout=180.0,
check=False,
)
if completion_marker.is_file() and (
worker_output.is_file() and worker_output.stat().st_size > 0
if (
completed.returncode == 0
and completion_marker.is_file()
and worker_output.is_file()
and worker_output.stat().st_size > 0
):
break
if attempt < 2:
time.sleep(0.15 * (attempt + 1))
if not completion_marker.is_file() or (
not worker_output.is_file() or worker_output.stat().st_size == 0
if (
completed is None
or completed.returncode != 0
or not completion_marker.is_file()
or not worker_output.is_file()
or worker_output.stat().st_size == 0
):
assert completed is not None
detail = completed.stderr.strip() or completed.stdout.strip()
+13 -12
View File
@@ -111,18 +111,19 @@ def _main(manifest_path: Path) -> None:
lock_path.parent.mkdir(parents=True, exist_ok=True)
with lock_path.open("a+b") as lock_file:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
try:
if mode == "views":
_render_views(datasets, output_path, options)
elif mode == "sdk":
_render_sdk(datasets, output_path, options)
else:
raise ValueError(f"unsupported render worker mode: {mode}")
Path(payload["completion_path"]).touch()
finally:
from .render import _finalize_offscreen_window
_finalize_offscreen_window()
if mode == "views":
_render_views(datasets, output_path, options)
elif mode == "sdk":
_render_sdk(datasets, output_path, options)
else:
raise ValueError(f"unsupported render worker mode: {mode}")
Path(payload["completion_path"]).touch()
# vtkCocoaRenderWindow can crash while Python unwinds VTK wrappers after
# rendering. Exit before this frame is destroyed; the OS releases the
# one-shot worker's native resources and file lock.
sys.stdout.flush()
sys.stderr.flush()
os._exit(0)
if __name__ == "__main__":
+7 -7
View File
@@ -667,19 +667,19 @@ def test_render_entity_map_rejects_empty_duplicate_and_invalid_options(tmp_path)
def test_render_worker_retries_crash_and_accepts_completed_image(
tmp_path, monkeypatch
):
def test_render_worker_retries_crash_and_requires_clean_exit(tmp_path, monkeypatch):
calls = []
def fake_run(command, **kwargs):
del kwargs
calls.append(tuple(command))
payload = json.loads(Path(command[-1]).read_text(encoding="utf-8"))
if len(calls) == 2:
Path(payload["output_path"]).write_bytes(b"complete-image")
Path(payload["completion_path"]).touch()
return SimpleNamespace(returncode=-11, stderr="", stdout="")
Path(payload["output_path"]).write_bytes(b"crashed-image")
Path(payload["completion_path"]).touch()
if len(calls) == 1:
return SimpleNamespace(returncode=-11, stderr="", stdout="")
Path(payload["output_path"]).write_bytes(b"complete-image")
return SimpleNamespace(returncode=0, stderr="", stdout="")
monkeypatch.setattr(
"simplecadapi.inspect.brep.render.subprocess.run", fake_run