Skip to content

make VideoFromFile.get_stream_source() re-entrant and BytesIO-compatible#11456

Open
bigcat88 wants to merge 1 commit intoComfy-Org:masterfrom
bigcat88:fix/comfy-api-async/VideoFromFile-3a
Open

make VideoFromFile.get_stream_source() re-entrant and BytesIO-compatible#11456
bigcat88 wants to merge 1 commit intoComfy-Org:masterfrom
bigcat88:fix/comfy-api-async/VideoFromFile-3a

Conversation

@bigcat88
Copy link
Copy Markdown
Contributor

This PR fixes a concurrency issue in ComfyUI video inputs when the same in-memory video is consumed by multiple parallel async nodes.
Previously VideoFromFile could expose a shared BytesIO cursor, causing one consumer to advance the stream and the other to read truncated data or hit EOF.

The solution introduces _ReentrantBytesIO: a read-only, seekable BytesIO subclass that shares immutable bytes while providing an independent cursor per consumer. VideoFromFile.get_stream_source() now returns a fresh _ReentrantBytesIO instance for in-memory sources, preserving compatibility with downstream code that checks isinstance(x, BytesIO)

Other options considered:

  1. Return a new BytesIO on every call (copy each time)

    Too expensive for large videos and parallel graphs.

  2. Lock/serialize access to a shared BytesIO

    Avoids copying but removes parallelism (and still relies on cursor rewinds)

  3. Write to a temporary file and return a path

    Compatible and re-entrant via independent file handles. The smallest implementation in size, can be found here


Currently chosen: return a lightweight BytesIO-compatible re-entrant view (shared bytes, independent cursor)

Minimal overhead, preserves parallelism, and keeps compatibility with node packs that expect a BytesIO.

@bigcat88 bigcat88 force-pushed the fix/comfy-api-async/VideoFromFile-3a branch 2 times, most recently from 32fe5f4 to ddb5dce Compare December 21, 2025 16:17
@bigcat88 bigcat88 force-pushed the fix/comfy-api-async/VideoFromFile-3a branch from ddb5dce to 28db275 Compare December 21, 2025 16:27
@bigcat88 bigcat88 marked this pull request as ready for review December 21, 2025 16:49
@comfy-pr-bot
Copy link
Copy Markdown
Member

Test Evidence Check

⚠️ Warning: Test Explanation Missing

If this PR modifies behavior that requires testing, a test explanation is required. PRs lacking applicable test explanations may not be reviewed until added. Please add test explanations to ensure code quality and prevent regressions.

⚠️ Warning: Visual Documentation Missing

If this PR changes user-facing behavior, visual proof (screen recording or screenshot) is required. PRs without applicable visual documentation may not be reviewed until provided.

You can add it by:

  • GitHub: Drag & drop media directly into the PR description
  • YouTube: Include a link to a short demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants