Skip to content

Commit d41b558

Browse files
committed
auto merge of #9089 : anasazi/rust/fix-acceptor-iterator, r=cmr
The iterator over incoming connections has no natural end, so it should always return Some(_). Currently, if an incoming connection fails, the iterator returns None. Trying to accept another connection afterwards enters the realm of undefined behavior (due to the iterator protocol being silent on the issue). This PR changes wraps the underlying accept call in Some, so the iterator never finishes.
2 parents ed695d4 + 8f0721b commit d41b558

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/libstd/rt/io/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,21 +493,26 @@ pub trait Acceptor<T> {
493493
/// then `accept` returns `None`.
494494
fn accept(&mut self) -> Option<T>;
495495

496-
/// Create an iterator over incoming connections
496+
/// Create an iterator over incoming connection attempts
497497
fn incoming<'r>(&'r mut self) -> IncomingIterator<'r, Self> {
498498
IncomingIterator { inc: self }
499499
}
500500
}
501501

502502
/// An infinite iterator over incoming connection attempts.
503503
/// Calling `next` will block the task until a connection is attempted.
504+
///
505+
/// Since connection attempts can continue forever, this iterator always returns Some.
506+
/// The Some contains another Option representing whether the connection attempt was succesful.
507+
/// A successful connection will be wrapped in Some.
508+
/// A failed connection is represented as a None and raises a condition.
504509
struct IncomingIterator<'self, A> {
505510
priv inc: &'self mut A,
506511
}
507512

508-
impl<'self, T, A: Acceptor<T>> Iterator<T> for IncomingIterator<'self, A> {
509-
fn next(&mut self) -> Option<T> {
510-
self.inc.accept()
513+
impl<'self, T, A: Acceptor<T>> Iterator<Option<T>> for IncomingIterator<'self, A> {
514+
fn next(&mut self) -> Option<Option<T>> {
515+
Some(self.inc.accept())
511516
}
512517
}
513518

0 commit comments

Comments
 (0)