Skip to content

fix(yamux): add concurrency lock to YamuxStream to prevent data corruption #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sujalsalekar
Copy link

Description

This pull request introduces a critical enhancement to the concurrency model of the Yamux stream muxer in py-libp2p. The update addresses a race condition where concurrent read and write operations on the same YamuxStream instance could interleave, resulting in data corruption and unpredictable protocol behavior.

Summary of Changes

  • Introduced a dedicated asynchronous read/write lock (self.rw_lock) to the YamuxStream class.
  • Both the read and write methods are now protected by this lock, ensuring that only one coroutine can access these methods at a time for a given stream.
  • This approach aligns the concurrency safety of Yamux with the existing pattern used in the Mplex stream muxer.

Impact

  • Prevents data races and corruption in Yamux streams.
  • Improves the stability and reliability of all protocols built on top of the stream muxer.
  • Brings the concurrency model of Yamux in line with best practices and other muxer implementations.

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