You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We propose introducing a formal Stream State mechanism in py-libp2p to track the lifecycle of a Stream. This would allow both internal components and external consumers to determine whether a stream is open, closed, reset, or has errored.
This mirrors behavior in go-libp2p and js-libp2p, and is aligned with good practices from transport protocols like TCP and QUIC.
Motivation
Currently, py-libp2p has no explicit notion of a stream’s state. As a result:
Errors from operations on a closed or reset stream are only caught at runtime.
There’s no unified API to check whether a stream is in a valid operational state.
Bugs like double-close, write-after-close, or inconsistent state transitions are harder to catch.
This makes reasoning about stream lifecycle harder for transport and application layers.
Introducing a structured StreamState will:
Improve debuggability and error prevention
Allow future stream muxers (e.g., Yamux, Mplex) to better coordinate state
Enable uniform stream lifecycle handling across libp2p implementations
Current Implementation
There is no state tracking today in the Stream class.
Streams are managed directly using asyncio reader/writer objects.
No guards exist for write/read/reset based on state.
Proposed Change
Step 1: Define a StreamState Enum
fromenumimportEnum, autoclassStreamState(Enum):
INIT=auto() # Created, not yet startedOPEN=auto() # Ready and transmitting dataRESET=auto() # Reset by remote or local peerCLOSED=auto() # Gracefully closedERRORED=auto() # Unexpected failure
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Description
We propose introducing a formal Stream State mechanism in
py-libp2p
to track the lifecycle of aStream
. This would allow both internal components and external consumers to determine whether a stream is open, closed, reset, or has errored.This mirrors behavior in
go-libp2p
andjs-libp2p
, and is aligned with good practices from transport protocols like TCP and QUIC.Motivation
Currently,
py-libp2p
has no explicit notion of a stream’s state. As a result:Introducing a structured
StreamState
will:libp2p
implementationsCurrent Implementation
Stream
class.asyncio
reader/writer objects.Proposed Change
Step 1: Define a StreamState Enum
Step 2: Add State to
Stream
ClassUpdate transitions in:
start()
→OPEN
reset()
→RESET
close()
→CLOSED
Exception
→ERRORED
Expose via:
Step 3: Guard Operations
Add guards in critical methods:
Apply similar logic to
write()
,reset()
, andclose()
.Example Usage
Design Notes
go-libp2p-core/network.Stream
semantics.Testing Plan
Next Steps
StreamState
enum and transition logicStream
classRelated Topics
yamux-py
or multiplexers for clean lifecycle managementBeta Was this translation helpful? Give feedback.
All reactions