Improve Stream Error Handling and Classification #4945
Merged
+1,232
−74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a comprehensive error classification system for stream read/write operations, replacing the previous behavior where streams were immediately closed on any error. The new system categorizes errors into recoverable and critical types, allowing streams to continue operating for transient issues while only closing streams for critical errors that require termination.
Problem
Previously, the stream implementation would close streams immediately upon encountering any error during read or write operations. This aggressive approach led to unnecessary stream closures for transient network issues, timeouts, and other recoverable errors, reducing system resilience and efficiency.
Solution
Error Classification System
ErrorTypeRemoteDisconnect- Remote peer disconnectedErrorTypeConnectionReset- Connection reset by peerErrorTypeBrokenPipe- Broken pipe errorsErrorTypeTimeout- Network timeoutsErrorTypeProgressTimeout- Progress timeout (application-level)ErrorTypeReadDeadline/ErrorTypeWriteDeadline- Deadline errorsErrorTypeLocalNetwork- Local network issuesErrorTypeResourceExhaustion- System resource exhaustionErrorTypeProtocol- Protocol errorsErrorTypeUnknown- Unclassified errorsKey Features
MaxRecoverableRetries) to prevent infinite loops while allowing recovery from transient errorsShouldCloseStream()function for consistent error handling across the codebaseError Handling Behavior
Recoverable Errors (stream continues):
Critical Errors (stream closes):
Changes
p2p/stream/types/error_handler.gowith error classification logicp2p/stream/protocols/sync/sync_stream.goto use new error handlingp2p/stream/types/stream.gowith improved error handlingp2p/stream/types/error_handler_test.gop2p/stream/types/metric.gop2p/stream/protocols/sync/const.goTesting
ShouldCloseStream()decision logicMetrics
New metrics added to track:
Benefits
Breaking Changes
None - this is a backward-compatible improvement to error handling.