[Fix] diffusion/mesh: Guard asynchronous job publication and defaults (#46081)

Co-authored-by: Mick Qian <mickqian@users.noreply.github.com>
This commit is contained in:
Mick
2026-10-24 12:18:25 +08:00
committed by GitHub
co-authored by Mick Qian
parent cdab6c1bcc
commit f4ecaae888
3 changed files with 180 additions and 15 deletions
@@ -62,15 +62,19 @@ def _build_sampling_params_from_request(
"image_path": [image_path] if image_path else None,
"save_output": True,
"output_file_name": f"{request_id}.{ext}",
"seed": req.seed,
"generator_device": req.generator_device,
}
if req.num_inference_steps is not None:
sampling_kwargs["num_inference_steps"] = req.num_inference_steps
if req.guidance_scale is not None:
sampling_kwargs["guidance_scale"] = req.guidance_scale
if req.negative_prompt is not None:
sampling_kwargs["negative_prompt"] = req.negative_prompt
sampling_kwargs.update(
req.model_dump(
include={
"seed",
"generator_device",
"num_inference_steps",
"guidance_scale",
"negative_prompt",
},
exclude_none=True,
)
)
return SamplingParams.from_user_sampling_params_args(
model_path=server_args.model_path,
@@ -103,9 +107,7 @@ async def _dispatch_job_async(job_id: str, batch: Req) -> None:
)
save_file_path = save_file_path_list[0]
file_size = None
if os.path.exists(save_file_path):
file_size = os.path.getsize(save_file_path)
file_size = os.path.getsize(save_file_path)
cloud_url = await cloud_storage.upload_and_cleanup(save_file_path)
@@ -124,7 +126,14 @@ async def _dispatch_job_async(job_id: str, batch: Req) -> None:
except Exception as e:
logger.exception("Mesh job %s failed", job_id)
await MESH_STORE.update_fields(
job_id, {"status": "failed", "error": {"message": str(e)}}
job_id,
{
"status": "failed",
"error": {"message": str(e)},
"file_path": None,
"url": None,
"file_size_bytes": None,
},
)
@@ -218,14 +227,13 @@ async def create_mesh(
)
sampling_params = _build_sampling_params_from_request(request_id, req, input_path)
job = _mesh_job_from_sampling(request_id, req, sampling_params)
await MESH_STORE.upsert(request_id, job)
batch = prepare_request(
server_args=server_args,
sampling_params=sampling_params,
)
job = _mesh_job_from_sampling(request_id, req, sampling_params)
await MESH_STORE.upsert(request_id, job)
asyncio.create_task(_dispatch_job_async(request_id, batch))
return MeshResponse(**job)
@@ -266,6 +274,10 @@ async def download_mesh_content(
if not job:
raise HTTPException(status_code=404, detail="Mesh not found")
# an exporter may create its final path before generation finishes
if job["status"] != "completed":
raise HTTPException(status_code=404, detail="Mesh output is not available")
if job.get("url"):
raise HTTPException(
status_code=400,
@@ -14,6 +14,9 @@ from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
from starlette.datastructures import UploadFile
from sglang.multimodal_gen.configs.pipeline_configs.hunyuan3d import (
Hunyuan3D2PipelineConfig,
)
from sglang.multimodal_gen.configs.sample.sampling_params import SamplingParams
from sglang.multimodal_gen.runtime.entrypoints.openai import (
image_api,
@@ -21,6 +24,8 @@ from sglang.multimodal_gen.runtime.entrypoints.openai import (
utils,
video_api,
)
from sglang.multimodal_gen.runtime.entrypoints.openai.stores import AsyncDictStore
from sglang.multimodal_gen.runtime.pipelines_core.schedule_batch import OutputBatch
from sglang.multimodal_gen.runtime.postprocess import (
bounded_cache,
realesrgan_upscaler,
@@ -191,6 +196,133 @@ def test_multipart_upload_is_sanitized_before_generation(
assert not (tmp_path / "evil.png").exists()
@pytest.mark.parametrize("status", ["queued", "in_progress", "failed", "completed"])
def test_mesh_content_is_not_published_before_success(monkeypatch, tmp_path, status):
"""A partial mesh may exist while generation is running or after it fails."""
path = tmp_path / "mesh.glb"
path.write_bytes(b"mesh bytes")
store = AsyncDictStore()
monkeypatch.setattr(mesh_api, "MESH_STORE", store)
app = FastAPI()
app.include_router(mesh_api.router)
async def run_test():
await store.upsert("mesh", {"status": status, "file_path": str(path)})
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://test"
) as client:
response = await client.get("/v1/meshes/mesh/content")
assert response.status_code == (200 if status == "completed" else 404)
if status == "completed":
assert response.content == path.read_bytes()
asyncio.run(run_test())
@pytest.mark.parametrize("output_exists", [False, True])
def test_mesh_completion_requires_a_downloadable_file(
monkeypatch, tmp_path, output_exists
):
"""A worker-returned path is not proof that a local output can be published."""
path = tmp_path / "mesh.glb"
if output_exists:
path.write_bytes(b"mesh bytes")
store = AsyncDictStore()
monkeypatch.setattr(mesh_api, "MESH_STORE", store)
monkeypatch.setattr(mesh_api.cloud_storage, "enabled", False)
async def generate(*args, **kwargs):
return [str(path)], OutputBatch()
monkeypatch.setattr(mesh_api, "process_generation_batch", generate)
async def run_test():
await store.upsert("mesh", {"status": "queued", "file_path": str(path)})
await mesh_api._dispatch_job_async("mesh", None)
job = await store.get("mesh")
assert job["status"] == ("completed" if output_exists else "failed")
if output_exists:
assert job["file_size_bytes"] == path.stat().st_size
else:
assert job["error"] and job["file_path"] is None
asyncio.run(run_test())
def test_rejected_mesh_preparation_does_not_create_a_queued_job(monkeypatch, tmp_path):
"""Failure before scheduler submission must not leave a permanently queued job."""
store = AsyncDictStore()
monkeypatch.setattr(mesh_api, "MESH_STORE", store)
monkeypatch.setattr(mesh_api, "get_global_server_args", lambda: SimpleNamespace())
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(
mesh_api,
"_build_sampling_params_from_request",
lambda *args: SamplingParams(
prompt="mesh", output_path=str(tmp_path), output_file_name="mesh.glb"
),
)
rejected = asyncio.Event()
def reject(**kwargs):
rejected.set()
raise ValueError("request preparation failed")
monkeypatch.setattr(mesh_api, "prepare_request", reject)
app = FastAPI()
app.include_router(mesh_api.router)
async def run_test():
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app, raise_app_exceptions=False),
base_url="http://test",
) as client:
response = await client.post(
"/v1/meshes", json={"input_image": "data:image/png;base64,aW1hZ2U="}
)
assert response.status_code == 500
assert rejected.is_set()
assert await store.list_page() == []
asyncio.run(run_test())
@pytest.mark.parametrize("seed,device", [(None, None), (0, "cpu"), ([0, 7], "cuda")])
def test_mesh_optional_sampling_fields_preserve_defaults(
monkeypatch, tmp_path, seed, device
):
args = SimpleNamespace(
model_path="mesh",
backend="native",
model_id=None,
pipeline_config=Hunyuan3D2PipelineConfig(),
output_path=str(tmp_path),
comfyui_mode=False,
)
monkeypatch.setattr(mesh_api, "get_global_server_args", lambda: args)
monkeypatch.setattr(
SamplingParams, "from_pretrained", lambda *a, **kw: SamplingParams()
)
request = mesh_api.MeshGenerationsRequest(
seed=seed,
generator_device=device,
num_inference_steps=4,
guidance_scale=5.0,
negative_prompt="test",
)
sampling = mesh_api._build_sampling_params_from_request(
"mesh", request, "input.png"
)
defaults = SamplingParams()
assert sampling.seed == (defaults.seed if seed is None else seed)
assert sampling.generator_device == (
defaults.generator_device if device is None else device
)
assert sampling.num_inference_steps == 4
assert sampling.guidance_scale == 5.0
assert sampling.negative_prompt == "test"
def test_invalid_output_quality_is_a_400_not_a_500(monkeypatch):
"""A bad output_quality must surface as HTTP 400, not an unhandled ValueError."""
monkeypatch.setattr(