Description
File handling refactor breaks loading of PNGs with NaN values in metadata
Background
PR #3955 refactored file handling to centralize the logic into a new fileHandlers.ts
module. This PR was reverted due to issues reported in:
- NaN causes workflows not to load from PNG comfyanonymous/ComfyUI#6915 - "NaN causes workflows not to load from PNG"
- ComfyUI can't open PNG files as workflows comfyanonymous/ComfyUI#8375 - Related issue with workflow loading
Root Cause: NaN in Workflow Metadata
When ComfyUI executes a workflow, the backend adds an is_changed
field to track node state. If a node's IS_CHANGED
method throws an exception, the backend sets this field to float("NaN")
:
# From ComfyUI/execution.py, lines 52-60
try:
is_changed = _map_node_over_list(class_def, input_data_all, "IS_CHANGED")
node["is_changed"] = [None if isinstance(x, ExecutionBlocker) else x for x in is_changed]
except Exception as e:
logging.warning("WARNING: {}".format(e))
node["is_changed"] = float("NaN") # This creates invalid JSON!
finally:
self.is_changed[node_id] = node["is_changed"]
This modified node data gets saved in the PNG metadata. Example from an affected PNG:
{
"prompt": {"16": {"inputs": {"image": "animated_webp.webp"}, "class_type": "DevToolsLoadAnimatedImageTest", "_meta": {"title": "Load Animated Image"}, "is_changed": NaN}, "17": {...}},
"workflow": {"id": "cffcce2d-a13c-4a5f-929b-82f274bacc36", "nodes": [...], ...}
}
Note: The workflow
field contains valid JSON, but the prompt
field contains invalid JSON with literal NaN
.
Why the Refactor Broke This
Before (app.ts):
if (pngInfo?.workflow) {
await this.loadGraphData(JSON.parse(pngInfo.workflow), true, true, fileName)
} else if (pngInfo?.prompt) {
this.loadApiJson(JSON.parse(pngInfo.prompt), fileName)
}
The if-else structure meant that if a valid workflow existed, the prompt was never parsed.
After (fileHandlers.ts):
return {
workflow: pngInfo?.workflow ? JSON.parse(pngInfo.workflow) : undefined,
prompt: pngInfo?.prompt ? JSON.parse(pngInfo.prompt) : undefined,
}
Both workflow and prompt are parsed eagerly, causing JSON.parse()
to fail on the invalid prompt JSON.
Next Steps
-
Immediate fix: Re-implement the file handler refactor with lazy evaluation:
return { workflow: pngInfo?.workflow ? () => JSON.parse(pngInfo.workflow) : undefined, prompt: pngInfo?.prompt ? () => JSON.parse(pngInfo.prompt) : undefined, }
-
Defensive parsing: Add try-catch blocks and/or sanitize NaN values before parsing
-
Backend fix: Create issue in ComfyUI repo to use
None
or valid JSON instead offloat("NaN")
Related Issues
- Original PR: [refactor] Refactor file handling #3955
- Backend issue report: NaN causes workflows not to load from PNG comfyanonymous/ComfyUI#6915
- Related workflow loading issue: ComfyUI can't open PNG files as workflows comfyanonymous/ComfyUI#8375
┆Issue is synchronized with this Notion page by Unito