Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12)

Side by Side Diff: Lib/asyncio/selector_events.py

Issue 27906: Socket accept exhaustion during high TCP traffic
Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/asyncio/base_events.py ('k') | Lib/test/test_asyncio/test_base_events.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Event loop using a selector and related classes. 1 """Event loop using a selector and related classes.
2 2
3 A selector is a "notify-when-ready" multiplexer. For a subclass which 3 A selector is a "notify-when-ready" multiplexer. For a subclass which
4 also includes support for signal handling, see the unix_events sub-module. 4 also includes support for signal handling, see the unix_events sub-module.
5 """ 5 """
6 6
7 __all__ = ['BaseSelectorEventLoop'] 7 __all__ = ['BaseSelectorEventLoop']
8 8
9 import collections 9 import collections
10 import errno 10 import errno
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 if csock is not None: 144 if csock is not None:
145 try: 145 try:
146 csock.send(b'\0') 146 csock.send(b'\0')
147 except OSError: 147 except OSError:
148 if self._debug: 148 if self._debug:
149 logger.debug("Fail to write a null byte into the " 149 logger.debug("Fail to write a null byte into the "
150 "self-pipe socket", 150 "self-pipe socket",
151 exc_info=True) 151 exc_info=True)
152 152
153 def _start_serving(self, protocol_factory, sock, 153 def _start_serving(self, protocol_factory, sock,
154 sslcontext=None, server=None): 154 sslcontext=None, server=None, backlog=100):
155 self.add_reader(sock.fileno(), self._accept_connection, 155 self.add_reader(sock.fileno(), self._accept_connection,
156 protocol_factory, sock, sslcontext, server) 156 protocol_factory, sock, sslcontext, server, backlog)
157 157
158 def _accept_connection(self, protocol_factory, sock, 158 def _accept_connection(self, protocol_factory, sock,
159 sslcontext=None, server=None): 159 sslcontext=None, server=None, backlog=100):
160 try: 160 # This method is only called once for each event loop tick where the
161 conn, addr = sock.accept() 161 # listening socket has triggered an EVENT_READ. There may be multiple
162 if self._debug: 162 # connections waiting for an .accept() so it is called in a loop.
163 logger.debug("%r got a new connection from %r: %r", 163 # See https://bugs.python.org/issue27906 for more details.
164 server, addr, conn) 164 for _ in range(backlog):
165 conn.setblocking(False) 165 try:
166 except (BlockingIOError, InterruptedError, ConnectionAbortedError): 166 conn, addr = sock.accept()
167 pass # False alarm. 167 if self._debug:
168 except OSError as exc: 168 logger.debug("%r got a new connection from %r: %r",
169 # There's nowhere to send the error, so just log it. 169 server, addr, conn)
170 if exc.errno in (errno.EMFILE, errno.ENFILE, 170 conn.setblocking(False)
171 errno.ENOBUFS, errno.ENOMEM): 171 except (BlockingIOError, InterruptedError, ConnectionAbortedError):
172 # Some platforms (e.g. Linux keep reporting the FD as 172 # Early exit because the socket accept buffer is empty.
173 # ready, so we remove the read handler temporarily. 173 return None
174 # We'll try again in a while. 174 except OSError as exc:
175 self.call_exception_handler({ 175 # There's nowhere to send the error, so just log it.
176 'message': 'socket.accept() out of system resource', 176 if exc.errno in (errno.EMFILE, errno.ENFILE,
177 'exception': exc, 177 errno.ENOBUFS, errno.ENOMEM):
178 'socket': sock, 178 # Some platforms (e.g. Linux keep reporting the FD as
179 }) 179 # ready, so we remove the read handler temporarily.
180 self.remove_reader(sock.fileno()) 180 # We'll try again in a while.
181 self.call_later(constants.ACCEPT_RETRY_DELAY, 181 self.call_exception_handler({
182 self._start_serving, 182 'message': 'socket.accept() out of system resource',
183 protocol_factory, sock, sslcontext, server) 183 'exception': exc,
184 'socket': sock,
185 })
186 self.remove_reader(sock.fileno())
187 self.call_later(constants.ACCEPT_RETRY_DELAY,
188 self._start_serving,
189 protocol_factory, sock, sslcontext, server,
190 backlog)
191 else:
192 raise # The event loop will catch, log and ignore it.
184 else: 193 else:
185 raise # The event loop will catch, log and ignore it. 194 extra = {'peername': addr}
186 else: 195 accept = self._accept_connection2(protocol_factory, conn, extra,
187 extra = {'peername': addr} 196 sslcontext, server)
188 accept = self._accept_connection2(protocol_factory, conn, extra, 197 self.create_task(accept)
189 sslcontext, server)
190 self.create_task(accept)
191 198
192 @coroutine 199 @coroutine
193 def _accept_connection2(self, protocol_factory, conn, extra, 200 def _accept_connection2(self, protocol_factory, conn, extra,
194 sslcontext=None, server=None): 201 sslcontext=None, server=None):
195 protocol = None 202 protocol = None
196 transport = None 203 transport = None
197 try: 204 try:
198 protocol = protocol_factory() 205 protocol = protocol_factory()
199 waiter = self.create_future() 206 waiter = self.create_future()
200 if sslcontext: 207 if sslcontext:
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 except Exception as exc: 1087 except Exception as exc:
1081 self._fatal_error(exc, 1088 self._fatal_error(exc,
1082 'Fatal write error on datagram transport') 1089 'Fatal write error on datagram transport')
1083 return 1090 return
1084 1091
1085 self._maybe_resume_protocol() # May append to buffer. 1092 self._maybe_resume_protocol() # May append to buffer.
1086 if not self._buffer: 1093 if not self._buffer:
1087 self._loop.remove_writer(self._sock_fd) 1094 self._loop.remove_writer(self._sock_fd)
1088 if self._closing: 1095 if self._closing:
1089 self._call_connection_lost(None) 1096 self._call_connection_lost(None)
OLDNEW
« no previous file with comments | « Lib/asyncio/base_events.py ('k') | Lib/test/test_asyncio/test_base_events.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+