Skip to content

MplexStream deadline functionality not implemented - operations can hang indefinitelyΒ #984

@acul71

Description

@acul71

Problem Description

The MplexStream implementation has deadline functionality methods (set_deadline, set_read_deadline, set_write_deadline) but the deadlines are never actually enforced in read/write operations. This means operations can hang indefinitely, leading to:

  • Resource leaks: Connections that never timeout
  • Unresponsive applications: Apps that freeze waiting for network data
  • Poor user experience: No feedback when network operations fail
  • API inconsistency: Interface promises functionality that doesn't work

Current State

Location: libp2p/stream_muxer/mplex/mplex_stream.py:244-271

The implementation has:

  • βœ… Deadline storage: self.read_deadline and self.write_deadline attributes
  • βœ… Deadline setting: Methods to set deadlines
  • ❌ Deadline enforcement: No timeout logic in read() and write() methods
  • ❌ Timeout checking: Operations can hang forever

Code Evidence

The TODO comment clearly indicates the issue:

# TODO deadline not in use
def set_deadline(self, ttl: int) -> bool:
    self.read_deadline = ttl      # Sets the deadline
    self.write_deadline = ttl     # Sets the deadline  
    return True

But the read() method has no deadline checking, so operations can hang forever.

Expected Behavior

Users should be able to set timeouts and get predictable behavior:

stream = mplex_stream
stream.set_deadline(30)  # 30 second timeout

try:
    data = await stream.read(1000)  # Should timeout after 30s
except TimeoutError:
    print("Read operation timed out")

Impact

  • High: API promises functionality that doesn't work
  • Medium: Users may expect timeouts to work and be confused when they don't
  • Low: Implementation effort - mostly adding deadline checks to existing methods

Proposed Solution

Implement deadline checking in read/write operations:

  1. Add deadline validation before operations
  2. Use trio.fail_after() for timeout enforcement
  3. Add appropriate exceptions for timeout scenarios
  4. Update tests to cover deadline functionality

Related Issues

Priority

High Priority: This is a straightforward enhancement that would complete the API's promised functionality and improve user experience for production applications.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions