Comfy-Org / Comfy-Org/ComfyUI_frontend

File handling refactor breaks loading of PNGs with NaN values in metadata

Open
#4,199 0 comments 0 reactions 0 assignees View on GitHub
area:workflows Potential Bug verified bug
Dominant language
TypeScript
Stars
2k
Forks
699
Avg merge
1d 7h
Merged PRs (30d)
490

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:
- comfyanonymous/ComfyUI#6915 - "NaN causes workflows not to load from PNG"
- 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")`:

```python
# 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:

```json
{
"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):
```typescript
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):
```typescript
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

1. **Immediate fix**: Re-implement the file handler refactor with lazy evaluation:
```typescript
return {
workflow: pngInfo?.workflow ? () => JSON.parse(pngInfo.workflow) : undefined,
prompt: pngInfo?.prompt ? () => JSON.parse(pngInfo.prompt) : undefined,
}
```

2. **Defensive parsing**: Add try-catch blocks and/or sanitize NaN values before parsing

3. **Backend fix**: Create issue in ComfyUI repo to use `None` or valid JSON instead of `float("NaN")`

## Related Issues
- Original PR: #3955
- Backend issue report: comfyanonymous/ComfyUI#6915
- Related workflow loading issue: comfyanonymous/ComfyUI#8375

┆Issue is synchronized with this [Notion page](https://www.notion.so/Issue-4199-File-handling-refactor-breaks-loading-of-PNGs-with-NaN-values-in-metadata-2146d73d3650815caaf4e1ae0ab17eef) by [Unito](https://www.unito.io)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.