Skip to content

Conversation

@bigcat88
Copy link
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
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.

1 participant