-
Notifications
You must be signed in to change notification settings - Fork 20
listener: Ensure tcp/websocket do not terminate on listener errors #439
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
lexnv
wants to merge
4
commits into
master
Choose a base branch
from
lexnv/listener-robust
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c0431ed
transport/listener: Continue polling on recoverable errors
lexnv f296c01
transport/listener: Drop fatal error listeners
lexnv 6a7499d
tcp: Ensure the transport does not terminate on listener errors
lexnv d806675
websocket: Ensure the transport does not terminate on listener errors
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ use socket2::{Domain, Socket, Type}; | |
use tokio::net::{TcpListener as TokioTcpListener, TcpStream}; | ||
|
||
use std::{ | ||
io, | ||
io::{self, ErrorKind}, | ||
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, | ||
pin::Pin, | ||
sync::Arc, | ||
|
@@ -157,7 +157,7 @@ impl DialAddresses { | |
/// Socket listening to zero or more addresses. | ||
pub struct SocketListener { | ||
/// Listeners. | ||
listeners: Vec<TokioTcpListener>, | ||
listeners: Vec<Option<TokioTcpListener>>, | ||
/// The index in the listeners from which the polling is resumed. | ||
poll_index: usize, | ||
} | ||
|
@@ -308,7 +308,7 @@ impl SocketListener { | |
vec![local_address] | ||
}; | ||
|
||
Some((listener, listen_addresses)) | ||
Some((Some(listener), listen_addresses)) | ||
}) | ||
.unzip(); | ||
|
||
|
@@ -455,17 +455,43 @@ impl Stream for SocketListener { | |
let len = self.listeners.len(); | ||
for index in 0..len { | ||
let current = (self.poll_index + index) % len; | ||
let listener = &mut self.listeners[current]; | ||
|
||
match listener.poll_accept(cx) { | ||
Poll::Pending => {} | ||
Poll::Ready(Err(error)) => { | ||
self.poll_index = (self.poll_index + 1) % len; | ||
return Poll::Ready(Some(Err(error))); | ||
} | ||
Poll::Ready(Ok((stream, address))) => { | ||
self.poll_index = (self.poll_index + 1) % len; | ||
return Poll::Ready(Some(Ok((stream, address)))); | ||
let Some(listener) = &mut self.listeners[current] else { | ||
continue; | ||
}; | ||
|
||
loop { | ||
match listener.poll_accept(cx) { | ||
Poll::Pending => { | ||
break; | ||
} | ||
Poll::Ready(Ok((stream, address))) => { | ||
self.poll_index = (self.poll_index + 1) % len; | ||
return Poll::Ready(Some(Ok((stream, address)))); | ||
} | ||
Poll::Ready(Err(error)) => match error.kind() { | ||
ErrorKind::ConnectionAborted | ErrorKind::ConnectionReset => { | ||
tracing::debug!( | ||
target: LOG_TARGET, | ||
?error, | ||
"Recoverable error for listener, continuing", | ||
); | ||
// Poll again the listener on recoverable errors. | ||
continue; | ||
} | ||
_ => { | ||
self.poll_index = (self.poll_index + 1) % len; | ||
|
||
tracing::error!( | ||
target: LOG_TARGET, | ||
?error, | ||
"Fatal error for listener", | ||
); | ||
self.listeners[current] = None; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO a better way would be to terminate the process and let the restarted process listen again, than stopping accepting incoming connections completely. |
||
|
||
return Poll::Ready(Some(Err(error))); | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would double check that
socket.accept()
can't generate these errors continuously after a socket error — otherwise we would end up busy-looping here.